Search in sources :

Example 46 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method clearSchemaData.

/**
 * Clears all knowledge of tables, cached requests, metadata, etc and resets
 * the store manager to its initial state.
 */
private void clearSchemaData() {
    deregisterAllStoreData();
    // Clear and reinitialise the schemaHandler
    schemaHandler.clear();
    ManagedConnection mc = connectionMgr.getConnection(-1);
    try {
        dba.initialiseTypes(schemaHandler, mc);
    } finally {
        mc.release();
    }
    ((RDBMSPersistenceHandler) persistenceHandler).removeAllRequests();
}
Also used : ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 47 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.

the class SQLController method getStatementForQuery.

/**
 * Convenience method to create a new PreparedStatement for a query.
 * @param conn The Connection to use for the statement
 * @param stmtText Statement text
 * @param resultSetType Type of result set
 * @param resultSetConcurrency Concurrency for the result set
 * @return The PreparedStatement
 * @throws SQLException thrown if an error occurs creating the statement
 */
public PreparedStatement getStatementForQuery(ManagedConnection conn, String stmtText, String resultSetType, String resultSetConcurrency) throws SQLException {
    Connection c = (Connection) conn.getConnection();
    if (supportsBatching) {
        // Check for a waiting batched statement that is ready for processing
        ConnectionStatementState state = getConnectionStatementState(conn);
        if (state != null && state.processable) {
            // Process the batch statement before returning our new query statement
            processConnectionStatement(conn);
        }
    }
    // Create a new PreparedStatement for this query
    PreparedStatement ps = null;
    if (resultSetType != null || resultSetConcurrency != null) {
        int rsTypeValue = ResultSet.TYPE_FORWARD_ONLY;
        if (resultSetType != null) {
            if (resultSetType.equals(RDBMSQueryUtils.QUERY_RESULTSET_TYPE_SCROLL_SENSITIVE)) {
                rsTypeValue = ResultSet.TYPE_SCROLL_SENSITIVE;
            } else if (resultSetType.equals(RDBMSQueryUtils.QUERY_RESULTSET_TYPE_SCROLL_INSENSITIVE)) {
                rsTypeValue = ResultSet.TYPE_SCROLL_INSENSITIVE;
            }
        }
        int rsConcurrencyValue = ResultSet.CONCUR_READ_ONLY;
        if (resultSetConcurrency != null && resultSetConcurrency.equals(RDBMSQueryUtils.QUERY_RESULTSET_CONCURRENCY_UPDATEABLE)) {
            rsConcurrencyValue = ResultSet.CONCUR_UPDATABLE;
        }
        ps = c.prepareStatement(stmtText, rsTypeValue, rsConcurrencyValue);
        // In case using statement caching and given one with batched statements left hanging (C3P0)
        ps.clearBatch();
    } else {
        ps = c.prepareStatement(stmtText);
        // In case using statement caching and given one with batched statements left hanging (C3P0)
        ps.clearBatch();
    }
    if (queryTimeout > 0) {
        // Apply any query timeout
        // queryTimeout is in milliseconds
        ps.setQueryTimeout(queryTimeout / 1000);
    }
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        NucleusLogger.DATASTORE.debug(Localiser.msg("052109", ps, StringUtils.toJVMIDString(c)));
    }
    if (!jdbcStatements) {
        // Wrap with our parameter logger
        ps = new ParamLoggingPreparedStatement(ps, stmtText);
        ((ParamLoggingPreparedStatement) ps).setParamsInAngleBrackets(paramValuesInBrackets);
    }
    return ps;
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement)

Example 48 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.

the class ConnectionFactoryImpl method createManagedConnection.

/**
 * Method to create a new ManagedConnection.
 * @param ec the object that is bound the connection during its lifecycle (if for a PM/EM operation)
 * @param options Options for creating the connection (optional)
 * @return The ManagedConnection
 */
