JTA 1.2 support, in particular for the javax.transaction.Transactional annotation

Issue: SPR-9139
This commit is contained in:
Juergen Hoeller
2013-03-27 22:33:17 +01:00
parent ff160f9aeb
commit 52fd84bb57
5 changed files with 229 additions and 5 deletions

View File

@@ -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.
@@ -35,7 +35,8 @@ import org.springframework.util.ClassUtils;
*
* <p>This class reads Spring's JDK 1.5+ {@link Transactional} annotation and
* exposes corresponding transaction attributes to Spring's transaction infrastructure.
* Also supports EJB3's {@link javax.ejb.TransactionAttribute} annotation (if present).
* Also supports JTA 1.2's {@link javax.transaction.Transactional} and EJB3's
* {@link javax.ejb.TransactionAttribute} annotation (if present).
* This class may also serve as base class for a custom TransactionAttributeSource,
* or get customized through {@link TransactionAnnotationParser} strategies.
*
@@ -53,6 +54,9 @@ import org.springframework.util.ClassUtils;
public class AnnotationTransactionAttributeSource extends AbstractFallbackTransactionAttributeSource
implements Serializable {
private static final boolean jta12Present = ClassUtils.isPresent(
"javax.transaction.Transactional", AnnotationTransactionAttributeSource.class.getClassLoader());
private static final boolean ejb3Present = ClassUtils.isPresent(
"javax.ejb.TransactionAttribute", AnnotationTransactionAttributeSource.class.getClassLoader());
@@ -83,6 +87,9 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa
this.publicMethodsOnly = publicMethodsOnly;
this.annotationParsers = new LinkedHashSet<TransactionAnnotationParser>(2);
this.annotationParsers.add(new SpringTransactionAnnotationParser());
if (jta12Present) {
this.annotationParsers.add(new JtaTransactionAnnotationParser());
}
if (ejb3Present) {
this.annotationParsers.add(new Ejb3TransactionAnnotationParser());
}

View File

@@ -0,0 +1,76 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.annotation;
import java.io.Serializable;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
/**
* Strategy implementation for parsing JTA 1.2's {@link javax.transaction.Transactional} annotation.
*
* @author Juergen Hoeller
* @since 4.0
*/
@SuppressWarnings("serial")
public class JtaTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
javax.transaction.Transactional ann = AnnotationUtils.getAnnotation(ae, javax.transaction.Transactional.class);
if (ann != null) {
return parseTransactionAnnotation(ann);
}
else {
return null;
}
}
public TransactionAttribute parseTransactionAnnotation(javax.transaction.Transactional ann) {
RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
rbta.setPropagationBehaviorName(RuleBasedTransactionAttribute.PREFIX_PROPAGATION + ann.value().toString());
ArrayList<RollbackRuleAttribute> rollBackRules = new ArrayList<RollbackRuleAttribute>();
Class[] rbf = ann.rollbackOn();
for (Class rbRule : rbf) {
RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
rollBackRules.add(rule);
}
Class[] nrbf = ann.dontRollbackOn();
for (Class rbRule : nrbf) {
NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
rollBackRules.add(rule);
}
rbta.getRollbackRules().addAll(rollBackRules);
return rbta;
}
@Override
public boolean equals(Object other) {
return (this == other || other instanceof JtaTransactionAnnotationParser);
}
@Override
public int hashCode() {
return JtaTransactionAnnotationParser.class.hashCode();
}
}

View File

@@ -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.
@@ -24,13 +24,15 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
* Strategy interface for parsing known transaction annotation types.
* {@link AnnotationTransactionAttributeSource} delegates to such
* parsers for supporting specific annotation types such as Spring's own
* {@link Transactional} or EJB3's {@link javax.ejb.TransactionAttribute}.
* {@link Transactional}, JTA 1.2's {@link javax.transaction.Transactional}
* or EJB3's {@link javax.ejb.TransactionAttribute}.
*
* @author Juergen Hoeller
* @since 2.5
* @see AnnotationTransactionAttributeSource
* @see SpringTransactionAnnotationParser
* @see Ejb3TransactionAnnotationParser
* @see JtaTransactionAnnotationParser
*/
public interface TransactionAnnotationParser {