Skip to content

Commit

Permalink
JtaTransactionManager relies on presence of JTA 1.1 API
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Mar 19, 2013
1 parent 27693bb commit 36942f6
Showing 1 changed file with 15 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,6 @@
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
Expand Down Expand Up @@ -101,8 +100,8 @@
* JtaTransactionManager autodetects the TransactionSynchronizationRegistry and uses
* it for registering Spring-managed synchronizations when participating in an existing
* JTA transaction (e.g. controlled by EJB CMT). If no TransactionSynchronizationRegistry
* is available (or the JTA 1.1 API isn't available), then such synchronizations
* will be registered via the (non-EE) JTA TransactionManager handle.
* is available, then such synchronizations will be registered via the (non-EE) JTA
* TransactionManager handle.
*
* <p>This class is serializable. However, active synchronizations do not survive serialization.
*
Expand Down Expand Up @@ -148,22 +147,6 @@ public class JtaTransactionManager extends AbstractPlatformTransactionManager
"java:comp/TransactionSynchronizationRegistry";


private static final String TRANSACTION_SYNCHRONIZATION_REGISTRY_CLASS_NAME =
"javax.transaction.TransactionSynchronizationRegistry";

private static Class<?> transactionSynchronizationRegistryClass;

static {
ClassLoader cl = JtaTransactionManager.class.getClassLoader();
try {
transactionSynchronizationRegistryClass = cl.loadClass(TRANSACTION_SYNCHRONIZATION_REGISTRY_CLASS_NAME);
}
catch (ClassNotFoundException ex) {
// JTA 1.1 API not available... simply proceed the JTA 1.0 way.
}
}


private transient JndiTemplate jndiTemplate = new JndiTemplate();

private transient UserTransaction userTransaction;
Expand All @@ -184,7 +167,7 @@ public class JtaTransactionManager extends AbstractPlatformTransactionManager

private String transactionSynchronizationRegistryName;

private transient Object transactionSynchronizationRegistry;
private transient TransactionSynchronizationRegistry transactionSynchronizationRegistry;

private boolean allowCustomIsolationLevels = false;

Expand Down Expand Up @@ -586,16 +569,12 @@ protected TransactionManager lookupTransactionManager(String transactionManagerN
* @see #setJndiTemplate
* @see #setTransactionSynchronizationRegistryName
*/
protected Object lookupTransactionSynchronizationRegistry(String registryName) throws TransactionSystemException {
if (transactionSynchronizationRegistryClass == null) {
throw new TransactionSystemException(
"JTA 1.1 [" + TRANSACTION_SYNCHRONIZATION_REGISTRY_CLASS_NAME + "] API not available");
}
protected TransactionSynchronizationRegistry lookupTransactionSynchronizationRegistry(String registryName) throws TransactionSystemException {
try {
if (logger.isDebugEnabled()) {
logger.debug("Retrieving JTA TransactionSynchronizationRegistry from JNDI location [" + registryName + "]");
}
return getJndiTemplate().lookup(registryName, transactionSynchronizationRegistryClass);
return getJndiTemplate().lookup(registryName, TransactionSynchronizationRegistry.class);
}
catch (NamingException ex) {
throw new TransactionSystemException(
Expand Down Expand Up @@ -637,7 +616,7 @@ protected TransactionManager retrieveTransactionManager() throws TransactionSyst
* or {@code null} if none found
* @throws TransactionSystemException in case of errors
*/
protected Object retrieveTransactionSynchronizationRegistry() throws TransactionSystemException {
protected TransactionSynchronizationRegistry retrieveTransactionSynchronizationRegistry() throws TransactionSystemException {
return null;
}

Expand Down Expand Up @@ -712,24 +691,15 @@ protected TransactionManager findTransactionManager(UserTransaction ut) {
* or {@code null} if none found
* @throws TransactionSystemException in case of errors
*/
protected Object findTransactionSynchronizationRegistry(UserTransaction ut, TransactionManager tm)
protected TransactionSynchronizationRegistry findTransactionSynchronizationRegistry(UserTransaction ut, TransactionManager tm)
throws TransactionSystemException {

if (transactionSynchronizationRegistryClass == null) {
// JTA 1.1 API not present - skip.
if (logger.isDebugEnabled()) {
logger.debug("JTA 1.1 [" + TRANSACTION_SYNCHRONIZATION_REGISTRY_CLASS_NAME + "] API not available");
}
return null;
}

// If we came here, we might be on Java EE 5, since the JTA 1.1 API is present.
if (this.userTransactionObtainedFromJndi) {
// UserTransaction has already been obtained from JNDI, so the
// TransactionSynchronizationRegistry probably sits there as well.
String jndiName = DEFAULT_TRANSACTION_SYNCHRONIZATION_REGISTRY_NAME;
try {
Object tsr = getJndiTemplate().lookup(jndiName, transactionSynchronizationRegistryClass);
TransactionSynchronizationRegistry tsr = getJndiTemplate().lookup(jndiName, TransactionSynchronizationRegistry.class);
if (logger.isDebugEnabled()) {
logger.debug("JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]");
}
Expand All @@ -743,14 +713,13 @@ protected Object findTransactionSynchronizationRegistry(UserTransaction ut, Tran
}
}
// Check whether the UserTransaction or TransactionManager implements it...
if (transactionSynchronizationRegistryClass.isInstance(ut)) {
return ut;
if (ut instanceof TransactionSynchronizationRegistry) {
return (TransactionSynchronizationRegistry) ut;
}
if (transactionSynchronizationRegistryClass.isInstance(tm)) {
return tm;
if (tm instanceof TransactionSynchronizationRegistry) {
return (TransactionSynchronizationRegistry) tm;
}
// OK, so no JTA 1.1 TransactionSynchronizationRegistry is available,
// despite the API being present...
// OK, so no JTA 1.1 TransactionSynchronizationRegistry is available...
return null;
}

Expand Down Expand Up @@ -1137,7 +1106,7 @@ protected void doRegisterAfterCompletionWithJtaTransaction(

if (this.transactionSynchronizationRegistry != null) {
// JTA 1.1 TransactionSynchronizationRegistry available - use it.
new InterposedSynchronizationDelegate().registerInterposedSynchronization(
this.transactionSynchronizationRegistry.registerInterposedSynchronization(
new JtaAfterCompletionSynchronization(synchronizations));
}

Expand Down Expand Up @@ -1195,16 +1164,4 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
initTransactionSynchronizationRegistry();
}


/**
* Inner class to avoid a direct dependency on the JTA 1.1 API
* (javax.transaction.TransactionSynchronizationRegistry interface).
*/
private class InterposedSynchronizationDelegate {

public void registerInterposedSynchronization(Synchronization synch) {
((TransactionSynchronizationRegistry) transactionSynchronizationRegistry).registerInterposedSynchronization(synch);
}
}

}

0 comments on commit 36942f6

Please sign in to comment.