Search in sources :

Example 36 with ContextManager

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

the class RemoveFile method remove.

/**
 *	  @see FileResource#remove
 *	  @exception StandardException Oops
 */
public void remove(String name, long currentGenerationId) throws StandardException {
    if (factory.isReadOnly())
        throw StandardException.newException(SQLState.FILE_READ_ONLY);
    ContextManager cm = FileContainer.getContextService().getCurrentContextManager();
    RawTransaction tran = factory.getRawStoreFactory().getXactFactory().findUserTransaction(factory.getRawStoreFactory(), cm, AccessFactoryGlobals.USER_TRANS_NAME);
    // Block the backup, If backup is already in progress wait
    // for the backup to finish. Jar files are unlogged but the
    // changes to the  references to the jar file in the catalogs
    // is logged. A consistent backup can not be made when jar file
    // is being removed.
    tran.blockBackup(true);
    tran.logAndDo(new RemoveFileOperation(name, currentGenerationId, true));
    Serviceable s = new RemoveFile(getAsFile(name, currentGenerationId));
    tran.addPostCommitWork(s);
}
Also used : Serviceable(org.apache.derby.iapi.services.daemon.Serviceable) ContextManager(org.apache.derby.iapi.services.context.ContextManager) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction)

Example 37 with ContextManager

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

the class T_RawStoreFactory method P054.

/**
 *		Test internal transaction
 *
 *		@exception T_Fail Unexpected behaviour from the API
 *		@exception StandardException Unexpected exception from the implementation
 */
protected void P054() throws StandardException, T_Fail {
    if (!testRollback)
        return;
    ContextManager previousCM = contextService.getCurrentContextManager();
    ContextManager cm1 = contextService.newContextManager();
    contextService.setCurrentContextManager(cm1);
    Transaction tuser = t_util.t_startTransaction();
    Transaction tinternal = null;
    try {
        long cid1 = t_util.t_addContainer(tuser, 0);
        ContainerHandle c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        Page p1 = t_util.t_addPage(c1);
        t_util.t_commit(tuser);
        // insert a row using user transaction
        T_RawStoreRow row = new T_RawStoreRow(REC_001);
        c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        p1 = t_util.t_getLastPage(c1);
        RecordHandle r1 = t_util.t_insert(p1, row);
        REPORT("starting internal transaction");
        tinternal = t_util.t_startInternalTransaction();
        long cid2 = t_util.t_addContainer(tinternal, 0);
        ContainerHandle c2 = t_util.t_openContainer(tinternal, 0, cid2, true);
        Page p2 = t_util.t_addPage(c2);
        RecordHandle r2 = t_util.t_insert(p2, row);
        // commit internal transaction
        tinternal.commit();
        // this will close the container and release
        tinternal.abort();
        // the page
        tinternal.close();
        tinternal = null;
        REPORT("commit internal transaction");
        // abort user transaction
        t_util.t_abort(tuser);
        REPORT("rollback user transaction");
        c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        p1 = t_util.t_getPage(c1, r1.getPageNumber());
        if (p1.recordExists(r1, false))
            throw T_Fail.testFailMsg("user transaction failed to rollback");
        c2 = t_util.t_openContainer(tuser, 0, cid2, true);
        // this should be unaffected by the
        t_util.t_checkFetch(c2, r2, REC_001);
        // user transaction rollback
        p2 = t_util.t_getLastPage(c2);
        T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
        if (!p2.spaceForInsert()) {
            REPORT("P054 not run, page cannot accomodate 2 rows");
            return;
        }
        RecordHandle r21 = t_util.t_insert(p2, row2);
        // throw an exception, make sure everything is aborted.
        tinternal = t_util.t_startInternalTransaction();
        long cid3 = t_util.t_addContainer(tinternal, 0);
        ContainerHandle c3 = t_util.t_openContainer(tinternal, 0, cid3, true);
        Page p3 = t_util.t_addPage(c3);
        RecordHandle r3 = t_util.t_insert(p3, row);
        try {
            // this will throw a data statement exception
            t_util.t_insertAtSlot(p3, 100, row);
        } catch (StandardException se) {
            REPORT("cleanup on error");
            // Assume database is not active. DERBY-4856 thread dump
            cm1.cleanupOnError(se, false);
            REPORT("done cleanup on error");
        }
        tinternal = null;
        // tuser = t_util.t_startTransaction();
        c2 = t_util.t_openContainer(tuser, 0, cid2, true);
        t_util.t_checkFetch(c2, r2, REC_001);
        p2 = t_util.t_getPage(c2, r21.getPageNumber());
        if (p2.recordExists(r21, false))
            throw T_Fail.testFailMsg("expect user transaction to rollback");
        // this should fail
        ContainerKey id3 = new ContainerKey(0, cid3);
        c3 = tuser.openContainer(id3, ContainerHandle.MODE_READONLY);
        if (c3 != null)
            throw T_Fail.testFailMsg("expect internal transaction to rollback");
        LockingPolicy nolock = tuser.newLockingPolicy(LockingPolicy.MODE_NONE, 0, false);
        RawContainerHandle stub = ((RawTransaction) tuser).openDroppedContainer(id3, nolock);
        if (stub == null)
            throw T_Fail.testFailMsg("expect container to be dropped");
        if (stub.getContainerStatus() != RawContainerHandle.COMMITTED_DROP)
            throw T_Fail.testFailMsg("expect container to be committed dropped");
        // this should fail
        p3 = stub.getPage(r3.getPageNumber());
        if (p3 != null)
            throw T_Fail.testFailMsg("should not getpage with committed dropped container");
        PASS("P054");
        // cleanup
        t_util.t_dropContainer(tuser, 0, cid2);
        // cleanup
        t_util.t_dropContainer(tuser, 0, cid1);
        if (tinternal != null) {
            t_util.t_abort(tinternal);
            tinternal.close();
        }
        if (tuser != null) {
            t_util.t_commit(tuser);
            tuser.close();
        }
    } finally {
        contextService.resetCurrentContextManager(cm1);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) ContextManager(org.apache.derby.iapi.services.context.ContextManager) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle)

