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

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

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

Transaction介绍

[英]The Transaction interface allows operations to be performed on transactions.
[中]事务接口允许对事务执行操作。

代码示例

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

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

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

int getStatus(Transaction transaction) {
  try {
    return transaction.getStatus();
  } catch (SystemException e) {
    return Status.STATUS_UNKNOWN;
  }
}

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

@Override
public boolean enlistResource(XAResource xaRes) throws RollbackException, SystemException {
  return this.transactionManager.getTransaction().enlistResource(xaRes);
}

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

public void endTransaction() throws Exception {
    if (this.transaction != null) {
      try {
        if (this.rollbackOnly) {
          this.transaction.rollback();
        }
        else {
          this.transaction.commit();
        }
      }
      finally {
        this.transaction = null;
        this.rollbackOnly = false;
      }
    }
  }
}

代码示例来源:origin: stackoverflow.com

final Session session = HibernateUtil.getSession();
try {
 final Transaction transaction = session.beginTransaction();
 try {
  // The real work is here
  transaction.commit();
 } catch (Exception ex) {
  // Log the exception here
  transaction.rollback();
  throw ex;
 }
} finally {
 HibernatilUtil.closeSession();
}

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

public void rollback() {
 // managed transaction, mark rollback-only if not done so already.
 try {
  Transaction transaction = getTransaction();
  int status = transaction.getStatus();
  if (status != Status.STATUS_NO_TRANSACTION && status != Status.STATUS_ROLLEDBACK) {
   transaction.setRollbackOnly();
  }
 } catch (IllegalStateException e) {
  throw new ActivitiException("Unexpected IllegalStateException while marking transaction rollback only");
 } catch (SystemException e) {
  throw new ActivitiException("SystemException while marking transaction rollback only");
 }
}

代码示例来源:origin: org.apache.openjpa/openjpa-all

