Java Code Examples for com.sleepycat.je.Transaction#commit()

The following examples show how to use com.sleepycat.je.Transaction#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpgradeFrom4To5.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Override
public void performUpgrade(final Environment environment, final UpgradeInteractionHandler handler, ConfiguredObject<?> parent)
{
    Transaction transaction = null;
    reportStarting(environment, 4);

    transaction = environment.beginTransaction(null, null);

    // find all queues which are bound to a topic exchange and which have a colon in their name
    final List<AMQShortString> potentialDurableSubs = findPotentialDurableSubscriptions(environment, transaction);

    Set<String> existingQueues = upgradeQueues(environment, handler, potentialDurableSubs, transaction);
    upgradeQueueBindings(environment, handler, potentialDurableSubs, transaction);
    Set<Long> messagesToDiscard = upgradeDelivery(environment, existingQueues, handler, transaction);
    upgradeContent(environment, handler, messagesToDiscard, transaction);
    upgradeMetaData(environment, handler, messagesToDiscard, transaction);
    renameRemainingDatabases(environment, handler, transaction);
    transaction.commit();

    reportFinished(environment, 5);
}
 
Example 2
Source File: ReplicatedEnvironmentFacadeTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeginTransaction() throws Exception
{
    ReplicatedEnvironmentFacade facade = createMaster();
    Transaction txn = null;
    try
    {
        TransactionConfig transactionConfig = new TransactionConfig();
        transactionConfig.setDurability(facade.getRealMessageStoreDurability());
        txn = facade.beginTransaction(transactionConfig);
        assertNotNull("Transaction is not created", txn);
        txn.commit();
        txn = null;
    }
    finally
    {
        if (txn != null)
        {
            txn.abort();
        }
    }
}
 
Example 3
Source File: StandardEnvironmentFacadeTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testOverrideJeParameter() throws Exception
{
    // verify that transactions can be created by default
    EnvironmentFacade ef = createEnvironmentFacade();
    Transaction t = ef.beginTransaction(null);
    t.commit();
    ef.close();

    // customize the environment to be non-transactional
    ef = createEnvironmentFacade(Collections.singletonMap(EnvironmentConfig.ENV_IS_TRANSACTIONAL, "false"));
    try
    {
        ef.beginTransaction(null);
        fail("Overridden settings were not picked up on environment creation");
    }
    catch(UnsupportedOperationException e)
    {
        // pass
    }
    ef.close();
}
 
Example 4
Source File: BDBTupleStore.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTuple(final Tuple tuple) throws IOException {
	Transaction txn = null;
	
	if(USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
	final byte[] tupleBytes = TupleHelper.tupleToBytes(tuple);
	final DatabaseEntry key = new DatabaseEntry(tuple.getKey().getBytes());
	final DatabaseEntry value = new DatabaseEntry(tupleBytes);
	final OperationStatus status = database.put(txn, key, value);

       if (status != OperationStatus.SUCCESS) {
           throw new RuntimeException("Data insertion got status " + status);
       }
       
       if(txn != null) {
       	txn.commit();
       }
}
 
Example 5
Source File: BDBTupleStore.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
@Override
public Tuple readTuple(final String key) throws IOException {
	final DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes());
    final DatabaseEntry value = new DatabaseEntry();
    
	Transaction txn = null;

	if(USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
    final OperationStatus result = database.get(null, keyEntry, value, LockMode.DEFAULT);
    
    if (result != OperationStatus.SUCCESS) {
        throw new RuntimeException("Data fetch got status " + result + " for " + key);
    }
    
       if(txn != null) {
       	txn.commit();
       }
       
       final ByteBuffer byteBuffer = ByteBuffer.wrap(value.getData());
       
       return TupleHelper.decodeTuple(byteBuffer);
}
 
Example 6
Source File: BDBWriterRunnable.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
protected void storeNode(final SerializableNode node) {
	final byte[] nodeBytes = node.toByteArray();

	Transaction txn = null;
	if(OSMBDBNodeStore.USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
	final DatabaseEntry key = OSMBDBNodeStore.buildDatabaseKeyEntry(node.getId());
	final DatabaseEntry value = new DatabaseEntry(nodeBytes);
	final OperationStatus status = database.put(txn, key, value);

       if (status != OperationStatus.SUCCESS) {
           throw new RuntimeException("Data insertion got status " + status);
       }
       
       if(txn != null) {
       	txn.commit();
       }
}
 
Example 7
Source File: UpgradeFrom5To6.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void upgradeConfiguredObjectsAndDependencies(Environment environment, UpgradeInteractionHandler handler, String virtualHostName)
{
    Transaction transaction = null;
    transaction = environment.beginTransaction(null, null);
    upgradeConfiguredObjects(environment, handler, transaction, virtualHostName);
    upgradeQueueEntries(environment, transaction, virtualHostName);
    upgradeXidEntries(environment, transaction, virtualHostName);
    transaction.commit();
}
 
Example 8
Source File: UpgradeFrom5To6.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void upgradeMessages(final Environment environment, final UpgradeInteractionHandler handler)
{
    Transaction transaction = null;
    transaction = environment.beginTransaction(null, null);
    upgradeMessages(environment, handler, transaction);
    transaction.commit();
}
 
Example 9
Source File: ReplicatedEnvironmentFacadeTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void putRecord(final ReplicatedEnvironmentFacade master, final Database db, final int keyValue,
                       final String dataValue)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();

    TransactionConfig transactionConfig = new TransactionConfig();
    transactionConfig.setDurability(master.getRealMessageStoreDurability());
    Transaction txn = master.beginTransaction(transactionConfig);
    IntegerBinding.intToEntry(keyValue, key);
    StringBinding.stringToEntry(dataValue, data);

    db.put(txn, key, data);
    txn.commit();
}
 
Example 10
Source File: BDBTupleStore.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	final EnvironmentConfig envConfig = new EnvironmentConfig();
	envConfig.setTransactional(USE_TRANSACTIONS);
	envConfig.setAllowCreate(true);

	environment = new Environment(dir, envConfig);

	Transaction txn = null;
	if(USE_TRANSACTIONS) {
		txn = environment.beginTransaction(null, null);
	}
	
	final DatabaseConfig dbConfig = new DatabaseConfig();
	dbConfig.setTransactional(USE_TRANSACTIONS);
	dbConfig.setAllowCreate(true);
	//dbConfig.setSortedDuplicates(true);
	dbConfig.setDeferredWrite(true);
	//dbConfig.setKeyPrefixing(true);
	//dbConfig.setNodeMaxEntries(128);

	database = environment.openDatabase(txn, "test", dbConfig);

	if(txn != null) {
		txn.commit();
	}	
}
 
