Search in sources :

Example 6 with TransactionContext

use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.

the class TestTransactionServer method testRequestCancel.

@Test
public void testRequestCancel() throws Exception {
    TransactionContext tc = server.getOrCreateTransactionContext(THREAD1);
    server.begin(tc);
    server.cancelTransactions(THREAD1, true);
    Mockito.verify(txn).setRollbackOnly();
}
Also used : TransactionContext(org.teiid.dqp.service.TransactionContext) Test(org.junit.Test)

Example 7 with TransactionContext

use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.

the class ForEachRowPlan method nextBatch.

@Override
public TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (planContext != null) {
        this.getContext().getTransactionServer().resume(planContext);
    }
    try {
        while (true) {
            if (currentTuple == null) {
                if (nextTuple != null) {
                    currentTuple = nextTuple;
                } else if (!nextNull) {
                    currentTuple = tupleSource.nextTuple();
                }
                if (currentTuple == null) {
                    if (this.planContext != null) {
                        TransactionService ts = this.getContext().getTransactionServer();
                        ts.commit(this.planContext);
                        this.planContext = null;
                    }
                    TupleBatch result = new TupleBatch(1, new List[] { Arrays.asList((int) Math.min(Integer.MAX_VALUE, updateCount)) });
                    result.setTerminationFlag(true);
                    return result;
                }
            }
            if (first) {
                TransactionContext tc = this.getContext().getTransactionContext();
                if (this.planContext == null && tc != null && tc.getTransactionType() == Scope.NONE) {
                    Boolean txnRequired = rowProcedure.requiresTransaction(false);
                    boolean start = false;
                    if (txnRequired == null) {
                        nextTuple = tupleSource.nextTuple();
                        if (nextTuple != null) {
                            start = true;
                        } else {
                            nextNull = true;
                        }
                    } else if (Boolean.TRUE.equals(txnRequired)) {
                        start = true;
                    }
                    if (start) {
                        this.getContext().getTransactionServer().begin(tc);
                        this.planContext = tc;
                    }
                }
                first = false;
            }
            if (this.rowProcessor == null) {
                rowProcedure.reset();
                CommandContext context = getContext().clone();
                this.rowProcessor = new QueryProcessor(rowProcedure, context, this.bufferMgr, this.dataMgr);
                Evaluator eval = new Evaluator(lookupMap, dataMgr, context);
                for (Map.Entry<ElementSymbol, Expression> entry : this.params.entrySet()) {
                    Integer index = this.lookupMap.get(entry.getValue());
                    if (index != null) {
                        rowProcedure.getCurrentVariableContext().setValue(entry.getKey(), this.currentTuple.get(index));
                    } else {
                        rowProcedure.getCurrentVariableContext().setValue(entry.getKey(), eval.evaluate(entry.getValue(), this.currentTuple));
                    }
                }
            }
            // save the variable context to get the key information
            VariableContext vc = rowProcedure.getCurrentVariableContext();
            // just getting the next batch is enough
            this.rowProcessor.nextBatch();
            if (insert) {
                assignGeneratedKey(vc);
            }
            this.rowProcessor.closeProcessing();
            this.rowProcessor = null;
            this.currentTuple = null;
            this.updateCount++;
        }
    } finally {
        if (planContext != null) {
            this.getContext().getTransactionServer().suspend(planContext);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TransactionService(org.teiid.dqp.service.TransactionService) CommandContext(org.teiid.query.util.CommandContext) Evaluator(org.teiid.query.eval.Evaluator) VariableContext(org.teiid.query.sql.util.VariableContext) QueryProcessor(org.teiid.query.processor.QueryProcessor) Expression(org.teiid.query.sql.symbol.Expression) TransactionContext(org.teiid.dqp.service.TransactionContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 8 with TransactionContext

use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.

the class ProcedurePlan method push.

public void push(Program program) throws XATransactionException {
    this.evaluator.close();
    program.reset(this.getContext().getConnectionId());
    program.setTrappingExceptions(program.getExceptionGroup() != null || (!this.programs.isEmpty() && this.programs.peek().isTrappingExceptions()));
    TempTableStore tts = getTempTableStore();
    getContext().setTempTableStore(program.getTempTableStore());
    program.getTempTableStore().setParentTempTableStore(tts);
    this.programs.push(program);
    VariableContext context = new VariableContext(true);
    context.setParentContext(this.currentVarContext);
    this.currentVarContext = context;
    VariableContext cc = new VariableContext(true);
    cc.setParentContext(this.cursorStates);
    this.cursorStates = cc;
    if (program.isAtomic() && this.blockContext == null) {
        TransactionContext tc = this.getContext().getTransactionContext();
        if (tc != null && tc.getTransactionType() == Scope.NONE && Boolean.TRUE.equals(program.instructionsRequireTransaction(false))) {
            // start a transaction
            this.getContext().getTransactionServer().begin(tc);
            this.blockContext = tc;
            program.setStartedTxn(true);
        }
    }
}
Also used : TempTableStore(org.teiid.query.tempdata.TempTableStore) TransactionContext(org.teiid.dqp.service.TransactionContext) VariableContext(org.teiid.query.sql.util.VariableContext)

Example 9 with TransactionContext

use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.

the class BatchedUpdatePlan method openPlan.

private void openPlan() throws TeiidComponentException, TeiidProcessingException {
    // reset prior to updating the context
    updatePlans[planIndex].reset();
    if (this.contexts != null && !this.contexts.isEmpty()) {
        CommandContext context = updatePlans[planIndex].getContext();
        VariableContext vc = context.getVariableContext();
        // this is just a safe guard against the plan not correctly resetting the context
        while (vc.getParentContext() != null) {
            vc = vc.getParentContext();
        }
        vc.clear();
        VariableContext currentValues = this.contexts.get(planIndex);
        vc.putAll(currentValues);
    }
    TransactionContext tc = this.getContext().getTransactionContext();
    if (startTxn[planIndex] && tc != null && tc.getTransactionType() == Scope.NONE) {
        this.getContext().getTransactionServer().begin(tc);
        this.planContexts[planIndex] = tc;
    }
    updatePlans[planIndex].open();
    planOpened[planIndex] = true;
}
Also used : CommandContext(org.teiid.query.util.CommandContext) TransactionContext(org.teiid.dqp.service.TransactionContext) VariableContext(org.teiid.query.sql.util.VariableContext)

Example 10 with TransactionContext

use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.

the class TransactionServerImpl method end.

/**
 * Global Transaction
 */
public void end(final String threadId, XidImpl xid, int flags, boolean singleTM) throws XATransactionException {
    TransactionContext tc = checkXAState(threadId, xid, true, true);
    try {
        switch(flags) {
            case XAResource.TMSUSPEND:
                {
                    tc.getSuspendedBy().add(threadId);
                    break;
                }
            case XAResource.TMSUCCESS:
                {
                    // TODO: should close all statements
                    break;
                }
            case XAResource.TMFAIL:
                {
                    cancelTransactions(threadId, false);
                    break;
                }
            default:
                throw new XATransactionException(QueryPlugin.Event.TEIID30520, XAException.XAER_INVAL, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30520));
        }
    } finally {
        tc.setThreadId(null);
        transactions.removeTransactionContext(threadId);
    }
}
Also used : TransactionContext(org.teiid.dqp.service.TransactionContext) XATransactionException(org.teiid.client.xa.XATransactionException)

Aggregations

TransactionContext (org.teiid.dqp.service.TransactionContext)34 Test (org.junit.Test)15 CommandContext (org.teiid.query.util.CommandContext)7 TransactionService (org.teiid.dqp.service.TransactionService)6 XATransactionException (org.teiid.client.xa.XATransactionException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 VariableContext (org.teiid.query.sql.util.VariableContext)5 List (java.util.List)4 SystemException (javax.transaction.SystemException)4 Transaction (javax.transaction.Transaction)4 TeiidComponentException (org.teiid.core.TeiidComponentException)4 TupleBatch (org.teiid.common.buffer.TupleBatch)3 AtomicRequestMessage (org.teiid.dqp.message.AtomicRequestMessage)3 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)3 Command (org.teiid.query.sql.lang.Command)3 ArrayList (java.util.ArrayList)2 RollbackException (javax.transaction.RollbackException)2 Synchronization (javax.transaction.Synchronization)2