Search in sources :

Example 1 with ValueGenerationConnectionProvider

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;
}
Also used : ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) AbstractConnectedGenerator(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)

Example 2 with ValueGenerationConnectionProvider

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

Example 3 with ValueGenerationConnectionProvider

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));
    }
}
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 4 with ValueGenerationConnectionProvider

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

ManagedConnection (org.datanucleus.store.connection.ManagedConnection)4 ValueGenerationConnectionProvider (org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider)4 Properties (java.util.Properties)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 AbstractConnectedGenerator (org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)2 ValueGenerationManager (org.datanucleus.store.valuegenerator.ValueGenerationManager)2 Configuration (org.datanucleus.Configuration)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 ConnectionPreference (org.datanucleus.store.valuegenerator.AbstractConnectedGenerator.ConnectionPreference)1 MacroString (org.datanucleus.util.MacroString)1