Example 11
Source File: OSMBDBNodeStore.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Init a new BDB environment in the given folder
 * @param folder
 */
@SuppressWarnings("unused")
protected void initNewBDBEnvironment(final File folder, final EnvironmentConfig envConfig) {

	final Environment dbEnv = new Environment(folder, envConfig);

	Transaction txn = null;
	if(USE_TRANSACTIONS) {
		txn = dbEnv.beginTransaction(null, null);
	}
	
	final DatabaseConfig dbConfig = new DatabaseConfig();
	dbConfig.setTransactional(USE_TRANSACTIONS);
	dbConfig.setAllowCreate(true);
	dbConfig.setSortedDuplicates(true);
	dbConfig.setDeferredWrite(true);
	//dbConfig.setKeyPrefixing(true);
	//dbConfig.setNodeMaxEntries(128);

	final Database database = dbEnv.openDatabase(txn, "osm", dbConfig);

	if(txn != null) {
		txn.commit();
	}
	
	environments.add(dbEnv);
	databases.add(database);
}
 
Example 12
Source File: DatabasePinger.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public void pingDb(EnvironmentFacade facade)
{
    try
    {
        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("Beginning ping transaction");
        }

        final Database db = facade.openDatabase(PING_DATABASE_NAME,
                                                DATABASE_CONFIG);

        DatabaseEntry key = new DatabaseEntry();
        IntegerBinding.intToEntry(ID, key);

        DatabaseEntry value = new DatabaseEntry();
        LongBinding.longToEntry(System.currentTimeMillis(), value);
        Transaction txn = null;
        try
        {
            txn = facade.beginTransaction(_pingTransactionConfig);
            db.put(txn, key, value);
            txn.commit();

            txn = null;
        }
        finally
        {
            if (LOGGER.isDebugEnabled())
            {
                LOGGER.debug("Ping transaction completed");
            }

            if (txn != null)
            {
                txn.abort();
            }
        }
    }
    catch (RuntimeException de)
    {
        RuntimeException handledException = facade.handleDatabaseException("DatabaseException from DatabasePinger ", de);
        LOGGER.debug("Non fatal exception on invoking DatabasePinger. Ignoring...", handledException);
    }
}
 
