Search in sources :

Example 1 with ValueGenerationManager

use of org.datanucleus.store.valuegenerator.ValueGenerationManager 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));
    }
}
Also used : Configuration(org.datanucleus.Configuration) ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ValueGenerationManager(org.datanucleus.store.valuegenerator.ValueGenerationManager) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Properties(java.util.Properties) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 2 with ValueGenerationManager

use of org.datanucleus.store.valuegenerator.ValueGenerationManager 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));
    }
}
Also used : ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ValueGenerationManager(org.datanucleus.store.valuegenerator.ValueGenerationManager) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)2 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)2 ValueGenerationConnectionProvider (org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider)2 ValueGenerationManager (org.datanucleus.store.valuegenerator.ValueGenerationManager)2 Configuration (org.datanucleus.Configuration)1 NucleusException (org.datanucleus.exceptions.NucleusException)1