Search in sources :

Example 46 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class commit_method method XATest_6.

/**
 * Very simple testing of changing a local transaction to a global.
 * <p>
 * @exception  StandardException  Standard exception policy.
 */
void XATest_6(commit_method commit_method) throws StandardException, T_Fail {
    REPORT("(XATest_5) starting");
    ContextManager cm = getContextService().getCurrentContextManager();
    TransactionController tc = store.getTransaction(cm);
    // Create a heap conglomerate.
    T_AccessRow template_row = new T_AccessRow(1);
    long conglomid = tc.createConglomerate(// create a heap conglomerate
    "heap", // 1 column template.
    template_row.getRowArray(), // column sort order - not required for heap
    null, // default collation
    null, // default properties
    null, // not temporary
    TransactionController.IS_DEFAULT);
    tc.commit();
    // COMMIT AN IDLE TRANSACTION.
    // Start a global transaction
    XATransactionController xa_tc = (XATransactionController) tc.createXATransactionFromLocalTransaction(// fake format id
    42, global_id, branch_id);
    if (!xa_tc.isGlobal()) {
        throw T_Fail.testFailMsg("should be a global transaction.");
    }
    // Open a scan on the conglomerate.
    ScanController scan1 = xa_tc.openScan(conglomid, // don't hold
    false, // not for update
    0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
    (FormatableBitSet) null, // start position - first row in conglomerate
    null, // unused if start position is null.
    0, // qualifier - accept all rows
    null, // stop position - last row in conglomerate
    null, // unused if stop position is null.
    0);
    scan1.next();
    scan1.close();
    // Create a heap conglomerate.
    template_row = new T_AccessRow(1);
    conglomid = xa_tc.createConglomerate(// create a heap conglomerate
    "heap", // 1 column template.
    template_row.getRowArray(), // column sort order - not required for heap
    null, // default collation
    null, // default properties
    null, // not temporary
    TransactionController.IS_DEFAULT);
    // Should be no prepared transactions, there is one update global xact.
    if (((XAResourceManager) store.getXAResourceManager()).recover(XAResource.TMSTARTRSCAN).length != 0) {
        throw T_Fail.testFailMsg("recover incorrectly returned prepared xacts.");
    }
    // prepare the update xact.
    if (xa_tc.xa_prepare() != XATransactionController.XA_OK) {
        throw T_Fail.testFailMsg("prepare of update xact did not return XA_OK.");
    }
    try {
        // Open a scan on the conglomerate.
        scan1 = xa_tc.openScan(conglomid, // don't hold
        false, // not for update
        0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, // all columns, all as objects
        (FormatableBitSet) null, // start position - first row in conglomerate
        null, // unused if start position is null.
        0, // qualifier - accept all rows
        null, // stop position - last row in conglomerate
        null, // unused if stop position is null.
        0);
        scan1.next();
        scan1.close();
        throw T_Fail.testFailMsg("Should not be able to do anything on xact after prepare.");
    } catch (StandardException se) {
    // expected exception, fall through.
    }
    // commit a prepared transaction - using two phase.
    commit_method.commit(false, 42, global_id, branch_id, xa_tc);
    xa_tc.destroy();
    REPORT("(XATest_6) finishing");
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextManager(org.apache.derby.iapi.services.context.ContextManager) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet)

Example 47 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class T_CreateConglomRet method runTestSet.

/**
 * Driver routine for the btree secondary index tests.
 * <p>
 *
 * @exception  T_Fail  Throws T_Fail on any test failure.
 */
