Declarative transaction scope
Both the Spring and JPA @Transaction
annotation allow you to define the scope of a given application transaction.
So, if a service method is annotated with the @Transactional
annotation, it will run in a transactional context. If the service method uses multiple DAO or Repositories, all read and write operations will be executed in the same database transaction.
Spring @Transactional
The org.springframework.transaction.annotation.Transactional
annotation has been available since the 1.2 version of the Spring framework (circa 2005), and it allows you to set the following transactional properties:
isolation
: the underlying database isolation level
noRollbackFor
and noRollbackForClassName
: the list of Java Exception
classes that can be triggered without triggering a transaction rollback
rollbackFor
and rollbackForClassName
: the list of Java Exception
classes that trigger a transaction rollback when being thrown
propagation
: the transaction propagation type given by the Propagation
Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED
) or a new transaction context should be created (e.g., REQUIRES_NEW
) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY
) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED
).
readOnly
: whether the current transaction should only read data without applying any changes.
timeout
: how many seconds should the transaction context be allowed to run until a timeout exception is thrown.
value
or transactionManager
: the name of the Spring TransactionManager
bean to be used when binding the transaction context.
Java EE @Transactional
The javax.transaction.Transactional
annotation was added by the Java EE 7 specification (circa 2013). So, the Java EE annotation was added 8 years later than its Spring counterpart.
The Java EE @Transactional
defines just 3 attributes:
dontRollbackOn
: the list of Java Exception
classes that can be triggered without triggering a transaction rollback
rollbackOn
: the list of Java Exception
classes that trigger a transaction rollback when being thrown
value
: the propagation strategy, given by the TxType
Enum. For instance, if the transaction context can be inherited (e.g., REQUIRED
) or a new transaction context should be created (e.g., REQUIRES_NEW
) or if an exception should be thrown if no transaction context is present (e.g., MANDATORY
) or if an exception should be thrown if a current transaction context is found (e.g., NOT_SUPPORTED
).
Which one to choose?
If you're using Spring or Spring Boot, then use the Spring @Transactional
annotation, as it allows you to configure more attributes than the Java EE @Transactional
annotation.
If you are using Java EE alone, and you deploy your application on a Java EE application server, then use the Java EE @Transactional
annotation.
javax.transaction.Transactional
so one could now use it also in Spring applications without any additional actions. IMO, this was quite bad decision from design standpoint, because out of my experience a lot of developers unconciously confuse these two in their code which leads to problems afterwards. – Wildman