Search in sources :

Example 56 with ContextManager

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

the class SPSDescriptor method compileStatement.

/**
 * Compiles this SPS.
 * <p>
 * <em>Note:</em> This SPS may still be marked as invalid after this method
 * has completed, because an invalidation request may have been received
 * while compiling.
 *
 * @param lcc connection
 * @param triggerTable subject table (may be {@code null})
 * @param tc transaction controller to use (may be {@code null})
 * @throws StandardException if something fails
 */
// @GuardedBy("this")
private void compileStatement(LanguageConnectionContext lcc, TableDescriptor triggerTable, TransactionController tc) throws StandardException {
    ContextManager cm = lcc.getContextManager();
    LanguageConnectionFactory lcf = lcc.getLanguageConnectionFactory();
    DataDictionary dd = getDataDictionary();
    /*
		** If we are a trigger, then we have to go ahead
		** and locate the trigger's table descriptor and
		** push it on the lcc.  This is expensive, but
		** pretty atypical since trigger actions aren't
		** likely to be invalidated too often.  Also, when
		** possible, we already have the triggerTable.
		*/
    if (type == SPS_TYPE_TRIGGER && triggerTable == null) {
        // 49 because name consists of (see CreateTriggerConstantAction):
        // TRIGGER<ACTN|WHEN>_<UUID:36>_<UUID:36>
        String uuidStr = name.substring(49);
        triggerTable = dd.getTableDescriptor(recreateUUID(uuidStr));
        if (SanityManager.DEBUG) {
            if (triggerTable == null) {
                SanityManager.THROWASSERT("couldn't find trigger table for trigger sps " + name);
            }
        }
    }
    if (triggerTable != null) {
        lcc.pushTriggerTable(triggerTable);
    }
    // stored statements always stored as unicode.
    Statement stmt = lcf.getStatement(dd.getSchemaDescriptor(compSchemaId, null), text, true);
    try {
        preparedStatement = (ExecPreparedStatement) stmt.prepareStorable(lcc, preparedStatement, getParameterDefaults(), getSchemaDescriptor(), type == SPS_TYPE_TRIGGER);
    } finally {
        if (triggerTable != null) {
            lcc.popTriggerTable(triggerTable);
        }
    }
    // when the query is getting compiled.
    if (preparedStatement.referencesSessionSchema())
        throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
    setCompileTime();
    setParams(preparedStatement.getParameterTypes());
    if (!dd.isReadOnlyUpgrade()) {
        /*
			** Indicate that we are going to write the data
			** dictionary.  We have probably already done this
			** but it is ok to call startWriting more than once.
			*/
        dd.startWriting(lcc);
        DependencyManager dm = dd.getDependencyManager();
        /*
			** Clear out all the dependencies that exist
			** before we recreate them so we don't grow
			** SYS.SYSDEPENDS forever.
			*/
        dm.clearDependencies(lcc, this, tc);
        /*
			** Copy over all the dependencies to me
			*/
        // from
        dm.copyDependencies(// from
        preparedStatement, // to
        this, // persistent only
        false, cm, tc);
        // between this sps and the trigger table DERBY-5120
        if (triggerTable != null)
            dm.addDependency(this, triggerTable, lcc.getContextManager());
    }
    // mark it as valid
    valid = true;
}
Also used : LanguageConnectionFactory(org.apache.derby.iapi.sql.conn.LanguageConnectionFactory) Statement(org.apache.derby.iapi.sql.Statement) StorablePreparedStatement(org.apache.derby.iapi.sql.StorablePreparedStatement) ExecPreparedStatement(org.apache.derby.iapi.sql.execute.ExecPreparedStatement) ContextManager(org.apache.derby.iapi.services.context.ContextManager) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager)

Example 57 with ContextManager

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

the class BasicDaemon method serviceClient.

