use of org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider 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;
}
use of org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider 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;
}
use of org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider in project datanucleus-rdbms by datanucleus.
the class NucleusSequenceImpl method setGenerator.
/**
* Method to set the value generator.
* Uses "sequence" if the datastore supports it, otherwise "increment".
*/
public void setGenerator() {
// Allocate the ValueGenerationManager for this sequence
String valueGeneratorName = null;
if (((RDBMSStoreManager) storeManager).getDatastoreAdapter().supportsOption(DatastoreAdapter.SEQUENCES)) {
valueGeneratorName = ValueGenerationStrategy.SEQUENCE.toString();
} else {
valueGeneratorName = ValueGenerationStrategy.INCREMENT.toString();
}
// Create the controlling properties for this sequence
Properties props = new Properties();
Map<String, String> seqExtensions = seqMetaData.getExtensions();
if (seqExtensions != null && seqExtensions.size() > 0) {
props.putAll(seqExtensions);
}
props.put(ValueGenerator.PROPERTY_SEQUENCE_NAME, seqMetaData.getDatastoreSequence());
if (seqMetaData.getAllocationSize() > 0) {
props.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + seqMetaData.getAllocationSize());
}
if (seqMetaData.getInitialValue() > 0) {
props.put(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE, "" + seqMetaData.getInitialValue());
}
// Get a ValueGenerationManager to create the generator
ValueGenerationManager mgr = storeManager.getValueGenerationManager();
ValueGenerationConnectionProvider connProvider = new ValueGenerationConnectionProvider() {
ManagedConnection mconn;
public ManagedConnection retrieveConnection() {
// Obtain a new connection
// Note : it may be worthwhile to use the PM's connection here however where a Sequence doesnt yet
// exist the connection would then be effectively dead until the end of the tx
// The way around this would be to find a way of checking for existence of the sequence
Configuration conf = ec.getNucleusContext().getConfiguration();
int isolationLevel = TransactionUtils.getTransactionIsolationLevelForName(conf.getStringProperty(PropertyNames.PROPERTY_VALUEGEN_TXN_ISOLATION));
this.mconn = ((RDBMSStoreManager) storeManager).getConnectionManager().getConnection(isolationLevel);
return mconn;
}
public void releaseConnection() {
try {
// Release the connection
mconn.release();
} catch (NucleusException e) {
NucleusLogger.PERSISTENCE.error(Localiser.msg("017007", e));
throw e;
}
}
};
generator = mgr.createValueGenerator(valueGeneratorName, seqMetaData.getName(), props, connProvider);
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("017003", seqMetaData.getName(), valueGeneratorName));
}
}
use of org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider in project datanucleus-core by datanucleus.
the class NucleusSequenceImpl method setGenerator.
/**
* Method to set the value generator to use.
*/
protected void setGenerator() {
// Allocate the ValueGenerationManager for this sequence
String valueGeneratorName = "sequence";
// Create the controlling properties for this sequence
Properties props = new Properties();
Map<String, String> seqExtensions = seqMetaData.getExtensions();
if (seqExtensions != null && seqExtensions.size() > 0) {
props.putAll(seqExtensions);
}
props.put("sequence-name", seqMetaData.getDatastoreSequence());
props.put("sequence-name", seqMetaData.getDatastoreSequence());
if (seqMetaData.getAllocationSize() > 0) {
props.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + seqMetaData.getAllocationSize());
}
if (seqMetaData.getInitialValue() > 0) {
props.put(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE, "" + seqMetaData.getInitialValue());
}
// Get a ValueGenerationManager to create the generator
ValueGenerationManager mgr = storeManager.getValueGenerationManager();
ValueGenerationConnectionProvider connProvider = new ValueGenerationConnectionProvider() {
ManagedConnection mconn;
public ManagedConnection retrieveConnection() {
mconn = storeManager.getConnectionManager().getConnection(ec);
return mconn;
}
public void releaseConnection() {
this.mconn.release();
this.mconn = null;
}
};
generator = mgr.createValueGenerator(valueGeneratorName, seqMetaData.getName(), props, connProvider);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
NucleusLogger.DATASTORE.debug(Localiser.msg("017003", seqMetaData.getName(), valueGeneratorName));
}
}
Aggregations