Transactional annotation on whole class + excluding a single method
Asked Answered
A

2

34

I have a class with @Transactional annotation (instead of marking it for all of its method).

Although i have a single method inside that class that shouldn't be annotated as @Transactional.

My question is is there an annotation i can put in this method to mark it as "non-transactional"? or should i start marking each single method in this class as "transactional" excluding this method (a lot of work)

thanks.

Armillda answered 14/8, 2013 at 13:31 Comment(2)
have you tried @Transactional(propagation=NOT_SUPPORTED)?Hexosan
It should actually be @Transactional(propagation = Propagation.NOT_SUPPORTED)Acrocarpous
S
35

There are different transaction propagation strategies to use. These exist in the enum Propagation. The ones you might want to use are

/**
 * Execute non-transactionally, suspend the current transaction if one exists.
 * Analogous to EJB transaction attribute of the same name.
 * <p>Note: Actual transaction suspension will not work on out-of-the-box
 * on all transaction managers. This in particular applies to JtaTransactionManager,
 * which requires the {@code javax.transaction.TransactionManager} to be
 * made available it to it (which is server-specific in standard J2EE).
 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
 */
NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

/**
 * Execute non-transactionally, throw an exception if a transaction exists.
 * Analogous to EJB transaction attribute of the same name.
 */
NEVER(TransactionDefinition.PROPAGATION_NEVER), // maybe not this one

So annotate the method inside your class with either of these.

@Transactional
public class MyTransactionalClass { 
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void nonTransactionalMethod() {...}
}

You can find all the propagation strategies here.

Su answered 14/8, 2013 at 15:9 Comment(2)
It helped me to solve the problem with non-working "readOnly = true"Fuller
Suggesting either NOT_SUPPORTED or NEVER options is not a thorough answer. Developers should be very careful to use it as it may cause unexpected exceptions at runtime. NOT_SUPPORTED option may not work if the transaction suspension isn't supported (see javadoc). NEVER will throw exception if the method is accidentially called within a transaction. Developers should consider SUPPORTS options as it will work happily wherever it's called. Unless NEVER is trictly intended.Berkman
R
14

The accepted answer of using propogation of NOT_SUPPORTED or NEVER doesn't actually answer the question. The question was how to have a method behave as though it was not annotated with @Transactional at all. Such a method will take part in a transaction if there is one, or execute non-transactionally if there is not. It will not suspend an existing transaction, or refuse to execute if there is one, one of which would be the result of the accepted answer.

The propagation to use is Propagation.SUPPORTS (in other words annotate the method with @Transactional(propagation = SUPPORTS). This will take part in the transaction if there is one, or execute non-transactionally if not, just like a non-annotated method would.

Not that according to the Javadoc it is not exactly the same as having no @Transactional annotation at all. It says:

Support a current transaction, execute non-transactionally if none exists. Analogous to EJB transaction attribute of the same name.

Note: For transaction managers with transaction synchronization, SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization will apply for. As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) will be shared for the entire specified scope. Note that this depends on the actual synchronization configuration of the transaction manager.

The only way to have the method behave exacty as if it was not annotated with @Transactional at all is to do as OP suggests: don't annotate the class, but annotate all the other methods, but for most uses that will not be necessary and using Propagation.SUPPORTS will suffice.

Rodrique answered 18/12, 2019 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.