Search in sources :

Example 21 with TransactionContext

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

the class TransactionServerImpl method begin.

/**
 * Local Transaction
 */
public TransactionContext begin(String threadId) throws XATransactionException {
    TransactionContext tc = checkLocalTransactionState(threadId, false);
    beginDirect(tc);
    tc.setTransactionType(TransactionContext.Scope.LOCAL);
    return tc;
}
Also used : TransactionContext(org.teiid.dqp.service.TransactionContext)

Example 22 with TransactionContext

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

the class ForEachRowPlan method open.

@Override
public void open() throws TeiidComponentException, TeiidProcessingException {
    TransactionContext tc = this.getContext().getTransactionContext();
    if (tc != null && tc.getTransactionType() == Scope.NONE && queryPlan != null && !Boolean.FALSE.equals(queryPlan.requiresTransaction(false))) {
        // start a transaction - if not each of the row plans will
        // be executed in it's own transaction, which is bad for performance
        // TODO: should probably allow non-atomic row plans
        // the parser accepts a trigger block without atomic
        // but the spec mandates it - and we treat it as atomic
        // either way
        // TODO: for non-transactional environments this will
        // trigger an error
        this.getContext().getTransactionServer().begin(tc);
        this.planContext = tc;
    }
    if (queryPlan != null) {
        queryProcessor = new QueryProcessor(queryPlan, getContext().clone(), this.bufferMgr, this.dataMgr);
        tupleSource = new BatchCollector.BatchProducerTupleSource(queryProcessor);
    }
}
Also used : TransactionContext(org.teiid.dqp.service.TransactionContext) BatchCollector(org.teiid.query.processor.BatchCollector) QueryProcessor(org.teiid.query.processor.QueryProcessor)

Example 23 with TransactionContext

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

the class ProcedurePlan method processProcedure.

/**
 * <p>Process the procedure, using the stack of Programs supplied by the
 * ProcessorEnvironment.  With each pass through the loop, the
 * current Program is gotten off the top of the stack, and the
 * current instruction is gotten from that program; each call
 * to an instruction's process method may alter the Program
 * Stack and/or the current instruction pointer of a Program,
 * so it's important that this method's loop refer to the
 * call stack of the ProcessorEnvironment each time, and not
 * cache things in local variables.  If the current Program's
 * current instruction is null, then it's time to pop that
 * Program off the stack.</p>
 *
 * @return List a single tuple containing one Integer: the update
 * count resulting from the procedure execution.
 */
