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

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

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

TransactionManager.suspend介绍

[英]Suspend the association the calling thread has to a transaction, and return the suspended transaction. When returning from this method, the calling thread is no longer associated with a transaction.
[中]挂起调用线程与事务的关联,并返回挂起的事务。从该方法返回时,调用线程不再与事务关联。

代码示例

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

  1. @Override
  2. public Object processInvocation(final InterceptorContext context) throws Exception {
  3. TransactionManager tm = component.getTransactionManager();
  4. int oldTimeout = getCurrentTransactionTimeout(component);
  5. try {
  6. Transaction oldTx = tm.suspend();
  7. try {
  8. return handleInvocation(context);
  9. } finally {
  10. if (oldTx != null) tm.resume(oldTx);
  11. }
  12. } finally {
  13. tm.setTransactionTimeout(oldTimeout == -1 ? 0 : oldTimeout);
  14. }
  15. }

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

  1. @Override
  2. public TransactionBatch createBatch() {
  3. if (this.tm == null) return NON_TX_BATCH;
  4. TransactionBatch batch = getCurrentBatch();
  5. try {
  6. if ((batch != null) && (batch.getState() == Batch.State.ACTIVE)) {
  7. return batch.interpose();
  8. }
  9. this.tm.suspend();
  10. this.tm.begin();
  11. Transaction tx = this.tm.getTransaction();
  12. tx.registerSynchronization(CURRENT_BATCH_SYNCHRONIZATION);
  13. batch = new InfinispanBatch(tx);
  14. setCurrentBatch(batch);
  15. return batch;
  16. } catch (RollbackException | SystemException | NotSupportedException e) {
  17. throw new CacheException(e);
  18. }
  19. }

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

  1. tm.setTransactionTimeout(timeout);
  2. try {
  3. final Transaction suspended = tm.suspend();
  4. try {
  5. tm.begin();
  6. final Transaction transaction = tm.suspend();
  7. SimpleXid gtid = SimpleXid.of(getXid(transaction)).withoutBranch();
  8. known.put(gtid, getEntryFor(transaction, gtid));
  9. } catch (Throwable t) {
  10. if (suspended != null) try {
  11. tm.resume(suspended);
  12. } catch (InvalidTransactionException e) {
  13. e.addSuppressed(t);

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

  1. @Test
  2. public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
  3. TransactionManager tm = mock(TransactionManager.class);
  4. Transaction tx = mock(Transaction.class);
  5. given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
  6. given(tm.suspend()).willReturn(tx);
  7. JtaTransactionManager ptm = newJtaTransactionManager(tm);
  8. TransactionTemplate tt = new TransactionTemplate(ptm);
  9. tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
  10. assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
  11. tt.execute(new TransactionCallbackWithoutResult() {
  12. @Override
  13. protected void doInTransactionWithoutResult(TransactionStatus status) {
  14. assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
  15. }
  16. });
  17. assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
  18. verify(tm).begin();
  19. verify(tm).commit();
  20. verify(tm).resume(tx);
  21. }

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

  1. public void testPutIfAbsent() throws Throwable {
  2. Object k1 = getKeyForCache(0);
  3. tm(0).begin();
  4. assertNull(cache(0).putIfAbsent(k1, "v1"));
  5. Transaction suspendedTx = tm(0).suspend();
  6. cache(0).put(k1, "v2");
  7. assertEquals(cache(0).get(k1), "v2");
  8. assertEquals(cache(1).get(k1), "v2");
  9. suspendedTx.commit();
  10. assertEquals("v1", cache(0).get(k1));
  11. assertEquals("v1", cache(1).get(k1));
  12. }

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

  1. @Test (expectedExceptions = TimeoutException.class)
  2. public void testMultiLockFailure() throws Exception {
  3. Cache<String, String> cache1 = cache(0), cache2 = cache(1);
  4. cache1.put("k1", "v");
  5. cache1.put("k2", "v");
  6. cache1.put("k3", "v");
  7. tm(1).begin();
  8. cache2.put("k3", "v2");
  9. tm(1).suspend();
  10. tm(0).begin();
  11. cache1.getAdvancedCache().lock(Arrays.asList("k1", "k2", "k3"));
  12. tm(0).rollback();
  13. }

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

  1. public void testStaleLock() throws SystemException, NotSupportedException {
  2. c1.put("k", "v");
  3. assert c1.get("k").equals("v");
  4. assert c2.get("k").equals("v");
  5. TransactionManager tm = TestingUtil.getTransactionManager(c1);
  6. tm.begin();
  7. c1.getAdvancedCache().lock("k");
  8. tm.suspend();
  9. // test that both c1 and c2 have locked k
  10. assertLocked(c1, "k");
  11. assertLocked(c2, "k");
  12. cacheManagers.get(0).stop();
  13. TestingUtil.blockUntilViewReceived(c2, 1);
  14. EmbeddedCacheManager cacheManager = c2.getCacheManager();
  15. assert cacheManager.getMembers().size() == 1;
  16. // may take a while from when the view change is seen through to when the lock is cleared
  17. TestingUtil.sleepThread(1000);
  18. assertNotLocked(c2, "k");
  19. }

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

  1. @Test
  2. public void test() throws Exception
  3. {
  4. TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
  5. tm.begin();
  6. Transaction tx = tm.suspend();
  7. tm.begin();
  8. tx.commit();
  9. tm.commit();
  10. }

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

  1. public void testSimpleReadOnlTx() throws Exception {
  2. tm().begin();
  3. assert cache.get("k") == null;
  4. Transaction transaction = tm().suspend();
  5. LocalXaTransaction localTransaction = (LocalXaTransaction) txTable().getLocalTransaction(transaction);
  6. assert localTransaction != null && localTransaction.isReadOnly();
  7. }

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

  1. public void testNotROWhenHasWrites() throws Exception {
  2. tm().begin();
  3. cache.put("k", "v");
  4. assert TestingUtil.extractLockManager(cache).isLocked("k");
  5. Transaction transaction = tm().suspend();
  6. LocalXaTransaction localTransaction = (LocalXaTransaction) txTable().getLocalTransaction(transaction);
  7. assert localTransaction != null && !localTransaction.isReadOnly();
  8. }

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

  1. assertEquals( 0, sessionFactory().getStatistics().getEntityLoadCount() );
  2. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  3. Session s = openSession();
  4. Map foo = new HashMap();
  5. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  6. Session s1 = openSession();
  7. foo = ( Map ) s1.get( "Item", "Foo" );
  8. Transaction tx = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend();
  9. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  10. Session s2 = openSession();
  11. foo = ( Map ) s2.get( "Item", "Foo" );
  12. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  13. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx );
  14. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  15. tx = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend();
  16. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  17. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  18. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx );
  19. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();

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

  1. public void testReplace2() throws Throwable {
  2. Object k1 = getKeyForCache(0);
  3. cache(0).put(k1, "v1");
  4. tm(0).begin();
  5. assertEquals("v1", cache(0).replace(k1, "v2"));
  6. Transaction suspendedTx = tm(0).suspend();
  7. cache(0).put(k1, "v3");
  8. assertEquals(cache(0).get(k1), "v3");
  9. assertEquals(cache(1).get(k1), "v3");
  10. suspendedTx.commit();
  11. assertEquals("v2", cache(0).get(k1));
  12. assertEquals("v2", cache(1).get(k1));
  13. }

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

  1. public void testSilentMultiLockFailure() throws Exception {
  2. Cache<String, String> cache1 = cache(0), cache2 = cache(1);
  3. cache1.put("k1", "v");
  4. cache1.put("k2", "v");
  5. cache1.put("k3", "v");
  6. tm(1).begin();
  7. cache2.put("k3", "v2");
  8. tm(1).suspend();
  9. tm(0).begin();
  10. assert !cache1.getAdvancedCache().withFlags(FAIL_SILENTLY).lock(Arrays.asList("k1", "k2", "k3"));
  11. tm(0).rollback();
  12. }

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

  1. transactionManager.resume(tx);
  2. if (tx != null) {
  3. if (transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION) {
  4. transactionManager.suspend();

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

  1. @Test
  2. public void testTxHandleCommitKeepsThreadAssociation() throws Exception {
  3. // don't wait for ages, has to be set before TX is begun
  4. tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
  5. tm.begin();
  6. Transaction tx = tm.getTransaction();
  7. assertNotNull("Transaction should have started.", tx);
  8. assertEquals("TX should have been active", Status.STATUS_ACTIVE, tx.getStatus());
  9. tx.commit();
  10. tx = tm.getTransaction();
  11. assertNotNull("Committing via TX handle should NOT disassociated TX from the current thread.", tx);
  12. assertEquals("TX status should have been COMMITTED.", Status.STATUS_COMMITTED, tx.getStatus());
  13. // Remove the TX-thread association. The only public API to achieve it is suspend(),
  14. // technically we never resume the same transaction (TX forget).
  15. Transaction suspended = tm.suspend();
  16. assertTrue("Wrong TX suspended?.", suspended.equals(tx));
  17. assertNull("TX should've been disassociated from the thread.", tm.getTransaction());
  18. // should be no-op and never fail
  19. tm.resume(null);
  20. // ensure we don't have any TX-Thread association lurking around a main thread
  21. assertNull(tm.getTransaction());
  22. }

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

  1. @Override
  2. public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException {
  3. // JCA 1.6 FR 13.5.6
  4. // The application server must set the thread context class loader to the endpoint
  5. // application class loader during the beforeDelivery call.
  6. previousClassLoader = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(getApplicationClassLoader());
  7. try {
  8. final TransactionManager tm = getTransactionManager();
  9. // TODO: in violation of JCA 1.6 FR 13.5.9?
  10. previousTx = tm.suspend();
  11. boolean isTransacted = service.isDeliveryTransacted(method);
  12. if (isTransacted) {
  13. tm.begin();
  14. currentTx = tm.getTransaction();
  15. if (xaRes != null)
  16. currentTx.enlistResource(xaRes);
  17. }
  18. } catch (Throwable t) {
  19. throw new ApplicationServerInternalException(t);
  20. } finally {
  21. WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(previousClassLoader);
  22. }
  23. }

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

  1. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  2. Session s = openSession();
  3. Map foo = new HashMap();
  4. bar.put( "description", "a small bar" );
  5. s.persist( "Item", bar );
  6. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  7. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  8. Session s4 = openSession();
  9. Transaction tx4 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend();
  10. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
  11. Session s1 = openSession();
  12. List r1 = s1.createCriteria( "Item" ).addOrder( Order.asc( "description" ) )
  13. Transaction tx1 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend();
  14. assertEquals( r2.size(), 2 );
  15. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  16. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 );
  17. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
  18. TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx4 );
  19. List r4 = s4.createCriteria( "Item" ).addOrder( Order.asc( "description" ) )
  20. .setCacheable( true ).list();

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

  1. public void testConditionalRemove() throws Throwable {
  2. Object k1 = getKeyForCache(0);
  3. cache(0).put(k1, "v1");
  4. tm(0).begin();
  5. assertTrue(cache(0).remove(k1, "v1"));
  6. Transaction suspendedTx = tm(0).suspend();
  7. cache(0).put(k1, "v2");
  8. assertEquals(cache(0).get(k1), "v2");
  9. assertEquals(cache(1).get(k1), "v2");
  10. log.trace("here it is");
  11. suspendedTx.commit();
  12. assertNull(cache(0).get(k1));
  13. assertNull(cache(1).get(k1));
  14. }
  15. }

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

  1. @Test (expectedExceptions = TimeoutException.class)
  2. public void testLockFailure() throws Exception {
  3. Cache<String, String> cache1 = cache(0), cache2 = cache(1);
  4. cache1.put("k", "v");
  5. tm(1).begin();
  6. cache2.put("k", "v2");
  7. tm(1).suspend();
  8. tm(0).begin();
  9. cache1.getAdvancedCache().lock("k");
  10. tm(0).rollback();
  11. }

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

  1. private <T> T doInSuspendedTransaction(HibernateCallable<T> callable) {
  2. try {
  3. Transaction surroundingTransaction = transactionManager.suspend();
  4. LOG.debugf( "Surrounding JTA transaction suspended [%s]", surroundingTransaction );
  5. transactionManager.resume( surroundingTransaction );
  6. LOG.debugf( "Surrounding JTA transaction resumed [%s]", surroundingTransaction );

相关文章