Search in sources :

Example 1 with AbstractConnectedGenerator

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

the class AbstractStoreManager method getNextValueForValueGenerator.

/**
 * Accessor for the next value from the specified ValueGenerator.
 * This implementation simply returns generator.next(). Any case where the generator requires datastore connections should override this method.
 * @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) {
            // datastore-based generator so set the connection provider, using connection for PM
            ValueGenerationConnectionProvider connProvider = new ValueGenerationConnectionProvider() {

                ManagedConnection mconn;

                public ManagedConnection retrieveConnection() {
                    mconn = connectionMgr.getConnection(ec);
                    return mconn;
                }

                public void releaseConnection() {
                    mconn.release();
                    mconn = null;
                }
            };
            ((AbstractConnectedGenerator) generator).setConnectionProvider(connProvider);
        }
        oid = generator.next();
    }
    return oid;
}
Also used : ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) AbstractConnectedGenerator(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)

Example 2 with AbstractConnectedGenerator

use of org.datanucleus.store.valuegenerator.AbstractConnectedGenerator 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

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