Java Code Examples for org.springframework.transaction.TransactionStatus#isNewTransaction()

The following examples show how to use org.springframework.transaction.TransactionStatus#isNewTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KualiTransactionInterceptor.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example 2
Source File: TransactionalMethodInterceptor.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
  DataSourceTransactionManager transactionManager =
      transactionManagerProvider.get();

  DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
  TransactionStatus transaction = transactionManager.getTransaction(
      transactionDefinition);

  try {
    Object result = invocation.proceed();

    try {
      if (transaction.isNewTransaction()) {
        transactionManager.commit(transaction);
      }
    } catch (UnexpectedRollbackException ignore) {
    }

    return result;
  } catch (Exception e) {
    if (transaction.isNewTransaction()) {
      transactionManager.rollback(transaction);
    }

    throw e;
  }
}
 
Example 3
Source File: RepositoryManagerImpl.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"Finally", "PMD.DoNotThrowExceptionInFinally"})
public <T> T runInTransaction(boolean rollbackOnly, Task<T> callable) {
    TransactionStatus transactionStatus = platformTransactionManager.getTransaction(NEW_TRANSACTION_DEFINITION);

    boolean shouldRollback = rollbackOnly;
    if (transactionStatus.isNewTransaction()) {
        if (rollbackOnly) {
            transactionStatus.setRollbackOnly();
        }
    } else {
        //there is a surrounding txn, so we can't set the rollback only flag
        shouldRollback = false;
    }
    String txnName = null;
    if (LOGGER.isDebugEnabled()) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        txnName = stackTraceElements[2].getMethodName();
        LOGGER.debug( "createTransaction:created '%s', rollbackOnly = '%b'", txnName, shouldRollback ); //$NON-NLS-1$
    }
    try {
        return callable.call();
    } finally {
        try {
            platformTransactionManager.commit(transactionStatus);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug( "transaction ended '%s'", //$NON-NLS-1$
                        txnName);
            }
        } catch (TransactionTimedOutException e) {
            throw new TimeoutException(e);
        }
    }
}
 
Example 4
Source File: AbstractPlatformTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation of commit handles participating in existing
 * transactions and programmatic rollback requests.
 * Delegates to {@code isRollbackOnly}, {@code doCommit}
 * and {@code rollback}.
 * @see org.springframework.transaction.TransactionStatus#isRollbackOnly()
 * @see #doCommit
 * @see #rollback
 */
@Override
public final void commit(TransactionStatus status) throws TransactionException {
	if (status.isCompleted()) {
		throw new IllegalTransactionStateException(
				"Transaction is already completed - do not call commit or rollback more than once per transaction");
	}

	DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
	if (defStatus.isLocalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Transactional code has requested rollback");
		}
		processRollback(defStatus);
		return;
	}
	if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
		}
		processRollback(defStatus);
		// Throw UnexpectedRollbackException only at outermost transaction boundary
		// or if explicitly asked to.
		if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
		return;
	}

	processCommit(defStatus);
}
 
Example 5
Source File: AbstractPlatformTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation of commit handles participating in existing
 * transactions and programmatic rollback requests.
 * Delegates to {@code isRollbackOnly}, {@code doCommit}
 * and {@code rollback}.
 * @see org.springframework.transaction.TransactionStatus#isRollbackOnly()
 * @see #doCommit
 * @see #rollback
 */
@Override
public final void commit(TransactionStatus status) throws TransactionException {
	if (status.isCompleted()) {
		throw new IllegalTransactionStateException(
				"Transaction is already completed - do not call commit or rollback more than once per transaction");
	}

	DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
	if (defStatus.isLocalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Transactional code has requested rollback");
		}
		processRollback(defStatus);
		return;
	}
	if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
		}
		processRollback(defStatus);
		// Throw UnexpectedRollbackException only at outermost transaction boundary
		// or if explicitly asked to.
		if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
		return;
	}

	processCommit(defStatus);
}
 
