Search in sources :

Example 1 with XATransactionController

use of org.apache.derby.iapi.store.access.XATransactionController in project derby by apache.

the class EmbedConnection method xa_prepare.

/*
	** methods to be overridden by subimplementations wishing to insert
	** their classes into the mix.
	** The reason we need to override them is because we want to create a
	** Local20/LocalStatment object (etc) rather than a Local/LocalStatment
	** object (etc).
	*/
/*
	** XA support
	*/
/**
 * Do not use this method directly use XATransactionState.xa_prepare
 * instead because it also maintains/cancels the timeout task which is
 * scheduled to cancel/rollback the global transaction.
 *
 * @return One of {@link org.apache.derby.iapi.store.access.XATransactionController#XA_OK} or
 *         {@link org.apache.derby.iapi.store.access.XATransactionController#XA_RDONLY}
 * @throws java.sql.SQLException
 */
public final int xa_prepare() throws SQLException {
    synchronized (getConnectionSynchronization()) {
        setupContextStack();
        try {
            LanguageConnectionContext lcc = privilegedGetLCC();
            XATransactionController tc = (XATransactionController) lcc.getTransactionExecute();
            try {
                lcc.checkIntegrity();
            } catch (StandardException e) {
                lcc.xaRollback();
                throw e;
            }
            int ret = tc.xa_prepare();
            if (ret == XATransactionController.XA_RDONLY) {
                // On a prepare call, xa allows an optimization that if the
                // transaction is read only, the RM can just go ahead and
                // commit it.  So if store returns this read only status -
                // meaning store has taken the liberty to commit already - we
                // needs to turn around and call internalCommit (without
                // committing the store again) to make sure the state is
                // consistent.  Since the transaction is read only, there is
                // probably not much that needs to be done.
                lcc.internalCommit(false);
            }
            InterruptStatus.restoreIntrFlagIfSeen(lcc);
            return ret;
        } catch (StandardException t) {
            throw handleException(t);
        } finally {
            restoreContextStack();
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) XATransactionController(org.apache.derby.iapi.store.access.XATransactionController) Savepoint(java.sql.Savepoint)

Example 2 with XATransactionController

use of org.apache.derby.iapi.store.access.XATransactionController in project derby by apache.

the class GenericLanguageConnectionContext method doCommit.

/**
 * This is where the work on internalCommit(), userCOmmit() and
 * internalCommitNoSync() actually takes place.
 * <p>
 * When a commit happens, the language connection context
 * will close all open activations/cursors and commit the
 * Store transaction.
 * <p>
 * REVISIT: we talked about having a LanguageTransactionContext,
 * but since store transaction management is currently in flux
 * and our context might want to delegate to that context,
 * for now all commit/rollback actions are handled directly by
 * the language connection context.
 * REVISIT: this may need additional alterations when
 * RELEASE SAVEPOINT/ROLLBACK TO SAVEPOINT show up.
 * <P>
 * Since the access manager's own context takes care of its own
 * resources on commit, and the transaction stays open, there is
 * nothing that this context has to do with the transaction controller.
 * <p>
 * Also, tell the data dictionary that the transaction is finished,
 * if necessary (that is, if the data dictionary was put into
 * DDL mode in this transaction.
 *
 * @param   commitStore true if we should commit the Store transaction
 * @param   sync        true means do a synchronized commit,
 *                      false means do an unsynchronized commit
 * @param   commitflag  if this is an unsynchronized commit, the flags to
 *                      pass to commitNoSync in the store's
 *                      TransactionController.  If this is a synchronized
 *                      commit, this flag is overloaded for xacommit.
 * @param   requestedByUser    False iff the commit is for internal use and
 *                      we should ignore the check to prevent commits
 *                      in an atomic statement.
 *
 * @exception StandardException     Thrown on error
 */
protected void doCommit(boolean commitStore, boolean sync, int commitflag, boolean requestedByUser) throws StandardException {
    StatementContext statementContext = getStatementContext();
    if (requestedByUser && (statementContext != null) && statementContext.inUse() && statementContext.isAtomic()) {
        throw StandardException.newException(SQLState.LANG_NO_COMMIT_IN_NESTED_CONNECTION);
    }
    checkIntegrity();
    // Log commit to error log, if appropriate
    if (logStatementText) {
        if (istream == null) {
            istream = Monitor.getStream();
        }
        String xactId = tran.getTransactionIdString();
        istream.printlnWithHeader(LanguageConnectionContext.xidStr + xactId + "), " + LanguageConnectionContext.lccStr + instanceNumber + "), " + LanguageConnectionContext.dbnameStr + dbname + "), " + LanguageConnectionContext.drdaStr + drdaID + "), Committing");
    }
    endTransactionActivationHandling(false);
    // Do clean up work required for temporary tables at commit time.
    if (allDeclaredGlobalTempTables != null) {
        tempTablesAndCommit(commitflag != NON_XA);
    }
    // reset the current savepoint level for the connection to 0 at the end
    // of commit work for temp tables
    currentSavepointLevel = 0;
    // which could be rolled back in case of a system crash.
    if (sync) {
        finishDDTransaction();
    }
    // before a commit.
    if (SanityManager.DEBUG) {
        if (readOnlyNestedTransaction != null) {
            SanityManager.THROWASSERT("Nested transaction active!");
        }
    }
    // now commit the Store transaction
    TransactionController tc = getTransactionExecute();
    if (tc != null && commitStore) {
        if (sync) {
            if (commitflag == NON_XA) {
                // regular commit
                tc.commit();
            } else {
                if (SanityManager.DEBUG)
                    SanityManager.ASSERT(commitflag == XA_ONE_PHASE || commitflag == XA_TWO_PHASE, "invalid commit flag");
                ((XATransactionController) tc).xa_commit(commitflag == XA_ONE_PHASE);
            }
        } else {
            tc.commitNoSync(commitflag);
        }
        // reset the savepoints to the new
        // location, since any outer nesting
        // levels expect there to be a savepoint
        resetSavepoints();
        // Do post commit XA temp table cleanup if necessary.
        if ((allDeclaredGlobalTempTables != null) && (commitflag != NON_XA)) {
            tempTablesXApostCommit();
        }
    }
}
Also used : XATransactionController(org.apache.derby.iapi.store.access.XATransactionController) TransactionController(org.apache.derby.iapi.store.access.TransactionController) XATransactionController(org.apache.derby.iapi.store.access.XATransactionController) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Example 3 with XATransactionController

use of org.apache.derby.iapi.store.access.XATransactionController in project derby by apache.

the class GenericLanguageConnectionContext method doRollback.

/**
 * When a rollback happens, the language connection context
 * will close all open activations and invalidate
 * their prepared statements. Then the language will abort the
 * Store transaction.
 * <p>
 * The invalidated statements can revalidate themselves without
 * a full recompile if they verify their dependencies' providers still
 * exist unchanged. REVISIT when invalidation types are created.
 * <p>
 * REVISIT: this may need additional alterations when
 * RELEASE SAVEPOINT/ROLLBACK TO SAVEPOINT show up.
 * <p>
 * Also, tell the data dictionary that the transaction is finished,
 * if necessary (that is, if the data dictionary was put into
 * DDL mode in this transaction.
 *
 * @param xa    true if this is an xa rollback
 * @param requestedByUser   true if requested by user
 *
 * @exception StandardException thrown on failure
 */
private void doRollback(boolean xa, boolean requestedByUser) throws StandardException {
    StatementContext statementContext = getStatementContext();
    if (requestedByUser && (statementContext != null) && statementContext.inUse() && statementContext.isAtomic()) {
        throw StandardException.newException(SQLState.LANG_NO_ROLLBACK_IN_NESTED_CONNECTION);
    }
    clearDeferreds();
    // Log rollback to error log, if appropriate
    if (logStatementText) {
        if (istream == null) {
            istream = Monitor.getStream();
        }
        String xactId = tran.getTransactionIdString();
        istream.printlnWithHeader(LanguageConnectionContext.xidStr + xactId + "), " + LanguageConnectionContext.lccStr + instanceNumber + "), " + LanguageConnectionContext.dbnameStr + dbname + "), " + LanguageConnectionContext.drdaStr + drdaID + "), Rolling back");
    }
    endTransactionActivationHandling(true);
    // reset the current savepoint level for the connection to 0 at the beginning of rollback work for temp tables
    currentSavepointLevel = 0;
    if (allDeclaredGlobalTempTables != null)
        tempTablesAndRollback();
    finishDDTransaction();
    // with the user transaction.
    if (readOnlyNestedTransaction != null) {
        readOnlyNestedTransaction.destroy();
        readOnlyNestedTransaction = null;
        queryNestingDepth = 0;
    }
    // now rollback the Store transaction
    TransactionController tc = getTransactionExecute();
    if (tc != null) {
        if (xa)
            ((XATransactionController) tc).xa_rollback();
        else
            tc.abort();
        // reset the savepoints to the new
        // location, since any outer nesting
        // levels expet there to be a savepoint
        resetSavepoints();
    }
}
Also used : TransactionController(org.apache.derby.iapi.store.access.TransactionController) XATransactionController(org.apache.derby.iapi.store.access.XATransactionController) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Aggregations

XATransactionController (org.apache.derby.iapi.store.access.XATransactionController)3 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)2 TransactionController (org.apache.derby.iapi.store.access.TransactionController)2 Savepoint (java.sql.Savepoint)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 StandardException (org.apache.derby.shared.common.error.StandardException)1