Transaction transaction = null;
try {
  transaction = transactionManager.getTransaction();
  int status = transaction.getStatus();
  if (status != Status.STATUS_ACTIVE && status != Status.STATUS_MARKED_ROLLBACK) {
    return null;
  throw (SQLException) new SQLException("Unable to determine current transaction ").initCause(e);

代码示例来源:origin: stackoverflow.com

try {
  final Transaction transaction = tm.getTransaction();
  if (transaction != null && transaction.getStatus() == Status.STATUS_ACTIVE) {
  throw new SQLException(e);

代码示例来源:origin: stackoverflow.com

public void save(Entity e) {
  SessionFactory sf = HibernateUtil.getSessionFactory();
  Session session = sf.openSession();
  Transaction tx = session.beginTransaction();
  try {
     session.save(e);
     tx.commit();
  } catch (HIbernateException he) {
    tx.rollback();
  } finally {
    session.close();
  }
}

代码示例来源:origin: stackoverflow.com

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for  ( int i=0; i<888888; i++ ) {
 TableA record = new TableA();
  record.setXXXX();
  session.save(record)
  if ( i % 50 == 0 ) { //50, same as the JDBC batch size
    //flush a batch of inserts and release memory:
    session.flush();
    session.clear();
  }
}
tx.commit();
session.close();

代码示例来源:origin: org.apache.openjpa/openjpa-all

/**
 * True if the transaction is active or marked for rollback only.
 * @return true if the transaction is active or marked for rollback only; false otherwise
 * @throws SQLException if a problem occurs obtaining the transaction status
 */
public boolean isActive() throws SQLException {
  try {
    Transaction transaction = (Transaction) this.transactionRef.get();
    if (transaction == null) {
      return false;
    }
    int status = transaction.getStatus();
    return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
  } catch (SystemException e) {
    throw (SQLException) new SQLException("Unable to get transaction status").initCause(e);
  }
}

代码示例来源:origin: stackoverflow.com

public TestType getTestTypeById(int idTestType) {
  Session session = sf.openSession(); // sf as a sessionFactory instance
  Transaction tx = null;
  TestType testType = null;
  try {
    tx = session.beginTransaction();
    testType = (TestType) session.get(TestType.class, idTestType);
    tx.commit();
  } catch (HibernateException e) {
    if (tx != null)
      tx.rollback();
    e.printStackTrace();
  } finally {
    session.close();
  }

  return testType;
}

代码示例来源:origin: org.codehaus.service-conduit/sca4j-xapool

final Transaction transaction = transactionManager.getTransaction();
    connection = new TransactedConnection(xaConnection);
    connectionCache.put(transaction, connection);
    transaction.registerSynchronization(new Synchronization() {
      public void afterCompletion(int status) {
        TransactedConnection connection = connectionCache.get(transaction);
  throw new SQLException(e.getMessage());
} catch (RollbackException e) {
  throw new SQLException(e.getMessage());

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

protected void startTx() throws Exception {
  tm(cache).begin();
  cache.put(key(), "value");
  tx = tm(cache).getTransaction();
  tx.enlistResource(new XAResourceAdapter()); // this is to force 2PC and to prevent transaction managers attempting to optimise the call to a 1PC.
  txsStarted.countDown();
}

代码示例来源:origin: org.apache.openjpa/openjpa-all

/**
 * Adds a listener for transaction completion events.
 *
 * @param listener the listener to add
 * @throws SQLException if a problem occurs adding the listener to the transaction
 */
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
  try {
    getTransaction().registerSynchronization(new Synchronization() {
      public void beforeCompletion() {
      }
      public void afterCompletion(int status) {
        listener.afterCompletion(TransactionContext.this, status == Status.STATUS_COMMITTED);
      }
    });
  } catch (RollbackException e) {
    // JTA spec doesn't let us register with a transaction marked rollback only
    // just ignore this and the tx state will be cleared another way.
  } catch (Exception e) {
    throw (SQLException) new SQLException("Unable to register transaction context listener").initCause(e);
  }
}

代码示例来源:origin: stackoverflow.com

public void addGrade(Grade grade) {
 Session session = sessionFactory.getCurrentSession()
  Transaction tx = null;
  try{
  tx = session.beginTransaction();
  session.save(grade);   
  tx.commit();
   }catch (HibernateException e) {
    if (tx!=null) tx.rollback();
    e.printStackTrace(); 
  }finally {
    session.close(); 
  }
  }

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

@Override
public Connection getConnection() throws SQLException {
  Transaction currentTransaction = findCurrentTransaction();
  try {
    if ( currentTransaction == null ) {
      // this block handles non enlisted connections ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Connection connection = delegate.getConnection();
      nonEnlistedConnections.add( connection );
      return connection;
    }
    // this portion handles enlisted connections ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Connection connection = (Connection) TestingJtaPlatformImpl.synchronizationRegistry().getResource(
        CONNECTION_KEY
    );
    if ( connection == null ) {
      connection = delegate.getConnection();
      TestingJtaPlatformImpl.synchronizationRegistry().putResource( CONNECTION_KEY, connection );
      XAResourceWrapper xaResourceWrapper = new XAResourceWrapper( this, connection );
      currentTransaction.enlistResource( xaResourceWrapper );
    }
    return connection;
  }
  catch (SQLException e) {
    throw e;
  }
  catch (Exception e) {
    throw new SQLException(e);
  }
}

代码示例来源: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();

    assertEquals(Status.STATUS_ACTIVE, theTransaction.getStatus());

    theTransaction.rollback();

    assertEquals(Status.STATUS_ROLLEDBACK, theTransaction.getStatus());

    assertEquals(Status.STATUS_ROLLEDBACK, tm.getStatus());

    theTransaction = tm.suspend();

    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());

    tm.resume(theTransaction);

    assertEquals(Status.STATUS_ROLLEDBACK, tm.getStatus());

    tm.suspend();
  }
}

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

final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
if ( transactionManager == null ) {
  throw new HibernateException( "No TransactionManagerLookup specified" );
  txn = transactionManager.getTransaction();
  if ( txn == null ) {
    throw new HibernateException( "Unable to locate current JTA transaction" );
  if ( !JtaStatusHelper.isActive( txn.getStatus() ) ) {
    throw new HibernateException( "Current transaction is not in progress" );
    txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
      currentSession.close();

相关文章