Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something analogous to Springs @Transactional annotation available in Java EE 6?

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?

like image 394
asmaier Avatar asked Jun 21 '11 11:06

asmaier


2 Answers

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.

like image 76
planetjones Avatar answered Oct 13 '22 00:10

planetjones


Java EE 7 now contains @javax.transactional.Transactional. It acts quite the same as the spring annotation.

like image 22
Christian Schneider Avatar answered Oct 13 '22 00:10

Christian Schneider