Search in sources :

Example 31 with ContextManager

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

the class commit_method method rollback.

public void rollback(int format_id, byte[] global_id, byte[] branch_id, XATransactionController xa_tc) throws StandardException {
    if (SanityManager.DEBUG)
        SanityManager.ASSERT((global_id != null) || (xa_tc != null));
    boolean local_online_xact = online_xact;
    if (global_id == null)
        local_online_xact = true;
    if (xa_tc == null)
        local_online_xact = false;
    if (local_online_xact) {
        xa_tc.xa_rollback();
    } else {
        Xid xid = new XAXactId(format_id, global_id, branch_id);
        ContextManager cm = ((XAResourceManager) store.getXAResourceManager()).find(xid);
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(cm != null, "could not find xid = " + xid);
            SanityManager.ASSERT(cm == T_XA.getContextService().getCurrentContextManager(), "cm = " + cm + "current = " + T_XA.getContextService().getCurrentContextManager());
        }
        ((XAResourceManager) store.getXAResourceManager()).rollback(cm, xid);
    }
}
Also used : Xid(javax.transaction.xa.Xid) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 32 with ContextManager

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

the class RemoveFile method add.

/**
 *	  @see FileResource#add
 *	  @exception StandardException Oops
 */
public long add(String name, InputStream source) throws StandardException {
    OutputStream os = null;
    if (factory.isReadOnly()) {
        throw StandardException.newException(SQLState.FILE_READ_ONLY);
    }
    long generationId = factory.getNextId();
    try {
        StorageFile file = getAsFile(name, generationId);
        if (file.exists()) {
            throw StandardException.newException(SQLState.FILE_EXISTS, file);
        }
        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 added.
        tran.blockBackup(true);
        StorageFile directory = file.getParentDir();
        StorageFile parentDir = directory.getParentDir();
        boolean pdExisted = parentDir.exists();
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                throw StandardException.newException(SQLState.FILE_CANNOT_CREATE_SEGMENT, directory);
            }
            directory.limitAccessToOwner();
            if (!pdExisted) {
                parentDir.limitAccessToOwner();
            }
        }
        os = file.getOutputStream();
        byte[] data = new byte[4096];
        int len;
        factory.writeInProgress();
        try {
            while ((len = source.read(data)) != -1) {
                os.write(data, 0, len);
            }
            factory.writableStorageFactory.sync(os, false);
        } finally {
            factory.writeFinished();
        }
    } catch (IOException ioe) {
        throw StandardException.newException(SQLState.FILE_UNEXPECTED_EXCEPTION, ioe);
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException ioe2) {
        /*RESOLVE: Why ignore this?*/
        }
        try {
            if (source != null)
                source.close();
        } catch (IOException ioe2) {
        /* RESOLVE: Why ignore this?*/
        }
    }
    return generationId;
}
Also used : OutputStream(java.io.OutputStream) ContextManager(org.apache.derby.iapi.services.context.ContextManager) StorageFile(org.apache.derby.io.StorageFile) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) IOException(java.io.IOException)

Example 33 with ContextManager

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

the class RemoveFile method removeJarDir.

/**
 * @see FileResource#removeJarDir
 */
public void removeJarDir(String f) 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);
    StorageFile ff = factory.storageFactory.newStorageFile(f);
    Serviceable s = new RemoveFile(ff);
    // Since this code is only used during upgrade to post-10.8 databases
    // we do no bother to build code for a special RemoveDirOperation and
    // do tran.logAndDo (cf. logic in #remove). If the post-commit removal
    // doesn't get completed, that is no big issue, the dirs can be removed
    // by hand if need be. A prudent DBA will rerun the upgrade from a
    // backup if something crashes anyway..
    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) StorageFile(org.apache.derby.io.StorageFile)

Example 34 with ContextManager

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

the class XactFactory method handlePreparedXacts.

/**
 *        Run through all prepared transactions known to this factory
 *        and restore their state such that they remain after recovery, and
 *        can be found and handled by a XA transaction manager.  This includes
 *        creating a context manager for each, pushing a xact context, and
 *        reclaiming update locks on all data changed by the transaction.
 *
 *        Expected to be called just after the redo and undo recovery loops,
 *        where the transaction table should be empty except for prepared
 *        xacts.
 *
 *		Used only in recovery.
 *
 *		@exception StandardException Derby Standard Error policy
 */
public void handlePreparedXacts(RawStoreFactory rsf) throws StandardException {
    if (SanityManager.DEBUG) {
        if (rawStoreFactory != null)
            SanityManager.ASSERT(rawStoreFactory == rsf, "raw store factory different");
    }
    int prepared_count = 0;
    if (ttab.hasPreparedRecoveredXact()) {
        while (true) {
            // allocate new context and associate new xact with it.
            ContextManager cm = contextFactory.newContextManager();
            contextFactory.setCurrentContextManager(cm);
            try {
                RawTransaction rawtran = startTransaction(rawStoreFactory, cm, AccessFactoryGlobals.USER_TRANS_NAME);
                if (ttab.getMostRecentPreparedRecoveredXact(rawtran)) {
                    // found a prepared xact.  The reprepare() call will
                    // accumulate locks, and change the transaction table entry
                    // to not be "in-recovery" so that it won't show up again.
                    rawtran.reprepare();
                    if (SanityManager.DEBUG)
                        prepared_count++;
                } else {
                    // get rid of last transaction allocated.
                    rawtran.destroy();
                    break;
                }
            } finally {
                contextFactory.resetCurrentContextManager(cm);
            }
        }
    }
    if (SanityManager.DEBUG) {
    // RESOLVE - need to only do this under a debug flag.
    // SanityManager.DEBUG_PRINT("",
    // "Recovery re-prepared " + prepared_count + " xa transactions.");
    }
}
Also used : ContextManager(org.apache.derby.iapi.services.context.ContextManager) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction)

Example 35 with ContextManager

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

the class BasicUnitTestManager method runATest.

private boolean runATest(UnitTest aTest) {
    boolean result;
    String thisTestName = aTest.getClass().getName();
    Date startTime = null, endTime;
    // push a new context manager
    ContextManager cm = null;
    if (contextService != null) {
        cm = contextService.newContextManager();
        contextService.setCurrentContextManager(cm);
    }
    if (performanceReportOn)
        startTime = new Date();
    try {
        emitAMessage("Starting test  '" + thisTestName + "'.");
        result = aTest.Execute(currentOutput);
        if (result == true)
            emitAMessage("Test '" + thisTestName + "' passed");
        else
            emitAMessage("Test '" + thisTestName + "' failed");
    } catch (Throwable t) {
        if (t instanceof ThreadDeath) {
            t.printStackTrace(output.getPrintWriter());
            Runtime.getRuntime().exit(1);
        }
        result = false;
        String msg = t.getMessage();
        if (msg == null)
            msg = t.getClass().getName();
        emitAMessage("Test '" + thisTestName + "' failed with exception '" + msg + "'.");
        t.printStackTrace(output.getPrintWriter());
    } finally {
        if (contextService != null) {
            // 
            // Assure the completed test does not stick around
            // cm.cleanupOnError
            // (BasicUnitTestDatabaseException.cleanUp());
            contextService.resetCurrentContextManager(cm);
        }
    }
    if (performanceReportOn) {
        endTime = new Date();
        emitAMessage("Test '" + thisTestName + "' took " + (endTime.getTime() - startTime.getTime()) + " milliseconds.");
    }
    return result;
}
Also used : ContextManager(org.apache.derby.iapi.services.context.ContextManager) Date(java.util.Date)

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