@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); } }
@Override public TransactionBatch createBatch() { if (this.tm == null) return NON_TX_BATCH; TransactionBatch batch = getCurrentBatch(); try { if ((batch != null) && (batch.getState() == Batch.State.ACTIVE)) { return batch.interpose(); } this.tm.suspend(); this.tm.begin(); Transaction tx = this.tm.getTransaction(); tx.registerSynchronization(CURRENT_BATCH_SYNCHRONIZATION); batch = new InfinispanBatch(tx); setCurrentBatch(batch); return batch; } catch (RollbackException | SystemException | NotSupportedException e) { throw new CacheException(e); } }
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); }
public void testPutIfAbsent() throws Throwable { Object k1 = getKeyForCache(0); tm(0).begin(); assertNull(cache(0).putIfAbsent(k1, "v1")); Transaction suspendedTx = tm(0).suspend(); cache(0).put(k1, "v2"); assertEquals(cache(0).get(k1), "v2"); assertEquals(cache(1).get(k1), "v2"); suspendedTx.commit(); assertEquals("v1", cache(0).get(k1)); assertEquals("v1", cache(1).get(k1)); }
@Test (expectedExceptions = TimeoutException.class) public void testMultiLockFailure() throws Exception { Cache<String, String> cache1 = cache(0), cache2 = cache(1); cache1.put("k1", "v"); cache1.put("k2", "v"); cache1.put("k3", "v"); tm(1).begin(); cache2.put("k3", "v2"); tm(1).suspend(); tm(0).begin(); cache1.getAdvancedCache().lock(Arrays.asList("k1", "k2", "k3")); tm(0).rollback(); }
public void testStaleLock() throws SystemException, NotSupportedException { c1.put("k", "v"); assert c1.get("k").equals("v"); assert c2.get("k").equals("v"); TransactionManager tm = TestingUtil.getTransactionManager(c1); tm.begin(); c1.getAdvancedCache().lock("k"); tm.suspend(); // test that both c1 and c2 have locked k assertLocked(c1, "k"); assertLocked(c2, "k"); cacheManagers.get(0).stop(); TestingUtil.blockUntilViewReceived(c2, 1); EmbeddedCacheManager cacheManager = c2.getCacheManager(); assert cacheManager.getMembers().size() == 1; // may take a while from when the view change is seen through to when the lock is cleared TestingUtil.sleepThread(1000); assertNotLocked(c2, "k"); }
public void testSimpleReadOnlTx() throws Exception { tm().begin(); assert cache.get("k") == null; Transaction transaction = tm().suspend(); LocalXaTransaction localTransaction = (LocalXaTransaction) txTable().getLocalTransaction(transaction); assert localTransaction != null && localTransaction.isReadOnly(); }
public void testNotROWhenHasWrites() throws Exception { tm().begin(); cache.put("k", "v"); assert TestingUtil.extractLockManager(cache).isLocked("k"); Transaction transaction = tm().suspend(); LocalXaTransaction localTransaction = (LocalXaTransaction) txTable().getLocalTransaction(transaction); assert localTransaction != null && !localTransaction.isReadOnly(); }
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();
public void testReplace2() throws Throwable { Object k1 = getKeyForCache(0); cache(0).put(k1, "v1"); tm(0).begin(); assertEquals("v1", cache(0).replace(k1, "v2")); Transaction suspendedTx = tm(0).suspend(); cache(0).put(k1, "v3"); assertEquals(cache(0).get(k1), "v3"); assertEquals(cache(1).get(k1), "v3"); suspendedTx.commit(); assertEquals("v2", cache(0).get(k1)); assertEquals("v2", cache(1).get(k1)); }
public void testSilentMultiLockFailure() throws Exception { Cache<String, String> cache1 = cache(0), cache2 = cache(1); cache1.put("k1", "v"); cache1.put("k2", "v"); cache1.put("k3", "v"); tm(1).begin(); cache2.put("k3", "v2"); tm(1).suspend(); tm(0).begin(); assert !cache1.getAdvancedCache().withFlags(FAIL_SILENTLY).lock(Arrays.asList("k1", "k2", "k3")); tm(0).rollback(); }
@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()); }
@Override public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException { // JCA 1.6 FR 13.5.6 // The application server must set the thread context class loader to the endpoint // application class loader during the beforeDelivery call. previousClassLoader = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(getApplicationClassLoader()); try { final TransactionManager tm = getTransactionManager(); // TODO: in violation of JCA 1.6 FR 13.5.9? previousTx = tm.suspend(); boolean isTransacted = service.isDeliveryTransacted(method); if (isTransacted) { tm.begin(); currentTx = tm.getTransaction(); if (xaRes != null) currentTx.enlistResource(xaRes); } } catch (Throwable t) { throw new ApplicationServerInternalException(t); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader); } }
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();
public void testConditionalRemove() throws Throwable { Object k1 = getKeyForCache(0); cache(0).put(k1, "v1"); tm(0).begin(); assertTrue(cache(0).remove(k1, "v1")); Transaction suspendedTx = tm(0).suspend(); cache(0).put(k1, "v2"); assertEquals(cache(0).get(k1), "v2"); assertEquals(cache(1).get(k1), "v2"); log.trace("here it is"); suspendedTx.commit(); assertNull(cache(0).get(k1)); assertNull(cache(1).get(k1)); } }
@Test (expectedExceptions = TimeoutException.class) public void testLockFailure() throws Exception { Cache<String, String> cache1 = cache(0), cache2 = cache(1); cache1.put("k", "v"); tm(1).begin(); cache2.put("k", "v2"); tm(1).suspend(); tm(0).begin(); cache1.getAdvancedCache().lock("k"); tm(0).rollback(); }
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 );