public ManagedConnection createManagedConnection(ExecutionContext ec, Map options) {
    if (dataSource == null) {
        // Lazy initialisation of DataSource
        initialiseDataSource();
    }
    ManagedConnection mconn = new ManagedConnectionImpl(ec, options);
    boolean singleConnection = storeMgr.getBooleanProperty(PropertyNames.PROPERTY_CONNECTION_SINGLE_CONNECTION);
    boolean releaseAfterUse = storeMgr.getBooleanProperty(PropertyNames.PROPERTY_CONNECTION_NONTX_RELEASE_AFTER_USE);
    if (ec != null && !ec.getTransaction().isActive() && (!releaseAfterUse || singleConnection)) {
        // Non-transactional connection and requested not to close on release
        mconn.setCloseOnRelease(false);
    }
    return mconn;
}
Also used : ManagedConnection(org.datanucleus.store.connection.ManagedConnection) AbstractManagedConnection(org.datanucleus.store.connection.AbstractManagedConnection)

Example 49 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection 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 50 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class SchemaTest method testEmbeddedMap.

/**
 * Test for JPA embedded map keys/values.
 */
public void testEmbeddedMap() throws Exception {
    addClassesToSchema(new Class[] { EmbeddedMapOwner.class, EmbeddedMapKey.class, EmbeddedMapValue.class });
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        // Map with embedded value taking default value column names
        Set<String> columnNames = new HashSet<String>();
        // FK to owner
        columnNames.add("JPA_MAP_EMB_OWNER_ID");
        // Key
        columnNames.add("MAPEMBEDDEDVALUE_KEY");
        // Value "name"
        columnNames.add("NAME");
        // Value "value"
        columnNames.add("VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_MAP_EMB_VALUE", columnNames);
        // Map with embedded value overriding the value column names
        Set<String> columnNames2 = new HashSet<String>();
        // FK to owner
        columnNames2.add("JPA_MAP_EMB_OWNER_ID");
        // Key "name"
        columnNames2.add("MAP_KEY");
        // Value "name"
        columnNames2.add("MAP_VALUE_NAME");
        // Value "value"
        columnNames2.add("MAP_VALUE_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_MAP_EMB_VALUE_OVERRIDE", columnNames2);
        // Map with embedded key taking default key column names
        Set<String> columnNames3 = new HashSet<String>();
        // FK to owner
        columnNames3.add("JPA_MAP_EMB_OWNER_ID");
        // Key "name"
        columnNames3.add("NAME");
        // Key "value"
        columnNames3.add("VALUE");
        // Value
        columnNames3.add("MAPEMBEDDEDKEY_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_MAP_EMB_KEY", columnNames3);
        // Map with embedded key overriding the key column names
        Set<String> columnNames4 = new HashSet<String>();
        // FK to owner
        columnNames4.add("JPA_MAP_EMB_OWNER_ID");
        // Key "name"
        columnNames4.add("MAP_KEY_NAME");
        // Key "value"
        columnNames4.add("MAP_KEY_VALUE");
        // Value
        columnNames4.add("MAPEMBEDDEDKEYOVERRIDE_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_MAP_EMB_KEY_OVERRIDE", columnNames4);
    } catch (Exception e) {
        LOG.error("Exception thrown", e);
        fail("Exception thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Aggregations

ManagedConnection (org.datanucleus.store.connection.ManagedConnection)157 SQLException (java.sql.SQLException)125 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)80 Connection (java.sql.Connection)75 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)74 PreparedStatement (java.sql.PreparedStatement)70 ExecutionContext (org.datanucleus.ExecutionContext)64 SQLController (org.datanucleus.store.rdbms.SQLController)63 HashSet (java.util.HashSet)62 DatabaseMetaData (java.sql.DatabaseMetaData)58 PersistenceManager (javax.jdo.PersistenceManager)46 Transaction (javax.jdo.Transaction)46 JDOFatalUserException (javax.jdo.JDOFatalUserException)45 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)45 ResultSet (java.sql.ResultSet)37 JDOFatalInternalException (javax.jdo.JDOFatalInternalException)24 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOUserException (javax.jdo.JDOUserException)21 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21 EntityTransaction (javax.persistence.EntityTransaction)19