Search in sources :

Example 1 with ConnectionPreference

use of org.datanucleus.store.valuegenerator.AbstractConnectedGenerator.ConnectionPreference in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method getNextValueForValueGenerator.

/**
 * Accessor for the next value from the specified ValueGenerator.
 * This implementation caters for datastore-specific generators and provides synchronisation on the connection to the datastore.
 * @param generator The generator
 * @param ec execution context
 * @return The next value.
 */
protected Object getNextValueForValueGenerator(ValueGenerator generator, final ExecutionContext ec) {
    Object oid = null;
    synchronized (generator) {
        // It maybe would be good to change ValueGenerator to have a next taking the connectionProvider
        if (generator instanceof AbstractConnectedGenerator) {
            ConnectionPreference connPref = ((AbstractConnectedGenerator) generator).getConnectionPreference();
            final boolean newConnection;
            if (connPref == ConnectionPreference.NONE) {
                // No preference from the generator so use NEW unless overridden by the persistence property
                newConnection = !getStringProperty(PropertyNames.PROPERTY_VALUEGEN_TXN_ATTRIBUTE).equalsIgnoreCase("EXISTING");
            } else {
                newConnection = connPref == ConnectionPreference.NEW;
            }
            // RDBMS-based generator so set the connection provider
            ValueGenerationConnectionProvider connProvider = new ValueGenerationConnectionProvider() {

                ManagedConnection mconn;

                public ManagedConnection retrieveConnection() {
                    if (newConnection) {
                        mconn = connectionMgr.getConnection(TransactionUtils.getTransactionIsolationLevelForName(getStringProperty(PropertyNames.PROPERTY_VALUEGEN_TXN_ISOLATION)));
                    } else {
                        mconn = connectionMgr.getConnection(ec);
                    }
                    return mconn;
                }

                public void releaseConnection() {
                    try {
                        mconn.release();
                        mconn = null;
                    } catch (NucleusException e) {
                        String msg = Localiser.msg("050025", e);
                        NucleusLogger.VALUEGENERATION.error(msg);
                        throw new NucleusDataStoreException(msg, e);
                    }
                }
            };
            ((AbstractConnectedGenerator) generator).setConnectionProvider(connProvider);
        }
        oid = generator.next();
    }
    return oid;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) MacroString(org.datanucleus.util.MacroString) ConnectionPreference(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator.ConnectionPreference) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractConnectedGenerator(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)1 AbstractConnectedGenerator (org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)1 ConnectionPreference (org.datanucleus.store.valuegenerator.AbstractConnectedGenerator.ConnectionPreference)1 ValueGenerationConnectionProvider (org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider)1 MacroString (org.datanucleus.util.MacroString)1