Example 13
Source File: UpgradeFrom8To9.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public void performUpgrade(final Environment environment,
                           final UpgradeInteractionHandler handler,
                           final ConfiguredObject<?> parent)
{
    reportStarting(environment, 8);

    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    final Transaction transaction = environment.beginTransaction(null, null);
    try
    {
        Database userPreferencesDb = environment.openDatabase(transaction, "USER_PREFERENCES", dbConfig);
        userPreferencesDb.close();

        try (Database userPreferencesVersionDb = environment.openDatabase(transaction,
                                                                          "USER_PREFERENCES_VERSION",
                                                                          dbConfig))
        {
            if (userPreferencesVersionDb.count() == 0L)
            {
                DatabaseEntry key = new DatabaseEntry();
                DatabaseEntry value = new DatabaseEntry();
                StringBinding.stringToEntry(DEFAULT_VERSION, key);
                LongBinding.longToEntry(System.currentTimeMillis(), value);
                OperationStatus status = userPreferencesVersionDb.put(transaction, key, value);
                if (status != OperationStatus.SUCCESS)
                {
                    throw new StoreException("Error initialising user preference version: " + status);
                }
            }
        }

        transaction.commit();
        reportFinished(environment, 9);
    }
    catch (RuntimeException e)
    {
        try
        {
            if (transaction.isValid())
            {
                transaction.abort();
            }
        }
        finally
        {
            throw e;
        }
    }
}
 
Example 14
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public void updateOrCreate(final Collection<PreferenceRecord> preferenceRecords)
{
    _useOrCloseRWLock.readLock().lock();
    try
    {
        if (!getStoreState().equals(StoreState.OPENED))
        {
            throw new IllegalStateException("PreferenceStore is not opened");
        }

        if (preferenceRecords.isEmpty())
        {
            return;
        }

        EnvironmentFacade environmentFacade = getEnvironmentFacade();
        Transaction txn = null;
        try
        {
            txn = environmentFacade.beginTransaction(null);
            updateOrCreateInternal(txn, preferenceRecords);
            txn.commit();
            txn = null;
        }
        catch (RuntimeException e)
        {
            throw environmentFacade.handleDatabaseException("Error on preferences updateOrCreate: "
                                                            + e.getMessage(),
                                                            e);
        }
        finally
        {
            if (txn != null)
            {
                abortTransactionSafely(txn, environmentFacade);
            }
        }
    }
    finally
    {
        _useOrCloseRWLock.readLock().unlock();
    }
}
 
Example 15
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private void removeAndAdd(final Collection<UUID> preferenceRecordsToRemove,
                          final Collection<PreferenceRecord> preferenceRecordsToAdd,
                          final Action<Transaction> preCommitAction)
{
    _useOrCloseRWLock.readLock().lock();
    try
    {
        final StoreState storeState = getStoreState();
        if (!storeState.equals(StoreState.OPENED))
        {
            throw new IllegalStateException(String.format("PreferenceStore is not opened. Actual state : %s", storeState));
        }

        if (preferenceRecordsToRemove.isEmpty() && preferenceRecordsToAdd.isEmpty())
        {
            return;
        }

        EnvironmentFacade environmentFacade = getEnvironmentFacade();
        Transaction txn = null;
        try
        {
            txn = environmentFacade.beginTransaction(null);
            Database preferencesDb = getPreferencesDb();
            DatabaseEntry key = new DatabaseEntry();
            UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
            for (UUID id : preferenceRecordsToRemove)
            {
                getLogger().debug("Removing preference {}", id);
                keyBinding.objectToEntry(id, key);
                OperationStatus status = preferencesDb.delete(txn, key);
                if (status == OperationStatus.NOTFOUND)
                {
                    getLogger().debug("Preference {} not found", id);
                }
            }
            updateOrCreateInternal(txn, preferenceRecordsToAdd);

            if (preCommitAction != null)
            {
                preCommitAction.performAction(txn);
            }

            txn.commit();
            txn = null;
        }
        catch (RuntimeException e)
        {
            throw environmentFacade.handleDatabaseException("Error on replacing of preferences: " + e.getMessage(),
                                                            e);
        }
        finally
        {
            if (txn != null)
            {
                abortTransactionSafely(txn, environmentFacade);
            }
        }
    }
    finally
    {
        _useOrCloseRWLock.readLock().unlock();
    }
}
 
Example 16
Source File: OrphanConfigurationRecordPurger.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private void purge() throws Exception
{
    EnvironmentConfig config = EnvironmentConfig.DEFAULT;
    config.setAllowCreate(false);
    config.setTransactional(true);

    try (Environment env = createEnvironment(config))
    {
        final int version = getVersion(env, READ_ONLY_DB_CONFIG);
        if (!ALLOWED_VERSIONS.contains(version))
        {
            throw new IllegalStateException(String.format("Store has unexpected version. Found %d expected %s",
                                                          version, ALLOWED_VERSIONS));
        }

        final Transaction tx = env.beginTransaction(null, TransactionConfig.DEFAULT);
        boolean success = false;
        try
        {
            purgeOrphans(env, tx);
            success = true;
        }
        finally
        {
            if (!success)
            {
                System.out.println("No config or config hierarchy records purged.");
                tx.abort();
            }
            else if (_dryRun)
            {
                System.out.println("No config or config hierarchy records purged - -dryRun flag specified.");
                tx.abort();
            }
            else
            {
                tx.commit();
                System.out.format("Config records(s) and associated hierarchy records purged.");
            }
        }
    }
}