Example 6
Source File: KualiTransactionInterceptor.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
    * @see org.springframework.transaction.interceptor.TransactionAspectSupport#createTransactionIfNecessary(java.lang.reflect.Method,
    *      java.lang.Class)
    */
@Override
   protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {
       TransactionInfo txInfo = super.createTransactionIfNecessary(method, targetClass);

       // 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("creating explicit transaction for " + txInfo.getJoinpointIdentification());
                   }
                   else {
                       if (txStatus instanceof DefaultTransactionStatus) {
                           DefaultTransactionStatus dtxStatus = (DefaultTransactionStatus) txStatus;

                           if (dtxStatus.isNewSynchronization()) {
                               LOG.debug("creating implicit transaction for " + txInfo.getJoinpointIdentification());
                           }
                       }
                   }
               }
           }
       }

       return txInfo;
   }
 
Example 7
Source File: KualiTransactionInterceptor.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
    * @see org.springframework.transaction.interceptor.TransactionAspectSupport#doCloseTransactionAfterThrowing(org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo,
    *      java.lang.Throwable)
    */
@Override
   protected void completeTransactionAfterThrowing(TransactionInfo txInfo, Throwable ex) {
       if (txInfo.getTransactionAttribute().rollbackOn(ex)) {
           LOG.fatal("Exception caught by Transaction Interceptor, this will cause a rollback at the end of the transaction.", ex);
       }

       // 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("closing explicit transaction for " + txInfo.getJoinpointIdentification());
                   }
                   else {
                       if (txStatus instanceof DefaultTransactionStatus) {
                           DefaultTransactionStatus dtxStatus = (DefaultTransactionStatus) txStatus;

                           if (dtxStatus.isNewSynchronization()) {
                               LOG.debug("closing implicit transaction for " + txInfo.getJoinpointIdentification());
                           }
                       }
                   }
               }
           }
       }

       super.completeTransactionAfterThrowing(txInfo, ex);
   }
 
Example 8
Source File: JtaUtil.java    From iaf with Apache License 2.0 4 votes vote down vote up
public static String displayTransactionStatus(TransactionStatus txStatus) {
	String result;
	result="txName ["+TransactionSynchronizationManager.getCurrentTransactionName()+"]";
	if (txStatus!=null) {
		result+=" status new ["+txStatus.isNewTransaction()+"]";
		result+=" status completeted ["+txStatus.isCompleted()+"]";
		result+=" status rollbackOnly ["+txStatus.isRollbackOnly()+"]";
		result+=" status hasSavepoint ["+txStatus.hasSavepoint()+"]";
	} else {
		result+=" currently not in a transaction";
	}
	result+=" isolation ["+TransactionSynchronizationManager.getCurrentTransactionIsolationLevel()+"]";
	result+=" active ["+TransactionSynchronizationManager.isActualTransactionActive()+"]";
	boolean syncActive=TransactionSynchronizationManager.isSynchronizationActive();
	result+=" synchronization active ["+syncActive+"]";
	result+="\n";
	Map<Object, Object> resources = TransactionSynchronizationManager.getResourceMap();
	result += "resources:\n";
	if (resources==null) {
		result+="  map is null\n";
	} else {
		for (Iterator<Object> it=resources.keySet().iterator(); it.hasNext();) {
			Object key = it.next();
			Object resource = resources.get(key);
			result += ClassUtils.nameOf(key)+"("+key+"): "+ClassUtils.nameOf(resource)+"("+resource+")\n";
			if (resource instanceof JmsResourceHolder) {
				JmsResourceHolder jrh = (JmsResourceHolder)resource; 
				result+="  connection: "+jrh.getConnection()+", session: "+jrh.getSession()+"\n";
			}
		}
	}
	if (syncActive) {
		List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
		result += "synchronizations:\n";
		for (int i=0; i<synchronizations.size(); i++) {
			TransactionSynchronization synchronization = synchronizations.get(i);
			result += ClassUtils.nameOf(synchronization)+"("+synchronization+")\n"; 
		}
	}
	return result;
}