javax.transaction.TransactionManager.rollback()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(265)

本文整理了Java中javax.transaction.TransactionManager.rollback()方法的一些代码示例,展示了TransactionManager.rollback()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TransactionManager.rollback()方法的具体详情如下:
包路径:javax.transaction.TransactionManager
类名称:TransactionManager
方法名:rollback

TransactionManager.rollback介绍

[英]Rolls back the transaction associated with the calling thread.
[中]回滚与调用线程关联的事务。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

public static void tryCommit(TransactionManager tm) throws Exception {
    if ( tm.getStatus() == Status.STATUS_MARKED_ROLLBACK ) {
      tm.rollback();
    }
    else {
      tm.commit();
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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 );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@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;
    }
  }
}

代码示例来源:origin: org.infinispan/infinispan-core

@Override
  public Void call() throws Exception {
   tm.begin();
   try {
     cache.put(1, "v2");
     waitLatch.countDown();
     continueLatch.await();
   } catch (Exception e) {
     tm.setRollbackOnly();
     throw e;
   } finally {
     if (tm.getStatus() == Status.STATUS_ACTIVE) tm.commit();
     else tm.rollback();
   }
   return null;
  }
});

代码示例来源:origin: org.infinispan/infinispan-core

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();
   }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

tm.begin();
transactionCoordinator.explicitJoin();
assertEquals(
);
tm.commit();
assertFalse( transactionCoordinator.isJoined() );
tm.begin();
transactionCoordinator.explicitJoin();
assertEquals(
);
tm.rollback();

代码示例来源:origin: infinispan/infinispan-simple-tutorials

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();
}

代码示例来源:origin: org.infinispan/infinispan-core

public void testTxCreationRollback() throws Exception {
 tm.begin();
 cache.put("key", "value");
 tm.rollback();
 expectTransactionBoundaries(false);
 expectSingleEntryOnlyPreCreated("key", "value");
}

代码示例来源:origin: hibernate/hibernate-orm

@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() );
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Used by envers...
 */
public static void tryCommit() throws Exception {
  if ( transactionManager().getStatus() == Status.STATUS_MARKED_ROLLBACK ) {
    transactionManager().rollback();
  }
  else {
    transactionManager().commit();
  }
}

代码示例来源:origin: kiegroup/jbpm

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();

代码示例来源:origin: org.infinispan/infinispan-core

@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;
}

代码示例来源:origin: org.infinispan/infinispan-cachestore-jdbc

@Test
public void testTxRollback() throws Exception {
 TransactionManager tm = TestingUtil.getTransactionManager(cache);
 tm.begin();
 Transaction tx = tm.getTransaction();
 cache.put(KEY1, VAL1);
 cache.put(KEY2, VAL2);
 tm.rollback();
 assert tx.getStatus() == STATUS_ROLLEDBACK;
 assertRowCount(0);
}

代码示例来源:origin: mulesoft/mule

@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());
}

代码示例来源:origin: wildfly/wildfly

@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;
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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() );

代码示例来源:origin: org.infinispan/infinispan-core

transactionManager.begin();
lastValue = value;
cache.put("counter", value);
transactionManager.commit();
success = true;
    transactionManager.rollback();
 } catch (Throwable t) {

代码示例来源:origin: org.infinispan/infinispan-core

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();
 }
}

代码示例来源:origin: org.mule.tests/mule-tests-unit

@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());
}

代码示例来源:origin: com.expanset.hk2/hk2-persistence

protected void commitOrRollback() 
      throws SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    if(transactionManager.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
      transactionManager.rollback();
    } else {
      transactionManager.commit();
    }
  }    
}

相关文章