private <T> T doInNewTransaction(HibernateCallable<T> callable, TransactionManager transactionManager) { try { // start the new isolated transaction transactionManager.begin(); try { T result = callable.call(); // if everything went ok, commit the isolated transaction transactionManager.commit(); return result; } catch (Exception e) { try { transactionManager.rollback(); } catch (Exception ignore) { LOG.unableToRollbackIsolatedTransaction( e, ignore ); } throw new HibernateException( "Could not apply work", e ); } } catch (SystemException e) { throw new HibernateException( "Unable to start isolated transaction", e ); } catch (NotSupportedException e) { throw new HibernateException( "Unable to start isolated transaction", e ); } }
@Test(expected = IllegalStateException.class) public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() throws Exception { try (Session s = openSession()) { tm.begin(); Transaction tx = null; try { // A call to begin() with an active Tx should cause an IllegalStateException tx = s.beginTransaction(); } catch (Exception e) { if ( tx != null && tx.isActive() ) { tx.rollback(); } throw e; } } catch (Exception e) { if ( tm.getStatus() == Status.STATUS_ACTIVE ) { tm.rollback(); } throw e; } } }
private void assertNotLocked(Cache<String, String> c, String key) throws SystemException, NotSupportedException { TransactionManager tm = TestingUtil.getTransactionManager(c); tm.begin(); try { c.put(key, "dummy"); // should time out } catch (TimeoutException e) { assert false : "Should not have been locked!"; } finally { tm.rollback(); } } }
public static void main(String[] args) throws Exception { // Define the default cache to be transactional ConfigurationBuilder builder = new ConfigurationBuilder(); builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL); // Construct a local cache manager using the configuration we have defined DefaultCacheManager cacheManager = new DefaultCacheManager(builder.build()); // Obtain the default cache Cache<String, String> cache = cacheManager.getCache(); // Obtain the transaction manager TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager(); // Perform some operations within a transaction and commit it transactionManager.begin(); cache.put("key1", "value1"); cache.put("key2", "value2"); transactionManager.commit(); // Display the current cache contents System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2")); // Perform some operations within a transaction and roll it back transactionManager.begin(); cache.put("key1", "value3"); cache.put("key2", "value4"); transactionManager.rollback(); // Display the current cache contents System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2")); // Stop the cache manager and release all resources cacheManager.stop(); }
@Test public void rollbackCmtUsageTest() throws Exception { // pre conditions final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager(); assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() ); // begin the transaction tm.begin(); assertEquals( Status.STATUS_ACTIVE, tm.getStatus() ); final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator( true ); // NOTE : because of auto-join assertTrue( transactionCoordinator.isSynchronizationRegistered() ); // create and add a local Synchronization SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl(); transactionCoordinator.getLocalSynchronizations().registerSynchronization( localSync ); // rollback the transaction tm.rollback(); // post conditions assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() ); assertFalse( transactionCoordinator.isSynchronizationRegistered() ); assertEquals( 0, localSync.getBeforeCompletionCount() ); assertEquals( 0, localSync.getSuccessfulCompletionCount() ); assertEquals( 1, localSync.getFailedCompletionCount() ); }
com.arjuna.ats.jta.TransactionManager.transactionManager().begin(); try { EntityManager em = getEmf().createEntityManager(); query.executeUpdate(); com.arjuna.ats.jta.TransactionManager.transactionManager().commit(); } catch( Throwable t ) { com.arjuna.ats.jta.TransactionManager.transactionManager().rollback();
@Override public Integer call() throws Exception { int c = 0; while (!stop) { boolean success = false; try { if (tx) tm.begin(); cache.put("test" + c, c); if (tx) tm.commit(); success = true; c++; // Without this, the writing thread would occupy 1 core completely before the 2nd node joins. Thread.sleep(1); } catch (Exception e) { log.errorf(e, "Error writing key test%s", c); stop(); } finally { if (tx && !success) { try { tm.rollback(); } catch (SystemException e) { log.error(e); } } } } return c; }
@Test public void testTxManagerRollbackDissassociatesThread() 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()); tm.rollback(); assertNull("Committing via TX Manager should have disassociated TX from the current thread.", tm.getTransaction()); }
@Override public void afterDelivery() throws ResourceException { final TransactionManager tm = getTransactionManager(); try { if (currentTx != null) { if (currentTx.getStatus() == Status.STATUS_MARKED_ROLLBACK) tm.rollback(); else tm.commit(); currentTx = null; } if (previousTx != null) { tm.resume(previousTx); previousTx = null; } } catch (InvalidTransactionException e) { throw new LocalTransactionException(e); } catch (HeuristicMixedException e) { throw new LocalTransactionException(e); } catch (SystemException e) { throw new LocalTransactionException(e); } catch (HeuristicRollbackException e) { throw new LocalTransactionException(e); } catch (RollbackException e) { throw new LocalTransactionException(e); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader); previousClassLoader = null; } }
sessionFactory().getStatistics().clear(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s = openSession(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertFalse( s.isOpen() ); assertEquals( sessionFactory().getStatistics().getEntityInsertCount(), 0 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); s = openSession(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); assertFalse( s.isOpen() ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); s = openSession(); Map item = new HashMap(); item.put( "description", "The only item we have" ); s.persist( "Item", item ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertFalse( s.isOpen() ); assertEquals( sessionFactory().getStatistics().getFlushCount(), 1 ); assertNotNull( item ); s.delete( item ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertFalse( s.isOpen() );
private void assertLocked(Cache<String, String> c, String key) throws SystemException, NotSupportedException { TransactionManager tm = TestingUtil.getTransactionManager(c); tm.begin(); try { c.put(key, "dummy"); // should time out assert false : "Should have been locked!"; } catch (TimeoutException e) { // ignoring timeout exception } catch (RemoteException e) { assert e.getCause() instanceof TimeoutException; // ignoring timeout exception } finally { tm.rollback(); } }
@Test public void testTxManagerRollbackDissassociatesThread() 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()); tm.rollback(); assertNull("Committing via TX Manager should have disassociated TX from the current thread.", tm.getTransaction()); }