📄 jtatransactionmanager.java
字号:
* Look up the JTA UserTransaction in JNDI via the configured name.
* Called by afterPropertiesSet if no direct UserTransaction reference was set.
* Can be overridden in subclasses to provide a different UserTransaction object.
* @param userTransactionName the JNDI name of the UserTransaction
* @return the UserTransaction object
* @throws CannotCreateTransactionException if the JNDI lookup failed
* @see #setJndiTemplate
* @see #setUserTransactionName
*/
protected UserTransaction lookupUserTransaction(String userTransactionName)
throws CannotCreateTransactionException {
try {
Object jndiObj = this.jndiTemplate.lookup(userTransactionName);
if (!(jndiObj instanceof UserTransaction)) {
throw new CannotCreateTransactionException("Object [" + jndiObj + "] available at JNDI location [" +
userTransactionName + "] does not implement " +
"javax.transaction.UserTransaction");
}
UserTransaction ut = (UserTransaction) jndiObj;
if (logger.isInfoEnabled()) {
logger.info("Using JTA UserTransaction [" + ut + "] from JNDI location [" +
userTransactionName + "]");
}
return ut;
}
catch (NamingException ex) {
throw new CannotCreateTransactionException("JTA UserTransaction is not available at JNDI location [" +
userTransactionName + "]", ex);
}
}
/**
* Look up the JTA TransactionManager in JNDI via the configured name.
* Called by afterPropertiesSet if no direct TransactionManager reference was set.
* Can be overridden in subclasses to provide a different TransactionManager object.
* @param transactionManagerName the JNDI name of the TransactionManager
* @return the UserTransaction object
* @throws CannotCreateTransactionException if the JNDI lookup failed
* @see #setJndiTemplate
* @see #setTransactionManagerName
*/
protected TransactionManager lookupTransactionManager(String transactionManagerName)
throws CannotCreateTransactionException {
try {
Object jndiObj = this.jndiTemplate.lookup(transactionManagerName);
if (!(jndiObj instanceof TransactionManager)) {
throw new CannotCreateTransactionException("Object [" + jndiObj + "] available at JNDI location [" +
transactionManagerName + "] does not implement " +
"javax.transaction.TransactionManager");
}
TransactionManager tm = (TransactionManager) jndiObj;
if (logger.isInfoEnabled()) {
logger.info("Using JTA TransactionManager [" + tm + "] from JNDI location [" +
transactionManagerName + "]");
}
return tm;
}
catch (NamingException ex) {
throw new CannotCreateTransactionException("JTA TransactionManager is not available at JNDI location [" +
transactionManagerName + "]", ex);
}
}
protected Object doGetTransaction() {
return this.userTransaction;
}
protected boolean isExistingTransaction(Object transaction) {
try {
int status = ((UserTransaction) transaction).getStatus();
return (status != Status.STATUS_NO_TRANSACTION);
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on getStatus", ex);
}
}
protected void doBegin(Object transaction, TransactionDefinition definition) {
if (logger.isDebugEnabled()) {
logger.debug("Beginning JTA transaction [" + transaction + "] ");
}
UserTransaction ut = (UserTransaction) transaction;
applyIsolationLevel(definition.getIsolationLevel());
try {
if (definition.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) {
ut.setTransactionTimeout(definition.getTimeout());
}
ut.begin();
}
catch (NotSupportedException ex) {
// assume "nested transactions not supported"
throw new IllegalTransactionStateException(
"JTA implementation does not support nested transactions", ex);
}
catch (UnsupportedOperationException ex) {
// assume "nested transactions not supported"
throw new IllegalTransactionStateException(
"JTA implementation does not support nested transactions", ex);
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on begin", ex);
}
}
/**
* Apply the given transaction isolation level. Default implementation
* will throw an exception for any level other than ISOLATION_DEFAULT.
* To be overridden in subclasses for specific JTA implementations.
* @param isolationLevel isolation level taken from transaction definition
* @throws InvalidIsolationLevelException if the given isolation level
* cannot be applied
*/
protected void applyIsolationLevel(int isolationLevel) throws InvalidIsolationLevelException {
if (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT) {
throw new InvalidIsolationLevelException("JtaTransactionManager does not support custom isolation levels");
}
}
protected Object doSuspend(Object transaction) {
if (this.transactionManager == null) {
throw new IllegalTransactionStateException("JtaTransactionManager needs a JTA TransactionManager for " +
"suspending a transaction - specify the 'transactionManager' " +
"or 'transactionManagerName' property");
}
try {
return this.transactionManager.suspend();
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on suspend", ex);
}
}
protected void doResume(Object transaction, Object suspendedResources) {
if (this.transactionManager == null) {
throw new IllegalTransactionStateException("JtaTransactionManager needs a JTA TransactionManager for " +
"suspending a transaction - specify the 'transactionManager' " +
"or 'transactionManagerName' property");
}
try {
this.transactionManager.resume((Transaction) suspendedResources);
}
catch (InvalidTransactionException ex) {
throw new IllegalTransactionStateException("Tried to resume invalid JTA transaction", ex);
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on resume", ex);
}
}
protected boolean isRollbackOnly(Object transaction) throws TransactionException {
try {
return ((UserTransaction) transaction).getStatus() == Status.STATUS_MARKED_ROLLBACK;
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on getStatus", ex);
}
}
protected void doCommit(DefaultTransactionStatus status) {
if (status.isDebug()) {
logger.debug("Committing JTA transaction [" + status.getTransaction() + "]");
}
try {
((UserTransaction) status.getTransaction()).commit();
}
catch (RollbackException ex) {
throw new UnexpectedRollbackException("JTA transaction rolled back", ex);
}
catch (HeuristicMixedException ex) {
throw new HeuristicCompletionException(HeuristicCompletionException.STATE_MIXED, ex);
}
catch (HeuristicRollbackException ex) {
throw new HeuristicCompletionException(HeuristicCompletionException.STATE_ROLLED_BACK, ex);
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on commit", ex);
}
}
protected void doRollback(DefaultTransactionStatus status) {
if (status.isDebug()) {
logger.debug("Rolling back JTA transaction [" + status.getTransaction() + "]");
}
try {
((UserTransaction) status.getTransaction()).rollback();
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on rollback", ex);
}
}
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
if (status.isDebug()) {
logger.debug("Setting JTA transaction [" + status.getTransaction() + "] rollback-only");
}
try {
((UserTransaction) status.getTransaction()).setRollbackOnly();
}
catch (IllegalStateException ex) {
throw new NoTransactionException("No active JTA transaction");
}
catch (SystemException ex) {
throw new TransactionSystemException("JTA failure on setRollbackOnly", ex);
}
}
protected void doCleanupAfterCompletion(Object transaction) {
// nothing to do here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -