Search in sources :

Example 11 with TransactionContext

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

the class TransactionServerImpl method commit.

/**
 * Local Transaction
 */
public void commit(String threadId) throws XATransactionException {
    TransactionContext tc = checkLocalTransactionState(threadId, true);
    commitDirect(tc);
}
Also used : TransactionContext(org.teiid.dqp.service.TransactionContext)

Example 12 with TransactionContext

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

the class TransactionServerImpl method start.

/**
 * Global Transaction
 */
public void start(final String threadId, final XidImpl xid, int flags, int timeout, boolean singleTM) throws XATransactionException {
    TransactionContext tc = null;
    switch(flags) {
        case XAResource.TMNOFLAGS:
            {
                try {
                    checkXAState(threadId, xid, false, false);
                    tc = transactions.getOrCreateTransactionContext(threadId);
                    if (tc.getTransactionType() != TransactionContext.Scope.NONE) {
                        throw new XATransactionException(QueryPlugin.Event.TEIID30517, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30517));
                    }
                    tc.setTransactionTimeout(timeout);
                    tc.setXid(xid);
                    tc.setTransactionType(TransactionContext.Scope.GLOBAL);
                    if (singleTM) {
                        tc.setTransaction(transactionManager.getTransaction());
                        if (tc.getTransaction() == null) {
                            // in theory we could inflow the txn and then change all of the methods to check singleTM off of the context
                            throw new XATransactionException(QueryPlugin.Event.TEIID30590, XAException.XAER_INVAL, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30590));
                        }
                    } else {
                        FutureWork<Transaction> work = new FutureWork<Transaction>(new Callable<Transaction>() {

                            @Override
                            public Transaction call() throws Exception {
                                return transactionManager.getTransaction();
                            }
                        }, 0);
                        workManager.doWork(work, WorkManager.INDEFINITE, tc, null);
                        tc.setTransaction(work.get());
                    }
                } catch (NotSupportedException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (WorkException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (InterruptedException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (ExecutionException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (SystemException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                }
                break;
            }
        case XAResource.TMJOIN:
        case XAResource.TMRESUME:
            {
                tc = checkXAState(threadId, xid, true, false);
                TransactionContext threadContext = transactions.getOrCreateTransactionContext(threadId);
                if (threadContext.getTransactionType() != TransactionContext.Scope.NONE) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30517, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30517));
                }
                if (flags == XAResource.TMRESUME && !tc.getSuspendedBy().remove(threadId)) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30518, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30518, new Object[] { xid, threadId }));
                }
                break;
            }
        default:
            throw new XATransactionException(QueryPlugin.Event.TEIID30519, XAException.XAER_INVAL, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30519));
    }
    tc.setThreadId(threadId);
    transactions.addTransactionContext(tc);
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) TransactionContext(org.teiid.dqp.service.TransactionContext) WorkException(javax.resource.spi.work.WorkException) XATransactionException(org.teiid.client.xa.XATransactionException) NotSupportedException(javax.resource.NotSupportedException) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Example 13 with TransactionContext

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

the class DQPCore method logMMCommand.

void logMMCommand(RequestWorkItem workItem, Event status, Long rowCount, Long cpuTime) {
    if ((status != Event.PLAN && !LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.INFO)) || (status == Event.PLAN && !LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.TRACE))) {
        return;
    }
    RequestMessage msg = workItem.requestMsg;
    DQPWorkContext workContext = DQPWorkContext.getWorkContext();
    RequestID rID = workItem.requestID;
    String txnID = null;
    TransactionContext tc = workItem.getTransactionContext();
    if (tc != null && tc.getTransactionType() != Scope.NONE) {
        txnID = tc.getTransactionId();
    }
    String appName = workContext.getAppName();
    // Log to request log
    CommandLogMessage message = null;
    if (status == Event.NEW) {
        message = new CommandLogMessage(System.currentTimeMillis(), rID.toString(), txnID, workContext.getSessionId(), appName, workContext.getUserName(), workContext.getVdbName(), workContext.getVdbVersion(), msg.getCommandString(), cpuTime);
    } else {
        QueryProcessor qp = workItem.getProcessor();
        PlanNode plan = null;
        if (LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.TRACE) && qp != null) {
            plan = qp.getProcessorPlan().getDescriptionProperties();
        }
        message = new CommandLogMessage(System.currentTimeMillis(), rID.toString(), txnID, workContext.getSessionId(), workContext.getUserName(), workContext.getVdbName(), workContext.getVdbVersion(), rowCount, status, plan);
    }
    LogManager.log(status == Event.PLAN ? MessageLevel.TRACE : MessageLevel.INFO, LogConstants.CTX_COMMANDLOGGING, message);
}
Also used : PlanNode(org.teiid.client.plan.PlanNode) RequestID(org.teiid.dqp.message.RequestID) TransactionContext(org.teiid.dqp.service.TransactionContext) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) RequestMessage(org.teiid.client.RequestMessage) CommandLogMessage(org.teiid.logging.CommandLogMessage) QueryProcessor(org.teiid.query.processor.QueryProcessor)

Example 14 with TransactionContext

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

the class TestProcErrors method testNestedBeginAtomicException.

@Test
public void testNestedBeginAtomicException() throws Exception {
    TransformationMetadata tm = RealMetadataFactory.example1Cached();
    String query = "BEGIN atomic\n" + " declare string VARIABLES.RESULT;\n" + " begin atomic select 1/0; exception e end end";
    ProcessorPlan plan = getProcedurePlan(query, tm);
    // Create expected results
    List<?>[] expected = new List[0];
    // $NON-NLS-1$
    CommandContext context = new CommandContext("pID", null, null, null, 1);
    QueryMetadataInterface metadata = new TempMetadataAdapter(tm, new TempMetadataStore());
    context.setMetadata(metadata);
    TransactionContext tc = new TransactionContext();
    Transaction txn = Mockito.mock(Transaction.class);
    tc.setTransaction(txn);
    tc.setTransactionType(Scope.REQUEST);
    TransactionService ts = Mockito.mock(TransactionService.class);
    context.setTransactionService(ts);
    context.setTransactionContext(tc);
    TestProcessor.helpProcess(plan, context, new HardcodedDataManager(), expected);
    Mockito.verify(txn, Mockito.times(3)).setRollbackOnly();
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) TransactionService(org.teiid.dqp.service.TransactionService) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) Transaction(javax.transaction.Transaction) TransactionContext(org.teiid.dqp.service.TransactionContext) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Test(org.junit.Test)

Example 15 with TransactionContext

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

the class TempTableStore method getSynchronization.

private TempTableSynchronization getSynchronization(CommandContext context) throws TeiidProcessingException {
    TempTableSynchronization synch = null;
    if (context == null || transactionMode == TransactionMode.NONE) {
        return null;
    }
    TransactionContext tc = context.getTransactionContext();
    if (tc == null || tc.getTransactionType() == Scope.NONE) {
        return null;
    }
    String transactionId = tc.getTransactionId();
    synch = synchronizations.get(transactionId);
    if (synch == null) {
        boolean success = false;
        try {
            synch = new TempTableSynchronization(transactionId);
            synchronizations.put(transactionId, synch);
            tc.getTransaction().registerSynchronization(synch);
            success = true;
        } catch (RollbackException e) {
            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30223, e);
        } catch (SystemException e) {
            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30224, e);
        } finally {
            if (!success) {
                synchronizations.remove(transactionId);
            }
        }
    }
    return synch;
}
Also used : SystemException(javax.transaction.SystemException) TransactionContext(org.teiid.dqp.service.TransactionContext) RollbackException(javax.transaction.RollbackException) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

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