protected void runTestSet() throws T_Fail {
    AccessFactory store = null;
    TransactionController tc = null;
    boolean pass = false;
    out.println("executing b2i test");
    store = (AccessFactory) store_module;
    if (store == null) {
        throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started.");
    }
    ContextManager cm1 = contextService.newContextManager();
    contextService.setCurrentContextManager(cm1);
    REPORT("(unitTestMain) Testing " + testService);
    try {
        tc = store.getTransaction(cm1);
        pass = true;
        if (t_005(tc) && t_001(tc) && t_003(tc) && t_004(tc) && t_005(tc) && t_006(tc) && t_009(tc) && t_010(tc) && t_011(tc) && t_012(tc) && t_013(tc) && t_014(tc) && t_017(tc) && t_018(tc) && t_019(tc) && t_020(tc) && t_021(tc)) {
            pass = true;
            if (SanityManager.DEBUG) {
                pass = false;
                if (t_002(tc) && t_007(tc) && t_008(tc) && t_015(tc) && t_016(tc))
                    pass = true;
            }
        }
        tc.commit();
        tc.destroy();
    } catch (StandardException e) {
        String msg = e.getMessage();
        if (msg == null)
            msg = e.getClass().getName();
        REPORT(msg);
        e.printStackTrace(out.getPrintWriter());
        cm1.cleanupOnError(e, isdbActive());
        pass = false;
    } catch (Throwable t) {
        String msg = t.getMessage();
        if (msg == null)
            msg = t.getClass().getName();
        REPORT(msg);
        t.printStackTrace(out.getPrintWriter());
        cm1.cleanupOnError(t, isdbActive());
        pass = false;
    } finally {
        contextService.resetCurrentContextManager(cm1);
    }
    if (!pass)
        throw T_Fail.testFailMsg("");
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextManager(org.apache.derby.iapi.services.context.ContextManager) TransactionController(org.apache.derby.iapi.store.access.TransactionController) AccessFactory(org.apache.derby.iapi.store.access.AccessFactory)

Example 48 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class T_CreateConglomRet method t_002.

/**
 * Test backout during critical times of splits.
 * <p>
 * Use trace points to force errors in split at critical points:
 *     leaf_split_abort{1,2,3,4}
 *
 * @exception  StandardException  Standard exception policy.
 * @exception  T_Fail  Throws T_Fail on any test failure.
 */
protected boolean t_002(TransactionController tc) throws StandardException, T_Fail {
    ScanController scan = null;
    // SanityManager.DEBUG_SET("LockTrace");
    REPORT("Starting t_002");
    T_CreateConglomRet create_ret = new T_CreateConglomRet();
    // Create the btree so that it only allows 2 rows per page.
    createCongloms(tc, 2, false, false, 2, create_ret);
    // Open the base table
    ConglomerateController base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // Open the secondary index
    ConglomerateController index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    if (!(index_cc instanceof B2IController)) {
        throw T_Fail.testFailMsg("openConglomerate returned wrong type");
    }
    index_cc.checkConsistency();
    // Create a row and insert into base table, remembering it's location.
    DataValueDescriptor[] r1 = TemplateRow.newU8Row(2);
    T_SecondaryIndexRow index_row1 = new T_SecondaryIndexRow();
    RowLocation base_rowloc1 = base_cc.newRowLocationTemplate();
    index_row1.init(r1, base_rowloc1, 3);
    // Commit the create of the tables so that the following aborts don't
    // undo that work.
    tc.commit();
    // Now try aborts of transactions during splits, using magic
    // trace flags.  This test inserts enough rows to cause a split
    // and then forces the split to fail at various key points.  The
    // split should be backed out and also the rows before the split.
    // The test makes sure that there are some inserts before the forced
    // split abort.
    String[] debug_strings = { "leaf_split_growRoot1", "leaf_split_growRoot2", "leaf_split_growRoot3", "leaf_split_growRoot4", "leaf_split_growRoot5", "leaf_split_abort1", "leaf_split_abort2", "leaf_split_abort3", "leaf_split_abort4", "branch_split_abort1", "branch_split_abort2", "branch_split_abort3", "branch_split_abort4", "BTreeController_doIns2" };
    for (int errs = 0; errs < debug_strings.length; errs++) {
        REPORT("Doing abort test: " + debug_strings[errs]);
        if (SanityManager.DEBUG)
            SanityManager.DEBUG_SET(debug_strings[errs]);
        try {
            // Open the base table
            base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
            // Open the secondary index
            index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
            // insert one row that does not cause failure.
            ((SQLLongint) r1[0]).setValue(2);
            ((SQLLongint) r1[1]).setValue(10000 + errs);
            // Insert the row into the base table;remember its location.
            base_cc.insertAndFetchLocation(r1, base_rowloc1);
            // Insert the row into the secondary index.
            if (index_cc.insert(index_row1.getRow()) != 0)
                throw T_Fail.testFailMsg("insert failed");
            // language to make sure error handling really works.
            if (SanityManager.DEBUG)
                SanityManager.DEBUG_SET(debug_strings[errs]);
            // now insert enough rows to cause failure
            for (int i = 100; i > 0; i -= 2) {
                ((SQLLongint) r1[0]).setValue(2);
                ((SQLLongint) r1[1]).setValue(i);
                // Insert the row into the base table;remember its location.
                base_cc.insertAndFetchLocation(r1, base_rowloc1);
                // Insert the row into the secondary index.
                if (index_cc.insert(index_row1.getRow()) != 0) {
                    throw T_Fail.testFailMsg("insert failed");
                }
            }
            throw T_Fail.testFailMsg("debug flag (" + debug_strings[errs] + ")did not cause exception.");
        } catch (StandardException e) {
            ContextService contextFactory = getContextService();
            // Get the context manager.
            ContextManager cm = contextFactory.getCurrentContextManager();
            if (SanityManager.DEBUG)
                SanityManager.ASSERT(cm != null);
            cm.cleanupOnError(e, isdbActive());
            // RESOLVE (mikem) - when split abort works come up with
            // a good sanity check here.
            // 
            // index check - there should be no records:
            scan = tc.openScan(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, null, ScanController.NA, null, null, ScanController.NA);
            index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
            index_cc.checkConsistency();
            index_cc.close();
            if (scan.next()) {
                throw T_Fail.testFailMsg("t_002: there are still rows in table.");
            }
            scan.close();
        }
        // Unset the flag.
        if (SanityManager.DEBUG)
            SanityManager.DEBUG_CLEAR(debug_strings[errs]);
    }
    // Try a simple abort.  The following adds enough rows to cause a
    // split.  The result of the split should be a tree with no rows, but
    // the splits will not be undone.  It is up to the implementation
    // whether the undo's cause shrinks in the tree.  In the initial
    // implementation it won't.
    {
        tc.commit();
        // Open the base table
        base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
        // Open the secondary index
        index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
        // Make sure that normal abort leaves the committed split.
        for (int i = 0; i < 3; i++) {
            ((SQLLongint) r1[0]).setValue(2);
            ((SQLLongint) r1[1]).setValue(i);
            // Insert the row into the base table;remember its location.
            base_cc.insertAndFetchLocation(r1, base_rowloc1);
            // Insert the row into the secondary index.
            if (index_cc.insert(index_row1.getRow()) != 0)
                throw T_Fail.testFailMsg("insert failed");
        }
        tc.abort();
        // index check - there should be no records left.
        ScanController empty_scan = tc.openScan(create_ret.index_conglomid, false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, null, ScanController.NA, null, null, ScanController.NA);
        if (empty_scan.next()) {
            throw T_Fail.testFailMsg("t_002: there are still rows in table.");
        }
    }
    tc.commit();
    REPORT("Ending t_002");
    return true;
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) ContextService(org.apache.derby.iapi.services.context.ContextService) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) SQLLongint(org.apache.derby.iapi.types.SQLLongint) StandardException(org.apache.derby.shared.common.error.StandardException) SQLLongint(org.apache.derby.iapi.types.SQLLongint) ContextManager(org.apache.derby.iapi.services.context.ContextManager) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 49 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class T_CreateConglomRet method t_016.

