users@jta-spec.java.net

[jta-spec users] Re: rough draft of JTA 1.2 section 3.7 Transactional Annotation

From: Jens Schumann <jens.schumann_at_openknowledge.de>
Date: Fri, 7 Dec 2012 07:24:46 +0000

Hey Paul,

Thanks for the first draft.

I have to get used to rollbackOn/dontRollBackOn, therefore I can't comment on this topic yet. At least I would recommend to declare rollbackOn and dontRollBackOn as @NonBinding. Maybe someone from the CDI EG can comment on this.

I would love to see support @ApplicationException outside the javax.ejb world too. Can we "copy" @ApplicationException to javax.transaction?

Jens

Von: Paul Parkinson <paul.parkinson_at_oracle.com<mailto:paul.parkinson_at_oracle.com>>
Antworten an: "users_at_jta-spec.java.net<mailto:users_at_jta-spec.java.net>" <users_at_jta-spec.java.net<mailto:users_at_jta-spec.java.net>>
Datum: Freitag, 7. Dezember 2012 04:24
An: "users_at_jta-spec.java.net<mailto:users_at_jta-spec.java.net>" <users_at_jta-spec.java.net<mailto:users_at_jta-spec.java.net>>
Betreff: [jta-spec users] rough draft of JTA 1.2 section 3.7 Transactional Annotation

Hi,

Below is a rough first run of the JTA 1.2 section 3.7 Transactional Annotation, http://java.net/jira/browse/JTA_SPEC-5

Thank you for any feedback as we need to close this down in the next couple weeks.

Regards,
Paul


3.7 Transactional Annotation

The javax.transaction.Transactional annotation provides the application the ability to control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE
specification such as servlets, JAX-RS resource classes, and JAX-WS service endpoints, declaratively. This provides the semantics of EJB transaction attributes in CDI without dependencies such as RMI. This support is implemented via an implementation of a CDI interceptor that conducts the necessary suspending, resuming, etc. [*todo specify the number used for the transactional interceptor, so that others can choose to order their interceptors before or after the transactional interceptor; coordinate with CDI for this]

By default checked exceptions do not result in the transactional interceptor marking the transaction for rollback and instances of RuntimeException and its subclasses do. This default behavior can be overridden by specifying which exceptions result in the interceptor marking the transaction for rollback. The rollbackOn element can be set to indicate which exceptions should cause the interceptor to mark the transaction for rollback. Conversely, the dontRollbackOn element can be set to indicate which exceptions should do not cause the interceptor to mark the transaction for rollback. When a class is specified for either of these elements, the designated behavior applies to subclasses of that class as well. If both elements are specified, dontRollbackOn takes precedence.

The following are some example usages of rollbackOn and dontRollbackOn elements.

- The following will override behavior for application exceptions, causing the transaction to be marked for rollback for all application exceptions.
@Transactional(rollbackOn={Exception.class})

- The following will prevent transactions from being marked for rollback by the interceptor when an IllegalStateException or any of its subclasses reaches the interceptor.
@Transactional(dontRollbackOn={IllegalStateException.class})

- The following will cause the transaction to be marked for rollback for all runtime exceptions and all SQLException types except for SQLWarning.
@Transactional(
rollbackOn={SQLException.class},
dontRollbackOn={SQLWarning.class}
)

EJB application exceptions (i.e., runtime exceptions annotated with @ApplicationException) are treated just as any other runtime exceptions unless otherwise specified.

When Transactional annotated managed beans are used in conjunction with EJB container managed transactions the EJB container behavior is applied before the bean is called. When the bean is called the CDI behavior is applied before calling the bean's methods. It is best practice to avoid such use of Transactional annotations in conjunction with EJB container managed transactions in order to avoid possible confusion.

“Java Transaction API Reference” on page 30 has a full description of this annotation


[...]


/**
 * Annotation used by applications to control transaction boundaries on CDI managed beans, as well as
 * classes defined as managed beans by the Java EE specification such as servlets, JAX-RS resource classes, and JAX-WS
 * service endpoints, declaratively. This provides the semantics of EJB transaction attributes in CDI without
 * dependencies such as RMI. This support is implemented via an implementation of a CDI interceptor that conducts the
 * necessary suspending, resuming, etc. [*todo specify the number used for the transactional interceptor, so that others
 * can choose to order their interceptors before or after the transactional interceptor; coordinate with CDI for this]
 * [*open issue as to whether the "value" attribute of the "Transactional" annotation should be binding or not]
 *
 * By default checked exceptions do not result in the transactional interceptor marking the transaction for rollback
 * and instances of RuntimeException and its subclasses do. This default behavior can be overridden by specifying
 * which exceptions result in the interceptor marking the transaction for rollback. The rollbackOn element can be set
 * to indicate which exceptions should cause the interceptor to mark the transaction for rollback. Conversely, the
 * dontRollbackOn element can be set to indicate which exceptions should do not cause the interceptor to mark the
 * transaction for rollback. When a class is specified for either of these elements, the designated behavior applies
 * to subclasses of that class as well. If both elements are specified, dontRollbackOn takes precedence.
 *
 * EJB application exceptions (i.e., runtime exceptions annotated with @ApplicationException) are treated just as any
 * other runtime exceptions unless otherwise specified.
 *
 * When Transactional annotated managed beans are used in conjunction with EJB container managed transactions the EJB
 * container behavior is applied before the bean is called. When the bean is called the CDI behavior is applied before
 * calling the bean's methods. It is best practice to avoid such use of Transactional annotations in conjunction with
 * EJB container managed transactions in order to avoid possible confusion.
 *
 *
 * @since JTA1.2
 */
@Inherited
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Transactional {
    TxType value() default TxType.REQUIRED;

    public enum TxType {REQUIRED, REQUIRES_NEW, MANDATORY, SUPPORTS, NOT_SUPPORTED, NEVER}

    Class[] rollbackOn() default {};

    Class[] dontRollbackOn() default {};

}