protected void serviceClient(ServiceRecord clientRecord) {
    clientRecord.serviced();
    Serviceable client = clientRecord.client;
    // client may have unsubscribed while it had items queued
    if (client == null)
        return;
    ContextManager cm = contextMgr;
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(cm != null, "Context manager is null");
        SanityManager.ASSERT(client != null, "client is null");
    }
    try {
        int status = client.performWork(cm);
        if (clientRecord.subscriber)
            return;
        if (status == Serviceable.REQUEUE) {
            List<ServiceRecord> queue = client.serviceASAP() ? highPQ : normPQ;
            synchronized (this) {
                queue.add(clientRecord);
                if (SanityManager.DEBUG) {
                    if (SanityManager.DEBUG_ON("memoryLeakTrace")) {
                        if (queue.size() > (OPTIMAL_QUEUE_SIZE * 2))
                            System.out.println("memoryLeakTrace:BasicDaemon " + queue.size());
                    }
                }
            }
        }
        return;
    } catch (Throwable e) {
        if (SanityManager.DEBUG)
            SanityManager.showTrace(e);
        // Assume database is not active. DERBY-4856 thread dump
        cm.cleanupOnError(e, false);
    }
}
Also used : Serviceable(org.apache.derby.iapi.services.daemon.Serviceable) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 58 with ContextManager

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

the class EmbedXAResource method prepare.

/**
 * Ask the resource manager to prepare for a transaction commit of the
 * transaction specified in xid.
 *
 * @param xid A global transaction identifier
 *
 * @return A value indicating the resource manager's vote on the outcome
 * of the transaction. The possible values are: XA_RDONLY or XA_OK. If the
 * resource manager wants to roll back the transaction, it should do so by
 * raising an appropriate XAException in the prepare method.
 *
 * @exception XAException An error has occurred. Possible exception values
 * are: XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or
 * XAER_PROTO.
 */
public final synchronized int prepare(Xid xid) throws XAException {
    checkXAActive();
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    XATransactionState tranState = getTransactionState(xid_im);
    if (tranState == null) {
        XAResourceManager rm = ra.getXAResourceManager();
        ContextManager inDoubtCM = rm.find(xid);
        // RM also does not know about this xid.
        if (inDoubtCM == null)
            throw new XAException(XAException.XAER_NOTA);
        // cannot prepare in doubt transactions
        throw new XAException(XAException.XAER_PROTO);
    }
    synchronized (tranState) {
        checkUserCredentials(tranState.creatingResource);
        // any XAResource.
        switch(tranState.associationState) {
            case XATransactionState.T0_NOT_ASSOCIATED:
                break;
            case XATransactionState.TRO_FAIL:
                throw new XAException(tranState.rollbackOnlyCode);
            default:
                throw new XAException(XAException.XAER_PROTO);
        }
        if (tranState.suspendedList != null && tranState.suspendedList.size() != 0)
            throw new XAException(XAException.XAER_PROTO);
        if (tranState.isPrepared)
            throw new XAException(XAException.XAER_PROTO);
        try {
            int ret = tranState.xa_prepare();
            if (ret == XATransactionController.XA_OK) {
                tranState.isPrepared = true;
                return XAResource.XA_OK;
            } else {
                returnConnectionToResource(tranState, xid_im);
                if (SanityManager.DEBUG) {
                    if (con.realConnection != null) {
                        SanityManager.ASSERT(con.realConnection.transactionIsIdle(), "real connection should have been idle." + "tranState = " + tranState + "ret = " + ret + "con.realConnection = " + con.realConnection);
                    }
                }
                return XAResource.XA_RDONLY;
            }
        } catch (SQLException sqle) {
            XAException xe = wrapInXAException(sqle);
            if (ExceptionUtil.isDeferredConstraintViolation(sqle.getSQLState())) {
                // We are rolling back
                returnConnectionToResource(tranState, xid_im);
            }
            throw xe;
        }
    }
}
Also used : XAXactId(org.apache.derby.iapi.store.access.xa.XAXactId) XAException(javax.transaction.xa.XAException) XAResourceManager(org.apache.derby.iapi.store.access.xa.XAResourceManager) SQLException(java.sql.SQLException) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 59 with ContextManager

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

the class EmbedXAResource method commit.

/**
 * Commit the global transaction specified by xid.
 * @param xid A global transaction identifier
 * @param onePhase If true, the resource manager should use a one-phase
 * commit protocol to commit the work done on behalf of xid.
 *
 * @exception XAException An error has occurred. Possible XAExceptions are
 * XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
 * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
 * <P>If the resource manager did not commit the transaction and
 * the paramether onePhase is set to true, the resource manager
 * may throw one of the XA_RB* exceptions. Upon return, the
 * resource manager has rolled back the branch's work and has
 * released all held resources.
 */
