@Override public void terminateTransaction(String threadId) throws AdminException { if (threadId == null) { return; } try { cancelTransactions(threadId, false); } catch (XATransactionException e) { throw new AdminProcessingException(QueryPlugin.Event.TEIID30542, e); } }
/** * Local Transaction */ public TransactionContext begin(String threadId) throws XATransactionException { TransactionContext tc = checkLocalTransactionState(threadId, false); beginDirect(tc); tc.setTransactionType(TransactionContext.Scope.LOCAL); return tc; }
/** * Local Transaction */ public void commit(String threadId) throws XATransactionException { TransactionContext tc = checkLocalTransactionState(threadId, true); commitDirect(tc); }
/** * Local Transaction */ public void rollback(String threadId) throws XATransactionException { TransactionContext tc = checkLocalTransactionState(threadId, true); rollbackDirect(tc); }
@Test public void testGlobalOnePhase_teiid_multiple() throws Exception { server.start(THREAD1, XID1, XAResource.TMNOFLAGS, 100,false); TransactionContext tc = server.getOrCreateTransactionContext(THREAD1); server.end(THREAD1, XID1, XAResource.TMSUCCESS, false); server.prepare(THREAD1, XID1, false); server.commit(THREAD1, XID1, true, false); // since there are two sources the commit is not single phase Mockito.verify(xaImporter).commit(tc.getXid(), false); }
@Test public void testGlobalOnePhaseRoolback() throws Exception { server.start(THREAD1, XID1, XAResource.TMNOFLAGS, 100,false); TransactionContext tc = server.getOrCreateTransactionContext(THREAD1); server.end(THREAD1, XID1, XAResource.TMSUCCESS, false); server.prepare(THREAD1, XID1, false); server.rollback(THREAD1, XID1, false); // since there are two sources the commit is not single phase Mockito.verify(xaImporter).rollback(tc.getXid()); }
/** * Global Transaction */ public void commit(final String threadId, XidImpl xid, boolean onePhase, boolean singleTM) throws XATransactionException { TransactionContext tc = checkXAState(threadId, xid, true, false); try { if (singleTM || (onePhase && XAResource.XA_RDONLY == prepare(threadId, xid, singleTM))) { return; //nothing to do } //TODO: we have no way of knowing for sure if we can safely use the onephase optimization this.xaImporter.commit(tc.getXid(), false); } catch (XAException e) { throw new XATransactionException(QueryPlugin.Event.TEIID30507, e); } finally { this.transactions.removeTransactionContext(tc); } }
/** * 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); } }
/** * Global Transaction */ public void forget(final String threadId, XidImpl xid, boolean singleTM) throws XATransactionException { TransactionContext tc = checkXAState(threadId, xid, true, false); try { if (singleTM) { return; } this.xaImporter.forget(xid); } catch (XAException err) { throw new XATransactionException(QueryPlugin.Event.TEIID30510, err); } finally { this.transactions.removeTransactionContext(tc); } }
@Test public void testRequestCancel() throws Exception{ TransactionContext tc = server.getOrCreateTransactionContext(THREAD1); server.begin(tc); server.cancelTransactions(THREAD1, true); Mockito.verify(txn).setRollbackOnly(); } }
@Test public void testPrepareWithSuspended() throws Exception { server.start(THREAD1, XID1, XAResource.TMNOFLAGS, 100,false); server.end(THREAD1, XID1, XAResource.TMSUSPEND,false); try { server.prepare(THREAD1, XID1,false); fail("exception expected"); //$NON-NLS-1$ } catch (XATransactionException ex) { assertEquals("TEIID30505 Suspended work still exists on transaction Teiid-Xid global:1 branch:null format:0.", ex.getMessage()); //$NON-NLS-1$ } }
@Test public void testLocalCancel() throws Exception { server.begin(THREAD1); server.cancelTransactions(THREAD1, false); Mockito.verify(txn).setRollbackOnly(); }
@Before public void setUp() throws Exception { server = new TransactionServerImpl(); tm = Mockito.mock(TransactionManager.class); txn = Mockito.mock(javax.transaction.Transaction.class); Mockito.stub(tm.getTransaction()).toReturn(txn); Mockito.stub(tm.suspend()).toReturn(txn); xaImporter = Mockito.mock(XAImporter.class); Mockito.stub(xaImporter.importTransaction(Mockito.any(), Mockito.any(), Mockito.eq(TIMEOUT))).toReturn(txn); server.setXaImporter(xaImporter); server.setTransactionManager(tm); }
/** * Request level transaction */ public void rollback(TransactionContext context) throws XATransactionException { Assertion.assertTrue(context.getTransactionType() == TransactionContext.Scope.REQUEST); try { rollbackDirect(context); } finally { context.setTransaction(null); context.setTransactionType(Scope.NONE); } }
/** * Request level transaction */ public void commit(TransactionContext context) throws XATransactionException { Assertion.assertTrue(context.getTransactionType() == TransactionContext.Scope.REQUEST); try { commitDirect(context); } finally { context.setTransaction(null); context.setTransactionType(Scope.NONE); } }
/** * Request level transaction */ public void begin(TransactionContext context) throws XATransactionException{ if (context.getTransactionType() != TransactionContext.Scope.NONE) { throw new XATransactionException(QueryPlugin.Event.TEIID30517, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30517)); } beginDirect(context); context.setTransactionType(TransactionContext.Scope.REQUEST); this.transactions.addTransactionContext(context); //it may have been removed if this is a block level operation }
if (config.getTransactionManager() == null) { LogManager.logInfo(LogConstants.CTX_RUNTIME, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089)); this.transactionService.setTransactionManager((TransactionManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] {TransactionManager.class}, new InvocationHandler() { this.transactionService.setDetectTransactions(true); this.transactionService.setTransactionManager(config.getTransactionManager());
@Test public void testGlobalPrepare() throws Exception { server.start(THREAD1, XID1, XAResource.TMNOFLAGS, 100,false); TransactionContext tc = server.getOrCreateTransactionContext(THREAD1); server.end(THREAD1, XID1, XAResource.TMSUCCESS, false); server.prepare(THREAD1, XID1, false); Mockito.verify(xaImporter).prepare(tc.getXid()); server.commit(THREAD1, XID1, true, false); }
/** * Global Transaction */ public void commit(final String threadId, XidImpl xid, boolean onePhase, boolean singleTM) throws XATransactionException { TransactionContext tc = checkXAState(threadId, xid, true, false); try { if (singleTM || (onePhase && XAResource.XA_RDONLY == prepare(threadId, xid, singleTM))) { return; //nothing to do } //TODO: we have no way of knowing for sure if we can safely use the onephase optimization this.xaTerminator.commit(tc.getXid(), false); } catch (XAException e) { throw new XATransactionException(QueryPlugin.Event.TEIID30507, e); } finally { this.transactions.removeTransactionContext(tc); } }
/** * 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); } }