Example 38 with ContextManager

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

the class T_RawStoreFactory method CP001.

/**
 *		Test checkpoint
 *		@exception T_Fail Unexpected behaviour from the API
 *		@exception StandardException Unexpected exception from the implementation
 */
protected void CP001() throws StandardException, T_Fail {
    if (!testRollback)
        return;
    ContextManager previousCM = contextService.getCurrentContextManager();
    Transaction longtran = null;
    ContextManager cm1 = null;
    Transaction t1 = null;
    ContextManager cm2 = null;
    Transaction t2 = null;
    // ContextManager cpm = null; // reserved for the checkpoint transaction
    try {
        T_RawStoreRow row1 = new T_RawStoreRow(REC_001);
        T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
        T_RawStoreRow row3 = new T_RawStoreRow(REC_003);
        T_RawStoreRow row4 = new T_RawStoreRow(REC_004);
        T_RawStoreRow row5 = new T_RawStoreRow(REC_005);
        // start a long running transaction that spans multiple checkpoints
        // make sure it can be rolled back a the end
        longtran = t_util.t_startTransaction();
        long cid = t_util.t_addContainer(longtran, 0);
        ContainerHandle c = t_util.t_openContainer(longtran, 0, cid, true);
        RecordHandle r1 = t_util.t_insert(c, row1);
        RecordHandle r2 = t_util.t_insert(c, row2);
        t_util.t_commit(longtran);
        c = t_util.t_openContainer(longtran, 0, cid, true);
        Page p2 = t_util.t_getPage(c, r2.getPageNumber());
        int slot2 = p2.getSlotNumber(r2);
        p2.updateAtSlot(slot2, row5.getRow(), null);
        p2.unlatch();
        // a bunch of short running transactions that criss cross the
        // checkpoints
        cm1 = contextService.newContextManager();
        contextService.setCurrentContextManager(cm1);
        t1 = t_util.t_startTransaction();
        long cid1 = t_util.t_addContainer(t1, 0);
        ContainerHandle c1 = t_util.t_openContainer(t1, 0, cid1, true);
        RecordHandle r3 = t_util.t_insert(c1, row3);
        RecordHandle r4 = t_util.t_insert(c1, row4);
        contextService.resetCurrentContextManager(cm1);
        cm2 = contextService.newContextManager();
        contextService.setCurrentContextManager(cm2);
        t2 = t_util.t_startTransaction();
        long cid2 = t_util.t_addContainer(t2, 0);
        ContainerHandle c2 = t_util.t_openContainer(t2, 0, cid2, true);
        RecordHandle r5 = t_util.t_insert(c2, row1);
        t_util.t_commit(t2);
        c2 = t_util.t_openContainer(t2, 0, cid2, true);
        Page p5 = t_util.t_getPage(c2, r5.getPageNumber());
        int slot5 = p5.getSlotNumber(r5);
        p5.updateAtSlot(slot5, row5.getRow(), null);
        p5.unlatch();
        // cpm = contextService.newContextManager();
        // contextService.setCurrentContextManager(cpm);
        factory.checkpoint();
        contextService.resetCurrentContextManager(cm2);
        // make sure checkpoint did not destroy any data
        contextService.setCurrentContextManager(previousCM);
        t_util.t_checkFetch(c, r1, REC_001);
        t_util.t_checkFetch(c, r2, REC_005);
        contextService.resetCurrentContextManager(previousCM);
        contextService.setCurrentContextManager(cm1);
        t_util.t_checkFetch(c1, r3, REC_003);
        t_util.t_checkFetch(c1, r4, REC_004);
        contextService.resetCurrentContextManager(cm1);
        contextService.setCurrentContextManager(cm2);
        t_util.t_checkFetch(c2, r5, REC_005);
        // two consecutive checkpoints
        // contextService.setCurrentContextManager(cpm);
        factory.checkpoint();
        contextService.resetCurrentContextManager(cm2);
        // we can insert some more
        contextService.setCurrentContextManager(previousCM);
        Page page = t_util.t_addPage(c);
        RecordHandle r6 = t_util.t_insertAtSlot(page, 0, row1, Page.INSERT_UNDO_WITH_PURGE);
        page.unlatch();
        contextService.resetCurrentContextManager(previousCM);
        // commit/abort everything except the long running transaction
        contextService.setCurrentContextManager(cm1);
        t_util.t_commit(t1);
        contextService.resetCurrentContextManager(cm1);
        contextService.setCurrentContextManager(cm2);
        t_util.t_abort(t2);
        contextService.resetCurrentContextManager(cm2);
        contextService.setCurrentContextManager(previousCM);
        t_util.t_checkFetch(c, r1, REC_001);
        t_util.t_checkFetch(c, r2, REC_005);
        t_util.t_checkFetch(c, r6, REC_001);
        contextService.resetCurrentContextManager(previousCM);
        contextService.setCurrentContextManager(cm1);
        c1 = t_util.t_openContainer(t1, 0, cid1, true);
        t_util.t_checkFetch(c1, r3, REC_003);
        t_util.t_checkFetch(c1, r4, REC_004);
        contextService.resetCurrentContextManager(cm1);
        contextService.setCurrentContextManager(cm2);
        c2 = t_util.t_openContainer(t2, 0, cid2, true);
        t_util.t_checkFetch(c2, r5, REC_001);
        // checkpoint again
        // contextService.setCurrentContextManager(cpm);
        factory.checkpoint();
        contextService.resetCurrentContextManager(cm2);
        contextService.setCurrentContextManager(previousCM);
        t_util.t_abort(longtran);
        c = t_util.t_openContainer(longtran, 0, cid, true);
        t_util.t_checkFetch(c, r1, REC_001);
        t_util.t_checkFetch(c, r2, REC_002);
        Page p6 = t_util.t_getPage(c, r6.getPageNumber());
        t_util.t_checkEmptyPage(p6);
        p6.unlatch();
        // cleanup
        t_util.t_dropContainer(longtran, 0, cid);
        contextService.resetCurrentContextManager(previousCM);
        contextService.setCurrentContextManager(cm1);
        t_util.t_checkFetch(c1, r3, REC_003);
        t_util.t_checkFetch(c1, r4, REC_004);
        t_util.t_dropContainer(t1, 0, cid1);
        contextService.resetCurrentContextManager(cm1);
        contextService.setCurrentContextManager(cm2);
        t_util.t_checkFetch(c2, r5, REC_001);
        t_util.t_dropContainer(t2, 0, cid2);
        // checkpoint again
        // contextService.setCurrentContextManager(cpm);
        factory.checkpoint();
        contextService.resetCurrentContextManager(cm2);
        PASS("CP001");
    } catch (Throwable t) {
        t.printStackTrace(System.err);
        if (cm1 != null)
            // Assume database is not active. DERBY-4856 thread dump
            cm1.cleanupOnError(t, false);
        if (cm2 != null)
            cm2.cleanupOnError(t, false);
    // if (cpm != null)
    // cpm.cleanupOnError(t, false);
    } finally {
        if (t2 != null) {
            contextService.setCurrentContextManager(cm2);
            t_util.t_commit(t2);
            t2.close();
            contextService.resetCurrentContextManager(cm2);
        }
        if (t1 != null) {
            contextService.setCurrentContextManager(cm1);
            t_util.t_commit(t1);
            t1.close();
            contextService.resetCurrentContextManager(cm1);
        }
        if (longtran != null) {
            contextService.setCurrentContextManager(previousCM);
            t_util.t_commit(longtran);
            longtran.close();
            contextService.resetCurrentContextManager(previousCM);
        }
    // DJD contextService.setCurrentContextManager(previousCM);
    }
}
Also used : RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) ContextManager(org.apache.derby.iapi.services.context.ContextManager) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle)