public final synchronized void commit(Xid xid, boolean onePhase) throws XAException {
    checkXAActive();
    // ensure immtable and correct equals method.
    XAXactId xid_im = new XAXactId(xid);
    XATransactionState tranState = getTransactionState(xid_im);
    if (tranState == null) {
        XAResourceManager rm = ra.getXAResourceManager();
        ContextManager inDoubtCM = rm.find(xid);
        // RM also does not know about this xid.
        if (inDoubtCM == null)
            throw new XAException(XAException.XAER_NOTA);
        ContextService csf = getContextService();
        csf.setCurrentContextManager(inDoubtCM);
        try {
            rm.commit(inDoubtCM, xid_im, onePhase);
            // close the connection/transaction since it can never
            // be used again. DERBY-4856 No extended diagnostic information needed.
            inDoubtCM.cleanupOnError(StandardException.closeException(), false);
            return;
        } catch (StandardException se) {
            // The rm threw an exception, clean it up in the approprate
            // context.  There is no transactionResource to handle the
            // exception for us.
            inDoubtCM.cleanupOnError(se, con.isActive());
            throw wrapInXAException(se);
        } finally {
            csf.resetCurrentContextManager(inDoubtCM);
        }
    }
    synchronized (tranState) {
        checkUserCredentials(tranState.creatingResource);
        // any XAResource.
        switch(tranState.associationState) {
            case XATransactionState.T0_NOT_ASSOCIATED:
                break;
            case XATransactionState.TRO_FAIL:
                throw new XAException(tranState.rollbackOnlyCode);
            default:
                throw new XAException(XAException.XAER_PROTO);
        }
        if (tranState.suspendedList != null && tranState.suspendedList.size() != 0)
            throw new XAException(XAException.XAER_PROTO);
        if (tranState.isPrepared == onePhase)
            throw new XAException(XAException.XAER_PROTO);
        try {
            tranState.xa_commit(onePhase);
        } catch (SQLException sqle) {
            throw wrapInXAException(sqle);
        } finally {
            returnConnectionToResource(tranState, xid_im);
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) XAXactId(org.apache.derby.iapi.store.access.xa.XAXactId) ContextService(org.apache.derby.iapi.services.context.ContextService) XAException(javax.transaction.xa.XAException) XAResourceManager(org.apache.derby.iapi.store.access.xa.XAResourceManager) SQLException(java.sql.SQLException) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 60 with ContextManager

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

the class RAMTransaction method getInternalTransaction.

/**
 * Get an Internal transaction.
 * <p>
 * Start an internal transaction.  An internal transaction is a completely
 * separate transaction from the current user transaction.  All work done
 * in the internal transaction must be physical (ie. it can be undone
 * physically by the rawstore at the page level, rather than logically
 * undone like btree insert/delete operations).  The rawstore guarantee's
 * that in the case of a system failure all open Internal transactions are
 * first undone in reverse order, and then other transactions are undone
 * in reverse order.
 * <p>
 * Internal transactions are meant to implement operations which, if
 * interupted before completion will cause logical operations like tree
 * searches to fail.  This special undo order insures that the state of
 * the tree is restored to a consistent state before any logical undo
 * operation which may need to search the tree is performed.
 * <p>
 *
 * @return The new internal transaction.
 *
 * @exception  StandardException  Standard exception policy.
 */
public TransactionManager getInternalTransaction() throws StandardException {
    // Get the context manager.
    ContextManager cm = getContextManager();
    // Allocate a new transaction no matter what.
    // Create a transaction, make a context for it, and push the context.
    // Note this puts the raw store transaction context
    // above the access context, which is required for
    // error handling assumptions to be correct.
    Transaction rawtran = accessmanager.getRawStore().startInternalTransaction(cm);
    RAMTransaction rt = new RAMTransaction(accessmanager, rawtran, null);
    RAMTransactionContext rtc = new RAMTransactionContext(cm, AccessFactoryGlobals.RAMXACT_INTERNAL_CONTEXT_ID, rt, true);
    rawtran.setDefaultLockingPolicy(accessmanager.getDefaultLockingPolicy());
    return (rt);
}
Also used : Transaction(org.apache.derby.iapi.store.raw.Transaction) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

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