javax.transaction.TransactionManager类的使用及代码示例

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

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

TransactionManager介绍

[英]The TransactionManager interface defines the methods that allow an application server to manage transactions on behalf of the applications.

User applications should not use this interface directly, but use UserTransaction insted if they need to do their own transaction management.

Internally, the transaction manager associates transactions with threads, and the methods here operate on the transaction associated with the calling thread.
[中]TransactionManager接口定义了允许应用程序服务器代表应用程序管理事务的方法。
用户应用程序不应直接使用此界面,但如果需要自己进行事务管理,则应使用UserTransaction insted。
在内部,事务管理器将事务与线程关联,这里的方法对与调用线程关联的事务进行操作。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public void registerSynchronization(Synchronization sync) throws RollbackException, SystemException {
  this.transactionManager.getTransaction().registerSynchronization(sync);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Transaction createTransaction(@Nullable String name, int timeout) throws NotSupportedException, SystemException {
  if (timeout >= 0) {
    this.transactionManager.setTransactionTimeout(timeout);
  }
  this.transactionManager.begin();
  return new ManagedTransactionAdapter(this.transactionManager);
}

代码示例来源: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: stackoverflow.com

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/fooService.xml")
public class FooServiceTests {

  private @Autowired TransactionManager transactionManager;
  private @Autowired FooService fooService;

  @Test
  public void testProvideService() {
    TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
    fooService.provideService();
    transactionManager.rollback(status);
    // assert repository values are unchanged ... 
}

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

private void doRollback(boolean isNew, Throwable originalException) {
 Throwable rollbackEx = null;
 try {
  if (isNew) {
   transactionManager.rollback();
  } else {
   transactionManager.setRollbackOnly();
  }
 } catch (SystemException e) {
  LOGGER.debug("Error when rolling back transaction", e);
 } catch (RuntimeException e) {
  rollbackEx = e;
  throw e;
 } catch (Error e) {
  rollbackEx = e;
  throw e;
 } finally {
  if (rollbackEx != null && originalException != null) {
   LOGGER.error("Error when rolling back transaction, original exception was:", originalException);
  }
 }
}

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

public void testInvalidation() throws Exception {
 assertEquals(Collections.singletonList(address(0)), advancedCache(0).getDistributionManager().locate(k0));
 assertEquals(Collections.singletonList(address(0)), advancedCache(1).getDistributionManager().locate(k0));
 advancedCache(1).put(k0, "k1");
 assertTrue(advancedCache(1).getDataContainer().containsKey(k0));
 assertTrue(advancedCache(0).getDataContainer().containsKey(k0));
 tm(0).begin();
 cache(0).put(k0, "v2");
 tm(0).commit();
 assertFalse(advancedCache(1).getDataContainer().containsKey(k0));
}

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

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

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

public void testFailure() throws Exception {
   TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager();
   transactionManager.begin();
   try {
     cache.put("k", "v");
     assert false;
   } catch (Exception e) {
     log.debug("Ignoring expected exception during put", e);
     assertEquals(transactionManager.getTransaction().getStatus(), Status.STATUS_MARKED_ROLLBACK);
   }
  }
}

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

public void testLockWithTmCommit() throws Throwable {
   tm().begin();
   cache.getAdvancedCache().lock("k");
   assertTrue(lockManager().isLocked("k"));
   tm().commit();
   assertFalse(lockManager().isLocked("k"));
  }
}

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

public void testLockWithTmRollback() throws Throwable {
 tm().begin();
 cache.getAdvancedCache().lock("k");
 assertTrue(lockManager().isLocked("k"));
 tm().rollback();
 assertFalse(lockManager().isLocked("k"));
}

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

@Override
 public void call() throws Exception {
   Cache<Object, Object> c = cm.getCache();
   if (inTran) c.getAdvancedCache().getTransactionManager().begin();
   c.put("key1", new SEntity(1, "name1", "surname1"));
   if (inTran) c.getAdvancedCache().getTransactionManager().commit();
   assertEquals(searchByName("name1", c).size(), 1, "should be 1, even repeating this");
 }
});

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

public void testKeySetIsEmptyAfterLocalClear() throws Exception {
 cache.put(1, "v1");
 tm().begin();
 try {
   cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
   assertTrue(cache.keySet().isEmpty());
 } finally {
   tm().commit();
 }
}

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

public void testEntrySetIsEmptyAfterLocalClear() throws Exception {
 cache.put(1, "v1");
 tm().begin();
 try {
   cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
   assertTrue(cache.entrySet().isEmpty());
 } finally {
   tm().commit();
 }
}

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

public void testValuesIsEmptyAfterLocalClear() throws Exception {
 cache.put(1, "v1");
 tm().begin();
 try {
   cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
   assertTrue(cache.values().isEmpty());
 } finally {
   tm().commit();
 }
}

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

private void testLocalOperation(CheckRemoteLockAcquiredOnlyOnceTest.CacheOperation o) throws Exception {
   assert !advancedCache(1).getRpcManager().getTransport().isCoordinator();
   assert advancedCache(0).getRpcManager().getTransport().isCoordinator();

   tm(0).begin();

   o.execute();

   assert lockManager(0).isLocked(key);
   assert !lockManager(1).isLocked(key);

   assertEquals(controlInterceptor.remoteInvocations, 0);
   tm(0).rollback();
  }
}

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

public void testTxCommit1() throws Exception {
 TransactionManager tm = TestingUtil.getTransactionManager(cache);
 tm.begin();
 cache.put("key", "value");
 Transaction t = tm.suspend();
 assertTrue(cache.isEmpty());
 tm.resume(t);
 tm.commit();
 assertFalse(cache.isEmpty());
}

代码示例来源:origin: spring-projects/spring-framework

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

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

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

代码示例来源:origin: jbosstm/narayana

@Test
  public void test() throws Exception
  {
    javax.transaction.TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();

    transactionManager.begin();

    Transaction currentTrans = transactionManager.getTransaction();

    TestResource res1, res2;
    currentTrans.enlistResource( res1 = new TestResource() );
    currentTrans.enlistResource( res2 = new TestResource() );

    currentTrans.delistResource( res2, XAResource.TMSUCCESS );
    currentTrans.delistResource( res1, XAResource.TMSUCCESS );

    transactionManager.commit();
  }
}

代码示例来源:origin: jbosstm/narayana

@Test
  public void test() throws Exception
  {
    javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();

    tm.begin();

    javax.transaction.Transaction theTransaction = tm.getTransaction();

    tm.commit();

    tm.resume(theTransaction);
  }
}

相关文章