/**
 * Test deadlocks during critical times of row level locking.
 * <p>
 * Use trace points to force errors in split at critical points:
 *     leaf_split_abort{1,2,3,4}
 *
 * @exception  StandardException  Standard exception policy.
 * @exception  T_Fail  Throws T_Fail on any test failure.
 */
protected boolean t_016(TransactionController tc) throws StandardException, T_Fail {
    ScanController scan = null;
    // SanityManager.DEBUG_SET("LockTrace");
    REPORT("Starting t_016");
    T_CreateConglomRet create_ret = new T_CreateConglomRet();
    // Create the btree so that it only allows 2 rows per page.
    createCongloms(tc, 2, false, false, 2, create_ret);
    // Open the base table
    ConglomerateController base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // Open the secondary index
    ConglomerateController index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    if (!(index_cc instanceof B2IController)) {
        throw T_Fail.testFailMsg("openConglomerate returned wrong type");
    }
    index_cc.checkConsistency();
    // Create a row and insert into base table, remembering it's location.
    DataValueDescriptor[] r1 = TemplateRow.newU8Row(2);
    T_SecondaryIndexRow index_row1 = new T_SecondaryIndexRow();
    RowLocation base_rowloc1 = base_cc.newRowLocationTemplate();
    index_row1.init(r1, base_rowloc1, 3);
    // Commit the create of the tables so that the following aborts don't
    // undo that work.
    tc.commit();
    // Open the base table
    base_cc = tc.openConglomerate(create_ret.base_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // Open the secondary index
    index_cc = tc.openConglomerate(create_ret.index_conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // now insert enough rows to cause failure
    for (int i = 100; i > 0; i -= 2) {
        ((SQLLongint) r1[0]).setValue(2);
        ((SQLLongint) r1[1]).setValue(i);
        // Insert the row into the base table;remember its location.
        base_cc.insertAndFetchLocation(r1, base_rowloc1);
        // Insert the row into the secondary index.
        if (index_cc.insert(index_row1.getRow()) != 0) {
            throw T_Fail.testFailMsg("insert failed");
        }
    }
    tc.abort();
    // Now try simulated deadlocks
    // RESOLVE (Mikem) - test out aborts and errors during inserts.
    String[] deadlock_debug_strings = { "B2iRowLocking3_1_lockScanRow2", "B2iRowLocking3_2_lockScanRow2", // "BTreeController_doIns2",
    "BTreeScan_positionAtStartPosition2", // "BTreeScan_reposition2",
    "BTreeScan_fetchNextGroup2" };
    for (int errs = 0; errs < deadlock_debug_strings.length; errs++) {
        try {
            REPORT("Doing deadlock tests: " + deadlock_debug_strings[errs]);
            // latch release path through the code.
            if (SanityManager.DEBUG)
                SanityManager.DEBUG_SET(deadlock_debug_strings[errs]);
            // Just scan the rows and make sure you see them all, mostly just
            // a test to make sure no errors are thrown by the latch release
            // code paths.
            scan = tc.openScan(create_ret.index_conglomid, false, 0, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, null, ScanController.NA, null, null, ScanController.NA);
            int row_count = 0;
            while (scan.next()) {
                row_count++;
            }
            scan.close();
            throw T_Fail.testFailMsg("expected deadlock");
        } catch (StandardException e) {
            if (!e.getMessageId().equals(SQLState.DEADLOCK))
                throw e;
            ContextService contextFactory = getContextService();
            // Get the context manager.
            ContextManager cm = contextFactory.getCurrentContextManager();
            if (SanityManager.DEBUG)
                SanityManager.ASSERT(cm != null);
            cm.cleanupOnError(e, isdbActive());
        }
    }
    tc.commit();
    REPORT("Ending t_016");
    return true;
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) ContextService(org.apache.derby.iapi.services.context.ContextService) ConglomerateController(org.apache.derby.iapi.store.access.ConglomerateController) SQLLongint(org.apache.derby.iapi.types.SQLLongint) StandardException(org.apache.derby.shared.common.error.StandardException) SQLLongint(org.apache.derby.iapi.types.SQLLongint) ContextManager(org.apache.derby.iapi.services.context.ContextManager) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

Example 50 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class SequenceUpdater method updateCurrentValueOnDisk.

// /////////////////////////////////////////////////////////////////////////////////
// 
// DISK WRITING MINIONS
// 
// /////////////////////////////////////////////////////////////////////////////////
/**
 * <p>
 * Update the value on disk. Does its work in a subtransaction of the user's
 * execution transaction. If that fails, raises a TOO MUCH CONTENTION exception.
 * </p>
 *
 * @return Returns true if the value was successfully updated, false if we lost a race with another session.
 */
public synchronized boolean updateCurrentValueOnDisk(Long oldValue, Long newValue) throws StandardException {
    LanguageConnectionContext lcc = getLCC();
    // 
    if (lcc == null) {
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(oldValue == null, "We should be flushing unused sequence values here.");
        }
        ContextService csf = getContextService();
        ContextManager cm = csf.getCurrentContextManager();
        AccessFactory af = _dd.af;
        TransactionController dummyTransaction = af.getTransaction(cm);
        boolean retval = updateCurrentValueOnDisk(dummyTransaction, oldValue, newValue, false);
        dummyTransaction.commit();
        dummyTransaction.destroy();
        return retval;
    }
    TransactionController executionTransaction = lcc.getTransactionExecute();
    TransactionController nestedTransaction = executionTransaction.startNestedUserTransaction(false, true);
    if (nestedTransaction != null) {
        boolean retval = false;
        boolean escalateToParentTransaction = false;
        try {
            retval = updateCurrentValueOnDisk(nestedTransaction, oldValue, newValue, false);
        } catch (StandardException se) {
            if (!se.isLockTimeout()) {
                if (se.isSelfDeadlock()) {
                    // We're blocked by a lock held by our parent transaction.
                    // Escalate into the parent transaction now. See DERBY-6554.
                    escalateToParentTransaction = true;
                } else {
                    Monitor.logThrowable(se);
                    throw se;
                }
            }
        } finally {
            // DERBY-5494, if this commit does not flush log then an
            // unorderly shutdown could lose the update.  Do not use
            // commitNoSync(), and store needs to flush user nested update
            // transaction commits by default.
            nestedTransaction.commit();
            nestedTransaction.destroy();
            if (escalateToParentTransaction) {
                retval = updateCurrentValueOnDisk(executionTransaction, oldValue, newValue, false);
            }
            return retval;
        }
    }
    throw tooMuchContentionException();
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextService(org.apache.derby.iapi.services.context.ContextService) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ContextManager(org.apache.derby.iapi.services.context.ContextManager) TransactionController(org.apache.derby.iapi.store.access.TransactionController) AccessFactory(org.apache.derby.iapi.store.access.AccessFactory)

Aggregations

ContextManager (org.apache.derby.iapi.services.context.ContextManager)61 StandardException (org.apache.derby.shared.common.error.StandardException)22 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)8 ContextService (org.apache.derby.iapi.services.context.ContextService)7 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)6 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)5 TransactionController (org.apache.derby.iapi.store.access.TransactionController)5 XAException (javax.transaction.xa.XAException)4 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)4 XAResourceManager (org.apache.derby.iapi.store.access.xa.XAResourceManager)4 XAXactId (org.apache.derby.iapi.store.access.xa.XAXactId)4 SQLException (java.sql.SQLException)3 Xid (javax.transaction.xa.Xid)3 Serviceable (org.apache.derby.iapi.services.daemon.Serviceable)3 ExecPreparedStatement (org.apache.derby.iapi.sql.execute.ExecPreparedStatement)3 RowLocation (org.apache.derby.iapi.types.RowLocation)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2