Java Code Examples for javax.transaction.TransactionManager#rollback()

The following examples show how to use javax.transaction.TransactionManager#rollback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBasicManagedDataSource.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/** DBCP-564 */
@Test
public void testSetRollbackOnlyBeforeGetConnectionDoesNotLeak() throws Exception {
    final TransactionManager transactionManager = ((BasicManagedDataSource) ds).getTransactionManager();
    final int n = 3;
    ds.setMaxIdle(n);
    ds.setMaxTotal(n);

    for (int i = 0; i <= n; i++) { // loop n+1 times
        transactionManager.begin();
        transactionManager.setRollbackOnly();
        final Connection conn = getConnection();
        assertNotNull(conn);
        conn.close();
        transactionManager.rollback();
    }

    assertEquals(0, ds.getNumActive());
    assertEquals(1, ds.getNumIdle());
}
 
Example 2
Source File: DSSXATransactionManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public void rollback() {
	TransactionManager txManager = getTransactionManager();
	if (txManager == null) {
           return;
       }
	try {
		txManager.rollback();				
	} catch (Exception e) {
		log.warn("Error from transaction manager when "
		        + "rollbacking: " + e.getMessage(), e);
	} finally {
		this.beginTx.set(false);
	}
}
 
Example 3
Source File: TransactionalInterceptorBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected void endTransaction(TransactionManager tm, Transaction tx, RunnableWithException afterEndTransaction)
        throws Exception {
    if (tx != tm.getTransaction()) {
        throw new RuntimeException(jtaLogger.i18NLogger.get_wrong_tx_on_thread());
    }

    if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
        tm.rollback();
    } else {
        tm.commit();
    }

    afterEndTransaction.run();
}
 
Example 4
Source File: InfinispanTx.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Construct a local cache manager
   DefaultCacheManager cacheManager = new DefaultCacheManager();
   // Create a transaction cache config
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL);
   Configuration cacheConfig = builder.build();
   // Create a cache with the config
   Cache<String, String> cache = cacheManager.administration()
           .withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
           .getOrCreateCache("cache", cacheConfig);
   // 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();
}
 
Example 5
Source File: TransactionalService.java    From tutorials with MIT License 5 votes vote down vote up
public void startBackgroundBatch() {
    try {
        TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager();
        tm.begin();
        transactionalCache.put(KEY, 1000);
        System.out.println("HowManyVisits should now be 1000, " + "but we are holding the transaction");
        Thread.sleep(1000L);
        tm.rollback();
        System.out.println("The slow batch suffered a rollback");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: InfinispanRemoteTx.java    From infinispan-simple-tutorials with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
   // Create a configuration for a locally-running server
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer()
            .host("127.0.0.1")
            .port(ConfigurationProperties.DEFAULT_HOTROD_PORT)
          .security().authentication()
            //Add user credentials.
            .username("username")
            .password("password")
            .realm("default")
            .saslMechanism("DIGEST-MD5");

   // Configure the RemoteCacheManager to use a transactional cache as default
   // Use the simple TransactionManager in hot rod client
   builder.transaction().transactionManagerLookup(RemoteTransactionManagerLookup.getInstance());
   // The cache will be enlisted as Synchronization
   builder.transaction().transactionMode(TransactionMode.NON_XA);

   // Connect to the server
   RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());

   // Create a transactional cache in the server since there is none available by default.
   cacheManager.administration().createCache(CACHE_NAME, new XMLStringConfiguration(TEST_CACHE_XML_CONFIG));
   RemoteCache<String, String> cache = cacheManager.getCache(CACHE_NAME);

   // Obtain the transaction manager
   TransactionManager transactionManager = cache.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();
}
 
Example 7
Source File: GridJtaTransactionManagerSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testJtaTxContextSwitchWithExistingTx() throws Exception {
    for (TransactionIsolation isolation : TransactionIsolation.values()) {
        TransactionConfiguration cfg = grid().context().config().getTransactionConfiguration();

        cfg.setDefaultTxConcurrency(txConcurrency);
        cfg.setDefaultTxIsolation(isolation);

        TransactionManager jtaTm = jotm.getTransactionManager();

        IgniteCache<Integer, String> cache = jcache();

        jtaTm.begin();

        Transaction tx1 = jtaTm.getTransaction();

        cache.put(1, Integer.toString(1));

        assertNotNull(grid().transactions().tx());

        assertEquals(ACTIVE, grid().transactions().tx().state());

        assertEquals(Integer.toString(1), cache.get(1));

        jtaTm.suspend();

        jtaTm.begin();

        Transaction tx2 = jtaTm.getTransaction();

        assertNotSame(tx1, tx2);

        cache.put(2, Integer.toString(2));

        try {
            jtaTm.resume(tx1);

            fail("jtaTm.resume shouldn't success.");
        }
        catch (IllegalStateException ignored) {
            // No-op.
        }
        finally {
            jtaTm.rollback(); //rolling back tx2
        }

        jtaTm.resume(tx1);
        jtaTm.rollback();

        cache.removeAll();
    }
}