@Override public void registerSynchronization(Synchronization sync) throws RollbackException, SystemException { this.transactionManager.getTransaction().registerSynchronization(sync); }
int getStatus(Transaction transaction) { try { return transaction.getStatus(); } catch (SystemException e) { return Status.STATUS_UNKNOWN; } }
@Override public boolean enlistResource(XAResource xaRes) throws RollbackException, SystemException { return this.transactionManager.getTransaction().enlistResource(xaRes); }
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(); }
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"); } }
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);
try { final Transaction transaction = tm.getTransaction(); if (transaction != null && transaction.getStatus() == Status.STATUS_ACTIVE) { throw new SQLException(e);
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(); } }
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();
/** * 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); } }
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; }
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());
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(); }
/** * 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); } }
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(); } }
@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); } }
@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(); } }
@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(); } }
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();