public Transaction persistent(Object status){ TransactionId tID=TransactionId.get(); String transactionKey=tID.getTransactionKey(); //将事务状态放入集合 List<Transaction> transactions=_TRANSACTION_HASH.containsKey(transactionKey)?_TRANSACTION_HASH.get(transactionKey):null; if(null==transactions) { STATUS_LOCK.writeLock().lock(); transactions = null == transactions && _TRANSACTION_HASH.containsKey(transactionKey) ? _TRANSACTION_HASH.get(transactionKey) : transactions; if (null == transactions) { transactions = new CopyOnWriteArrayList<Transaction>(); _TRANSACTION_HASH.put(transactionKey,transactions); _TRANSACTION_HOLD_TIMES.put(transactionKey,System.currentTimeMillis()); } STATUS_LOCK.writeLock().unlock(); } Transaction t=DBTransaction.createDbTransaction((DefaultTransactionStatus) status); transactions.add(t); return t; }
@Override protected void doRollback(DefaultTransactionStatus status) { TransactionContext transactionContext = getTransactionContext(); long txId = getTransactionId(); if (transactionContext == null) { logger.error(" [LITX] litx transaction has not been started. [TX ID: " + txId + "]"); throw new RuntimeException("LITX: litx transaction has not been started. [TX ID: " + txId + "]"); } if (transactionContext.isNeedRollback()) { logger.info(" [LITX]开始回滚事务 [TX ID: " + txId + "]"); try { transactionContext.doRollback(); }catch(Exception e){ logger.error(" [LITX]回滚异常", e); }finally { logger.info(" [LITX]回滚事务完成 [TX ID: " + txId + "]"); } } super.doRollback(status); }
@Override protected void doRollback(DefaultTransactionStatus status) { JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Rolling back JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]"); } try { EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction(); if (tx.isActive()) { tx.rollback(); } } catch (PersistenceException ex) { throw new TransactionSystemException("Could not roll back JPA transaction", ex); } finally { if (!txObject.isNewEntityManagerHolder()) { // Clear all pending inserts/updates/deletes in the EntityManager. // Necessary for pre-bound EntityManagers, to avoid inconsistent state. txObject.getEntityManagerHolder().getEntityManager().clear(); } } }
@Override protected void doCommit(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Committing JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); tx.commit(); } catch (JDOException ex) { // Assumably failed to flush changes to database. throw convertJdoAccessException(ex); } }
@Override protected void doRollback(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Rolling back JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.rollback(); } } catch (JDOException ex) { throw new TransactionSystemException("Could not roll back JDO transaction", ex); } }
@Override protected void prepareForCommit(DefaultTransactionStatus status) { if (this.earlyFlushBeforeCommit && status.isNewTransaction()) { HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); Session session = txObject.getSessionHolder().getSession(); if (!session.getFlushMode().lessThan(FlushMode.COMMIT)) { logger.debug("Performing an early flush for Hibernate transaction"); try { session.flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } finally { session.setFlushMode(FlushMode.MANUAL); } } } }
@Test public void noExistingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(false, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertTrue("Must not have transaction", status1.getTransaction() == null); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.getTransaction() != null); assertTrue("Must be new transaction", status2.isNewTransaction()); try { tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); fail("Should not have thrown NoTransactionException"); } catch (IllegalTransactionStateException ex) { // expected } }
@Test public void existingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(true, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertTrue("Must have transaction", status1.getTransaction() != null); assertTrue("Must not be new transaction", !status1.isNewTransaction()); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.getTransaction() != null); assertTrue("Must not be new transaction", !status2.isNewTransaction()); try { DefaultTransactionStatus status3 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); assertTrue("Must have transaction", status3.getTransaction() != null); assertTrue("Must not be new transaction", !status3.isNewTransaction()); } catch (NoTransactionException ex) { fail("Should not have thrown NoTransactionException"); } }
public static <T> AsyncFutureListener<T> wrapIfNeeded(AsyncFutureListener<T> listener, GigaSpace gigaSpace) { DefaultTransactionProvider txProvider = (DefaultTransactionProvider) gigaSpace.getTxProvider(); JiniTransactionHolder holder = txProvider.getHolder(); PlatformTransactionManager transactionManager = txProvider.getTransactionManager(); if (holder == null || transactionManager == null) { // just wrap for exception translation return new InternalAsyncFutureListener<T>(gigaSpace, listener, null, transactionManager, holder); } // here, we create a dummy transaction status (with its new transaction set to true, so the commit/roolback // process will be performed). We also increase the ref count of the transaction, so only the last one will // be performed AbstractJiniTransactionManager.JiniTransactionObject jiniTransactionObject = new AbstractJiniTransactionManager.JiniTransactionObject(); jiniTransactionObject.setJiniHolder(holder, false); TransactionStatus txStatus = new DefaultTransactionStatus(jiniTransactionObject, true, false, false, false, null); holder.incRef(); return new InternalAsyncFutureListener<T>(gigaSpace, listener, txStatus, transactionManager, holder); }
/** * @see org.springframework.transaction.interceptor.TransactionAspectSupport#doCommitTransactionAfterReturning(org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo) */ @Override protected void commitTransactionAfterReturning(TransactionInfo txInfo) { // using INFO level since DEBUG level turns on the (somewhat misleading) log statements of the superclass if (logger.isDebugEnabled()) { if (txInfo != null) { TransactionStatus txStatus = txInfo.getTransactionStatus(); if (txStatus != null) { if (txStatus.isNewTransaction()) { LOG.debug("committing explicit transaction for " + txInfo.getJoinpointIdentification()); } else { if (txStatus instanceof DefaultTransactionStatus) { DefaultTransactionStatus dtxStatus = (DefaultTransactionStatus) txStatus; if (dtxStatus.isNewSynchronization()) { LOG.debug("committing implicit transaction for " + txInfo.getJoinpointIdentification()); } } } } } } super.commitTransactionAfterReturning(txInfo); }
@Override protected void doCommit(DefaultTransactionStatus status) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Committing JDBC transaction on Connection [" + con + "]"); } try { con.commit(); } catch (SQLException ex) { throw new TransactionSystemException("Could not commit JDBC transaction", ex); } /**modify by guosg 这里提交ldtm事务*/ SynchronizationManager.getCurrentUserTransation().commit(); }
@Override protected void doRollback(DefaultTransactionStatus status) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Rolling back JDBC transaction on Connection [" + con + "]"); } try { con.rollback(); } catch (SQLException ex) { throw new TransactionSystemException("Could not roll back JDBC transaction", ex); } /**modify by guosg 这里提交回滚事务*/ SynchronizationManager.getCurrentUserTransation().rollback(); }
public void testNoExistingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(false, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertTrue("Must not have transaction", status1.getTransaction() == null); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.getTransaction() != null); assertTrue("Must be new transaction", status2.isNewTransaction()); try { tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); fail("Should not have thrown NoTransactionException"); } catch (IllegalTransactionStateException ex) { // expected } }
public void testExistingTransaction() { PlatformTransactionManager tm = new TestTransactionManager(true, true); DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)); assertTrue("Must have transaction", status1.getTransaction() != null); assertTrue("Must not be new transaction", !status1.isNewTransaction()); DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); assertTrue("Must have transaction", status2.getTransaction() != null); assertTrue("Must not be new transaction", !status2.isNewTransaction()); try { DefaultTransactionStatus status3 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)); assertTrue("Must have transaction", status3.getTransaction() != null); assertTrue("Must not be new transaction", !status3.isNewTransaction()); } catch (NoTransactionException ex) { fail("Should not have thrown NoTransactionException"); } }
/** * Define how message exchanges are processed. * * @param exchange the InOut message exchange * @throws FcrepoOperationFailedException when the underlying HTTP request results in an error */ @Override public void process(final Exchange exchange) throws FcrepoOperationFailedException { if (exchange.isTransacted()) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(final TransactionStatus status) { final DefaultTransactionStatus st = (DefaultTransactionStatus)status; final FcrepoTransactionObject tx = (FcrepoTransactionObject)st.getTransaction(); try { doRequest(exchange, tx.getSessionId()); } catch (FcrepoOperationFailedException ex) { throw new TransactionSystemException( "Error executing fcrepo request in transaction: ", ex); } } }); } else { doRequest(exchange, null); } }
@Override protected void doCommit(final DefaultTransactionStatus status) { final FcrepoTransactionObject tx = (FcrepoTransactionObject)status.getTransaction(); final InputStream is = null; final String contentType = null; try { getClient().post(URI.create(baseUrl + "/" + tx.getSessionId() + COMMIT)) .body(is, contentType).perform(); } catch (final FcrepoOperationFailedException ex) { LOGGER.debug("Transaction commit failed: ", ex); throw new TransactionSystemException("Could not commit fcrepo transaction"); } finally { tx.setSessionId(null); } }
@Override protected void doRollback(DefaultTransactionStatus status) { try{ TransactionException rollbackException = null; try{ super.doRollback(status); }catch(TransactionException te){ rollbackException = te; } // 保证触发资源回滚 triggerRollback(status); if(rollbackException != null){ throw rollbackException; } }finally{ cleanup(); } }
@Override protected void doCommit(DefaultTransactionStatus status) { JcrTransactionObject txObject = (JcrTransactionObject) status .getTransaction(); if (status.isDebug()) { LOG.debug("Committing JCR transaction on session [" + txObject.getSessionHolder().getSession() + "]"); } try { txObject.getSessionHolder().getTransaction().commit(); } catch (Exception ex) { // assumably from commit call to the underlying JCR repository throw new TransactionSystemException( "Could not commit JCR transaction", ex); } }
@Override protected void doRollback(DefaultTransactionStatus status) { JcrTransactionObject txObject = (JcrTransactionObject) status .getTransaction(); if (status.isDebug()) { LOG.debug("Rolling back JCR transaction on session [" + txObject.getSessionHolder().getSession() + "]"); } try { txObject.getSessionHolder().getTransaction().rollback(); } catch (Exception ex) { throw new TransactionSystemException( "Could not roll back JCR transaction", ex); } finally { if (!txObject.isNewSessionHolder()) { // Clear all pending inserts/updates/deletes in the Session. // Necessary for pre-bound Sessions, to avoid inconsistent // state. try { txObject.getSessionHolder().getSession().refresh(false); } catch (RepositoryException e) { // we already throw an exception (hold back this one). } } } }
@Override protected void doRollback(DefaultTransactionStatus status) { MolgenisTransaction transaction = (MolgenisTransaction) status.getTransaction(); if (LOG.isDebugEnabled()) { LOG.debug("Rollback transaction [{}]", transaction.getId()); } DefaultTransactionStatus jpaTransactionStatus = new DefaultTransactionStatus( transaction.getDataSourceTransaction(), status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources()); if (!status.isReadOnly()) { transactionListeners.forEach(j -> j.rollbackTransaction(transaction.getId())); } super.doRollback(jpaTransactionStatus); }
@Override protected void doCommit(DefaultTransactionStatus status) { String prefix=""; if(logger.isDebugEnabled()) { prefix += "[" + TransactionId.get().getCurrentId() + "]"; try { Connection connection = ((JdbcTransactionObjectSupport)status.getTransaction()).getConnectionHolder().getConnection(); prefix += "[" + (null != connection ? connection : "") + "]"; } catch (Exception e) {} } logger.debug(prefix+"DataSource doCommit"); //将status持久化到jvm内存 if(null!=TransactionContextFactory.getInstance()&& TransactionSupport.isProxy()){ logger.debug(prefix+"DataSource doCommit before store"); Transaction transaction=TransactionContextFactory.getInstance().persistent(status); if(transaction instanceof DBTransaction){ logger.debug(prefix+"DataSource doCommit in store"); DBTransaction dbT=(DBTransaction)transaction; dbT.setDataSource(getDataSource()); dbT.setIsNewConnectionHolder(checkNewConnectionHolder(status.getTransaction())); } }else{//如果事务容器工厂类初始化失败,执行默认逻辑 logger.debug(prefix+"DataSource doCommit don't store"); super.doCommit(status); super.doCleanupAfterCompletion(status.getTransaction()); } }
@Override public void run() { DefaultTransactionStatus status = prepareTransactionStatus( this.definition, (this.actualTransaction ? this : null), this.newTransaction, this.newSynchronization, this.debug, null); try { this.result = this.callback.doInTransaction(status); triggerBeforeCommit(status); } catch (Throwable ex) { this.exception = ex; uowManager.setRollbackOnly(); } finally { if (status.isLocalRollbackOnly()) { if (status.isDebug()) { logger.debug("Transactional code has requested rollback"); } uowManager.setRollbackOnly(); } triggerBeforeCompletion(status); if (status.isNewSynchronization()) { List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations(); TransactionSynchronizationManager.clear(); if (!synchronizations.isEmpty()) { uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations)); } } } }
@Override protected void doCommit(DefaultTransactionStatus status) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Committing JDBC transaction on Connection [" + con + "]"); } try { con.commit(); } catch (SQLException ex) { throw new TransactionSystemException("Could not commit JDBC transaction", ex); } }
@Override protected void doRollback(DefaultTransactionStatus status) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { logger.debug("Rolling back JDBC transaction on Connection [" + con + "]"); } try { con.rollback(); } catch (SQLException ex) { throw new TransactionSystemException("Could not roll back JDBC transaction", ex); } }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting JDBC transaction [" + txObject.getConnectionHolder().getConnection() + "] rollback-only"); } txObject.setRollbackOnly(); }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting CCI local transaction [" + txObject.getConnectionHolder().getConnection() + "] rollback-only"); } txObject.getConnectionHolder().setRollbackOnly(); }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "] rollback-only"); } txObject.setRollbackOnly(); }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "] rollback-only"); } txObject.setRollbackOnly(); }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting Hibernate transaction on Session [" + txObject.getSessionHolder().getSession() + "] rollback-only"); } txObject.setRollbackOnly(); }
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Setting Hibernate transaction on Session [" + SessionFactoryUtils.toString(txObject.getSessionHolder().getSession()) + "] rollback-only"); } txObject.setRollbackOnly(); }