Search in sources :

Example 6 with Transaction

use of org.datanucleus.Transaction in project datanucleus-rdbms by datanucleus.

the class MapKeySetStore method iterator.

/**
 * Accessor for an iterator for the set.
 * @param ownerOP ObjectProvider for the set.
 * @return Iterator for the set.
 */
public Iterator<K> iterator(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    if (iteratorStmtLocked == null) {
        synchronized (// Make sure this completes in case another thread needs the same info
        this) {
            // Generate the statement, and statement mapping/parameter information
            SQLStatement sqlStmt = getSQLStatementForIterator(ownerOP);
            iteratorStmtUnlocked = sqlStmt.getSQLText().toSQL();
            sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
            iteratorStmtLocked = sqlStmt.getSQLText().toSQL();
        }
    }
    Transaction tx = ec.getTransaction();
    String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? iteratorStmtLocked : iteratorStmtUnlocked);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement and set the owner
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            StatementMappingIndex ownerIdx = iteratorMappingParams.getMappingForParameter("owner");
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    ResultObjectFactory rof = null;
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new CollectionStoreIterator(ownerOP, rs, null, this);
                    }
                    rof = new PersistentClassROF(ec, rs, false, iteratorMappingDef, elementCmd, clr.classForName(elementType));
                    return new CollectionStoreIterator(ownerOP, rs, rof, this);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    } catch (MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Transaction(org.datanucleus.Transaction) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 7 with Transaction

use of org.datanucleus.Transaction in project datanucleus-api-jdo by datanucleus.

the class Hollow method transitionRetrieve.

/**
 * Method to transition to retrieve state.
 * @param op ObjectProvider.
 * @param fetchPlan the fetch plan to load fields
 * @return new LifeCycle state.
 */
public LifeCycleState transitionRetrieve(ObjectProvider op, FetchPlan fetchPlan) {
    op.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
    Transaction tx = op.getExecutionContext().getTransaction();
    if (!tx.getOptimistic() && tx.isActive()) {
        return changeState(op, P_CLEAN);
    } else if (tx.getOptimistic()) {
        return changeState(op, P_NONTRANS);
    }
    return super.transitionRetrieve(op, fetchPlan);
}
Also used : Transaction(org.datanucleus.Transaction)

Example 8 with Transaction

use of org.datanucleus.Transaction in project datanucleus-api-jdo by datanucleus.

the class PersistentClean method transitionRefresh.

/**
 * Method to transition to refresh state.
 * @param op ObjectProvider.
 * @return new LifeCycle state.
 */
public LifeCycleState transitionRefresh(ObjectProvider op) {
    op.clearSavedFields();
    // Refresh the FetchPlan fields and unload all others
    op.refreshFieldsInFetchPlan();
    op.unloadNonFetchPlanFields();
    Transaction tx = op.getExecutionContext().getTransaction();
    if (tx.isActive()) {
        return changeState(op, P_CLEAN);
    }
    return changeState(op, P_NONTRANS);
}
Also used : Transaction(org.datanucleus.Transaction)

Example 9 with Transaction

use of org.datanucleus.Transaction in project datanucleus-rdbms by datanucleus.

the class MapValueCollectionStore method iterator.

/**
 * Accessor for an iterator for the set.
 * @param ownerOP ObjectProvider for the set.
 * @return Iterator for the set.
 */
public Iterator<V> iterator(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    if (iteratorStmtLocked == null) {
        synchronized (// Make sure this completes in case another thread needs the same info
        this) {
            // Generate the statement, and statement mapping/parameter information
            SQLStatement sqlStmt = getSQLStatementForIterator(ownerOP);
            iteratorStmtUnlocked = sqlStmt.getSQLText().toSQL();
            sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
            iteratorStmtLocked = sqlStmt.getSQLText().toSQL();
        }
    }
    Transaction tx = ec.getTransaction();
    String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? iteratorStmtLocked : iteratorStmtUnlocked);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement and set the owner
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            StatementMappingIndex ownerIdx = iteratorMappingParams.getMappingForParameter("owner");
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    ResultObjectFactory rof = null;
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new CollectionStoreIterator(ownerOP, rs, null, this);
                    }
                    rof = new PersistentClassROF(ec, rs, false, iteratorMappingDef, elementCmd, clr.classForName(elementType));
                    return new CollectionStoreIterator(ownerOP, rs, rof, this);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    } catch (MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Transaction(org.datanucleus.Transaction) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 10 with Transaction

use of org.datanucleus.Transaction in project datanucleus-api-jdo by datanucleus.

the class PersistentNontransactional method transitionRetrieve.

/**
 * Method to transition to retrieve state.
 * @param op ObjectProvider.
 * @param fetchPlan the fetch plan to load fields
 * @return new LifeCycle state.
 */
public LifeCycleState transitionRetrieve(ObjectProvider op, FetchPlan fetchPlan) {
    Transaction tx = op.getExecutionContext().getTransaction();
    if (tx.isActive() && !tx.getOptimistic()) {
        // Save the fields for rollback.
        op.saveFields();
        op.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
        return changeState(op, P_CLEAN);
    } else if (tx.isActive() && tx.getOptimistic()) {
        // Save the fields for rollback.
        // TODO this is wrong... saving all the time, retrieve is asked... side effects besides performance?
        op.saveFields();
        op.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
        return this;
    } else {
        op.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
        return this;
    }
}
Also used : Transaction(org.datanucleus.Transaction)

Aggregations

Transaction (org.datanucleus.Transaction)13 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 SQLException (java.sql.SQLException)7 ExecutionContext (org.datanucleus.ExecutionContext)7 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)7 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)7 SQLController (org.datanucleus.store.rdbms.SQLController)7 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)7 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)6 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)6 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)5 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)5 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)3 NoSuchElementException (java.util.NoSuchElementException)2 ObjectProvider (org.datanucleus.state.ObjectProvider)2 EmbeddedKeyPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)2 SerialisedPCMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping)2 SerialisedReferenceMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping)2 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)2