private TupleSource processProcedure() throws TeiidComponentException, TeiidProcessingException, BlockedException {
    // execute plan
    ProgramInstruction inst = null;
    while (!this.programs.empty()) {
        Program program = peek();
        inst = program.getCurrentInstruction();
        if (inst == null) {
            // $NON-NLS-1$
            LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Finished program", program);
            // look ahead to see if we need to process in place
            VariableContext vc = this.cursorStates.getParentContext();
            CursorState last = (CursorState) this.cursorStates.getValue(null);
            if (last != null) {
                if (last.resultsBuffer == null && (last.usesLocalTemp || !txnTupleSources.isEmpty())) {
                    last.resultsBuffer = bufferMgr.createTupleBuffer(last.processor.getOutputElements(), getContext().getConnectionId(), TupleSourceType.PROCESSOR);
                    last.returning = true;
                }
                if (last.returning) {
                    while (last.ts.hasNext()) {
                        List<?> tuple = last.ts.nextTuple();
                        last.resultsBuffer.addTuple(tuple);
                    }
                    last.resultsBuffer.close();
                    last.ts = last.resultsBuffer.createIndexedTupleSource(true);
                    last.returning = false;
                }
            }
            this.pop(true);
            continue;
        }
        try {
            getContext().setCurrentTimestamp(System.currentTimeMillis());
            if (inst instanceof RepeatedInstruction) {
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Executing repeated instruction", inst);
                RepeatedInstruction loop = (RepeatedInstruction) inst;
                if (loop.testCondition(this)) {
                    // $NON-NLS-1$
                    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Passed condition, executing program " + loop.getNestedProgram());
                    inst.process(this);
                    this.push(loop.getNestedProgram());
                    continue;
                }
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Exiting repeated instruction", inst);
                loop.postInstruction(this);
            } else {
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Executing instruction", inst);
                inst.process(this);
                this.evaluator.close();
            }
        } catch (TeiidComponentException e) {
            throw e;
        } catch (Exception e) {
            // processing or teiidsqlexception
            boolean atomic = program.isAtomic();
            while (program.getExceptionGroup() == null) {
                this.pop(false);
                if (this.programs.empty()) {
                    // reached the top without a handler, so throw
                    if (e instanceof TeiidProcessingException) {
                        throw (TeiidProcessingException) e;
                    }
                    throw new ProcedureErrorInstructionException(QueryPlugin.Event.TEIID30167, e);
                }
                program = peek();
                atomic |= program.isAtomic();
            }
            try {
                // allow the current program to go out of scope
                this.pop(false);
                if (atomic) {
                    TransactionContext tc = this.getContext().getTransactionContext();
                    if (tc != null && tc.getTransactionType() != Scope.NONE) {
                        // a non-completing atomic block under a higher level transaction
                        // this will not work correctly until we support
                        // checkpoints/subtransactions
                        tc.getTransaction().setRollbackOnly();
                        getContext().addWarning(TeiidSQLException.create(e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31266)));
                    }
                }
            } catch (IllegalStateException | SystemException | TeiidComponentException e1) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Caught exception while rolling back transaction", e1);
            } catch (Throwable e1) {
                LogManager.logWarning(LogConstants.CTX_DQP, e1);
            }
            if (e instanceof RuntimeException) {
                LogManager.logWarning(LogConstants.CTX_DQP, e);
            } else {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Caught exception in exception hanlding block", e);
            }
            if (program.getExceptionProgram() == null) {
                continue;
            }
            Program exceptionProgram = program.getExceptionProgram();
            this.push(exceptionProgram);
            TeiidSQLException tse = TeiidSQLException.create(e);
            GroupSymbol gs = new GroupSymbol(program.getExceptionGroup());
            this.currentVarContext.setValue(exceptionSymbol(gs, 0), tse.getSQLState());
            this.currentVarContext.setValue(exceptionSymbol(gs, 1), tse.getErrorCode());
            this.currentVarContext.setValue(exceptionSymbol(gs, 2), tse.getTeiidCode());
            this.currentVarContext.setValue(exceptionSymbol(gs, 3), tse);
            this.currentVarContext.setValue(exceptionSymbol(gs, 4), tse.getCause());
            continue;
        }
        program.incrementProgramCounter();
    }
    CursorState last = (CursorState) this.cursorStates.getValue(null);
    if (last == null) {
        return CollectionTupleSource.createNullTupleSource();
    }
    return last.ts;
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ProcedureErrorInstructionException(org.teiid.client.ProcedureErrorInstructionException) VariableContext(org.teiid.query.sql.util.VariableContext) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ProcedureErrorInstructionException(org.teiid.client.ProcedureErrorInstructionException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) BlockedException(org.teiid.common.buffer.BlockedException) XATransactionException(org.teiid.client.xa.XATransactionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) SystemException(javax.transaction.SystemException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TransactionContext(org.teiid.dqp.service.TransactionContext) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 24 with TransactionContext

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

the class ProcedurePlan method pop.

/**
 * @param success
 * @throws TeiidComponentException
 * @throws XATransactionException
 */
public void pop(boolean success) throws TeiidComponentException {
    this.evaluator.close();
    Program program = this.programs.pop();
    VariableContext vc = this.currentVarContext;
    VariableContext cs = this.cursorStates;
    try {
        this.currentVarContext = this.currentVarContext.getParentContext();
        this.cursorStates = this.cursorStates.getParentContext();
        TempTableStore tempTableStore = program.getTempTableStore();
        this.getContext().setTempTableStore(tempTableStore.getParentTempTableStore());
        tempTableStore.removeTempTables();
        if (program.startedTxn()) {
            TransactionService ts = this.getContext().getTransactionServer();
            TransactionContext tc = this.blockContext;
            this.blockContext = null;
            try {
                ts.resume(tc);
                for (WeakReference<DataTierTupleSource> ref : txnTupleSources) {
                    DataTierTupleSource dtts = ref.get();
                    if (dtts != null) {
                        dtts.fullyCloseSource();
                    }
                }
                this.txnTupleSources.clear();
                if (success) {
                    ts.commit(tc);
                } else {
                    ts.rollback(tc);
                }
            } catch (XATransactionException e) {
                throw new TeiidComponentException(QueryPlugin.Event.TEIID30165, e);
            }
        }
    } finally {
        removeAllCursors(cs);
    }
}
Also used : TempTableStore(org.teiid.query.tempdata.TempTableStore) TransactionService(org.teiid.dqp.service.TransactionService) TransactionContext(org.teiid.dqp.service.TransactionContext) DataTierTupleSource(org.teiid.dqp.internal.process.DataTierTupleSource) TeiidComponentException(org.teiid.core.TeiidComponentException) XATransactionException(org.teiid.client.xa.XATransactionException) VariableContext(org.teiid.query.sql.util.VariableContext)

Example 25 with TransactionContext

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

the class TestConnectorWorkItem method testIsImmutablePropertySucceeds.

@Ignore
@Test
public void testIsImmutablePropertySucceeds() throws Exception {
    /*
    	 * Setup:
    	 *  1. requestMsg.isTransactional() must be TRUE 
    	 *  2. manager.isXa() must be FALSE  ()
    	 *  3. command must NOT be a SELECT
    	 *  4. Then, set isImmutable to TRUE, we should SUCCEED
    	 */
    ConnectorManager cm = TestConnectorManager.getConnectorManager();
    ((FakeConnector) cm.getExecutionFactory()).setImmutable(true);
    // command must not be a SELECT
    // $NON-NLS-1$
    Command command = helpGetCommand("update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT);
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    requestMsg.setCommand(command);
    // To make the AtomicRequestMessage transactional, construct your own
    requestMsg.setTransactionContext(new TransactionContext() {

        @Override
        public Xid getXid() {
            return Mockito.mock(Xid.class);
        }
    });
    new ConnectorWorkItem(requestMsg, cm);
}
Also used : Xid(javax.transaction.xa.Xid) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TransactionContext(org.teiid.dqp.service.TransactionContext) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Ignore(org.junit.Ignore) Test(org.junit.Test)

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