In my company the usage of the Spring framework is discouraged and instead Java EE 6 is promoted. But recently I read about Springs @Transactional
annotation at Using @Transactional and think this could be really useful for our code.
As far as I understand a method annotated with @Transactional
will either reuse an already existing transaction or open up a new transaction if no active transaction exists when calling that method. Is there something analogous (e.g. a similar annotation) available in Java EE 6?
EJB components have this transactional control in Java EE. You can set the transaction of a method on the EJB to be Required, RequiresNew, Supports, etc. You would almost always use a Stateless Session Bean (@Stateless
) for the requirements you describe:
> @TransactionAttribute(value=[MANDATORY,
> REQUIRED, REQUIRES_NEW, SUPPORTS,
> NOT_SUPPORTED, NEVER]
Required, the default, will re-use an existing txn if there's one running or create a new one if there is not. Java EE 6 ships with EJB 3.1, so you don't even need the Business Interface and you can package the EJBs in the WAR file if you want. Therefore you are using EJBs, but to the developer they are much easier to integrate if all you want is JTA support.
This is a useful cheat sheet for the EJB annotations and there are numerous guides if you Google for them.
Java EE 7 now contains @javax.transactional.Transactional. It acts quite the same as the spring annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With