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;
}
Aggregations