@Override public Object processInvocation(final InterceptorContext context) throws Exception { TransactionManager tm = component.getTransactionManager(); int oldTimeout = getCurrentTransactionTimeout(component); try { Transaction oldTx = tm.suspend(); try { return handleInvocation(context); } finally { if (oldTx != null) tm.resume(oldTx); } } finally { tm.setTransactionTimeout(oldTimeout == -1 ? 0 : oldTimeout); } }
tm.setTransactionTimeout(timeout); try { final Transaction suspended = tm.suspend(); try { tm.begin(); final Transaction transaction = tm.suspend(); SimpleXid gtid = SimpleXid.of(getXid(transaction)).withoutBranch(); known.put(gtid, getEntryFor(transaction, gtid)); } catch (Throwable t) { if (suspended != null) try { tm.resume(suspended); } catch (InvalidTransactionException e) { e.addSuppressed(t);
@Test public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception { TransactionManager tm = mock(TransactionManager.class); Transaction tx = mock(Transaction.class); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx); JtaTransactionManager ptm = newJtaTransactionManager(tm); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).begin(); verify(tm).commit(); verify(tm).resume(tx); }
assertEquals( 0, sessionFactory().getStatistics().getEntityLoadCount() ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s = openSession(); Map foo = new HashMap(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s1 = openSession(); foo = ( Map ) s1.get( "Item", "Foo" ); Transaction tx = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s2 = openSession(); foo = ( Map ) s2.get( "Item", "Foo" ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); tx = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
@Test public void testTxHandleCommitKeepsThreadAssociation() throws Exception { // don't wait for ages, has to be set before TX is begun tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS); tm.begin(); Transaction tx = tm.getTransaction(); assertNotNull("Transaction should have started.", tx); assertEquals("TX should have been active", Status.STATUS_ACTIVE, tx.getStatus()); tx.commit(); tx = tm.getTransaction(); assertNotNull("Committing via TX handle should NOT disassociated TX from the current thread.", tx); assertEquals("TX status should have been COMMITTED.", Status.STATUS_COMMITTED, tx.getStatus()); // Remove the TX-thread association. The only public API to achieve it is suspend(), // technically we never resume the same transaction (TX forget). Transaction suspended = tm.suspend(); assertTrue("Wrong TX suspended?.", suspended.equals(tx)); assertNull("TX should've been disassociated from the thread.", tm.getTransaction()); // should be no-op and never fail tm.resume(null); // ensure we don't have any TX-Thread association lurking around a main thread assertNull(tm.getTransaction()); }
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s = openSession(); Map foo = new HashMap(); bar.put( "description", "a small bar" ); s.persist( "Item", bar ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s4 = openSession(); Transaction tx4 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s1 = openSession(); List r1 = s1.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) Transaction tx1 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); assertEquals( r2.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx4 ); List r4 = s4.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list();
private <T> T doInSuspendedTransaction(HibernateCallable<T> callable) { try { Transaction surroundingTransaction = transactionManager.suspend(); LOG.debugf( "Surrounding JTA transaction suspended [%s]", surroundingTransaction ); transactionManager.resume( surroundingTransaction ); LOG.debugf( "Surrounding JTA transaction resumed [%s]", surroundingTransaction );
public void testReadSafetyRollback() throws Exception { AtomicMap<String, String> map = AtomicMapLookup.getAtomicMap(cache, "map"); tm.begin(); map.put("blah", "blah"); assert map.size() == 1; assert map.get("blah").equals("blah"); assert map.containsKey("blah"); Transaction t = tm.suspend(); assertIsEmpty(map); assertIsEmptyMap(cache, "map"); tm.resume(t); tm.rollback(); assertIsEmpty(map); assertIsEmptyMap(cache, "map"); }
bar.put( "description", "a small bar" ); s.persist( "Item", bar ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s4 = openSession(); Transaction tx4 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); .setCacheable( true ).list(); assertEquals( r2.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertEquals( sessionFactory().getStatistics().getUpdateTimestampsCachePutCount(), 0 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); assertEquals( sessionFactory().getStatistics().getUpdateTimestampsCacheMissCount(), 0 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx4 ); List r4 = s4.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list();
Transaction existingTx = this.tm.suspend(); if (existingBatch.getTransaction() != existingTx) { throw new IllegalStateException(); this.tm.resume(tx); setCurrentBatch(batch); return () -> { try { this.tm.suspend(); if (existingBatch != null) { try { this.tm.resume(existingBatch.getTransaction()); } catch (InvalidTransactionException e) { throw new CacheException(e);
public void testTxCommit2() throws Exception { TransactionManager tm = TestingUtil.getTransactionManager(cache); cache.put("key", "old"); tm.begin(); assertEquals("old", cache.get("key")); cache.put("key", "value"); assertEquals("value", cache.get("key")); Transaction t = tm.suspend(); assertEquals("old", cache.get("key")); tm.resume(t); tm.commit(); assertEquals("value", cache.get("key")); assertFalse(cache.isEmpty()); }
public void testRollbacks() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; TransactionManager tm = tl.tm; cache.put("k", "v"); tm.begin(); assertEquals("v", cache.get("k")); Transaction reader = tm.suspend(); tm.begin(); cache.put("k", "v2"); tm.rollback(); tm.resume(reader); Object value = cache.get("k"); assertEquals("v", value); tm.commit(); // even after commit assertEquals("v", cache.get("k")); assertNoLocks(); }
@Test public void jtaTransactionManagerWithPropagationNotSupported() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); Transaction tx = mock(Transaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).resume(tx); }
public void testRollbacksOnNullEntry() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; TransactionManager tm = tl.tm; tm.begin(); assert null == cache.get("k"); Transaction reader = tm.suspend(); tm.begin(); cache.put("k", "v"); assertEquals("v", cache.get("k")); tm.rollback(); tm.resume(reader); assert null == cache.get("k") : "Expecting null but was " + cache.get("k"); tm.commit(); // even after commit assert null == cache.get("k"); assertNoLocks(); } }
@Test public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); Transaction tx = mock(Transaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).begin(); verify(ut).commit(); verify(tm).resume(tx); }
public void testPreviousValueIgnored() throws Exception { cache.put("k", "init"); tm.begin(); cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put("k", "v1"); assertEquals("v1", cache.put("k", "v2")); Transaction tx = tm.suspend(); assertEquals("init", cache.put("k", "other")); tm.resume(tx); commit(); }
@Test public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); Transaction tx = mock(Transaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx); willThrow(new SystemException()).given(ut).begin(); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); } }); fail("Should have thrown CannotCreateTransactionException"); } catch (CannotCreateTransactionException ex) { // expected } assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).resume(tx); }