Example 39 with ContextManager

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

the class T_AccessFactory method runTests.

/**
 *		@exception T_Fail Unexpected behaviour from the API
 */
protected void runTests() throws T_Fail {
    TransactionController tc = null;
    boolean pass = false;
    // don't automatic boot this service if it gets left around
    if (startParams == null) {
        startParams = new Properties();
    }
    startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
    // remove the service directory to ensure a clean run
    startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString());
    // see if we are testing encryption
    startParams = T_Util.setEncryptionParam(startParams);
    try {
        store = (AccessFactory) createPersistentService(getModuleToTestProtocolName(), testService, startParams);
    } catch (StandardException mse) {
        throw T_Fail.exceptionFail(mse);
    }
    if (store == null) {
        throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started.");
    }
    REPORT("(unitTestMain) Testing " + testService);
    try {
        ContextManager cm = getContextService().getCurrentContextManager();
        tc = store.getAndNameTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME);
        if (dropTest(tc) && holdCursor(tc) && readUncommitted(tc) && updatelocks(tc) && nestedUserTransaction(tc) && positionAtRowLocation(tc) && sortCost(tc) && storeCost(tc) && partialScan(tc) && scanInfo(tc) && insertAndUpdateExample(tc) && insertAndFetchExample(tc) && scanExample(tc) && alterTable(tc, false) && alterTable(tc, true) && tempTest(tc) && getTableProperties(tc) && insert_bench(tc) && transactionalProperties(tc) && commitTest(tc)) {
            pass = true;
        }
        // Make sure commitNoSync gets executed sometimes.
        tc.commitNoSync(TransactionController.RELEASE_LOCKS);
        tc.destroy();
        if (!pass)
            throw T_Fail.testFailMsg("test failed");
    } catch (StandardException e) {
        String msg = e.getMessage();
        if (msg == null)
            msg = e.getClass().getName();
        REPORT(msg);
        e.printStackTrace();
        throw T_Fail.exceptionFail(e);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextManager(org.apache.derby.iapi.services.context.ContextManager) Properties(java.util.Properties)

Example 40 with ContextManager

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

the class T_AccessFactory method commitTest.

// test various flavors of commit
protected boolean commitTest(TransactionController tc) throws StandardException, T_Fail {
    REPORT("(commitTest)");
    // 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();
    // Open it.
    ConglomerateController cc = tc.openConglomerate(conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
    // Create a row.
    T_AccessRow r1 = new T_AccessRow(1);
    SQLInteger c1 = new SQLInteger(0);
    r1.setCol(0, c1);
    // Get a location template
    RowLocation rowloc = cc.newRowLocationTemplate();
    // Insert the row and remember its location.
    cc.insertAndFetchLocation(r1.getRowArray(), rowloc);
    // now commit nosync without releasing the row lock
    tc.commitNoSync(TransactionController.KEEP_LOCKS);
    // cc should be closed
    try {
        cc.newRowLocationTemplate();
        throw T_Fail.testFailMsg("conglomerate controller is not closed after commit");
    } catch (StandardException se) {
    // expect fail
    }
    // get another transaction going
    ContextManager cm2 = getContextService().newContextManager();
    getContextService().setCurrentContextManager(cm2);
    TransactionController tc2 = null;
    ConglomerateController cc2 = null;
    try {
        tc2 = store.getTransaction(cm2);
        cc2 = tc2.openConglomerate(conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE);
        // try to lock the row, it should fail
        // Mikem RESOLVE: this does not get a row lock
        // cc2.fetch(rowloc, r1.getRowArray(), (FormatableBitSet)null)
        cc2.delete(rowloc);
        throw T_Fail.testFailMsg("expected time out did not happen");
    } catch (StandardException lfe) {
        if (!lfe.getMessageId().equals(SQLState.LOCK_TIMEOUT))
            throw lfe;
    } finally {
        getContextService().resetCurrentContextManager(cm2);
    }
    // see whether anyone is blocked at all
    if (tc.anyoneBlocked()) {
        throw T_Fail.testFailMsg("No transactions should be blocked");
    }
    // now really commit the transaction
    tc.commit();
    getContextService().setCurrentContextManager(cm2);
    try {
        cc2.fetch(rowloc, r1.getRowArray(), (FormatableBitSet) null);
        // get rid of the other transaction
        tc2.commitNoSync(TransactionController.RELEASE_LOCKS);
        tc2.destroy();
    } finally {
        getContextService().resetCurrentContextManager(cm2);
    }
    REPORT("(commitTest) succeeded");
    return true;
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextManager(org.apache.derby.iapi.services.context.ContextManager) RowLocation(org.apache.derby.iapi.types.RowLocation) SQLInteger(org.apache.derby.iapi.types.SQLInteger)

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