Arun
Arun

Reputation: 619

What is the use of @Transactional annotation

I am new to the EJB Projects. And am trying to understand the usage of @Transactional annotation at top of my EJB methods. I have searched for the content and there is no clear explanation on this. Can anyone explain clearly about this.

Upvotes: 8

Views: 13775

Answers (1)

Miljen Mikic
Miljen Mikic

Reputation: 15241

@Transactional comes from the Spring world, but Oracle finally included it in Java EE 7 specification (docs). Previously, you could only annotate EJBs with @TransactionAttribute annotation, and similar is now possible for CDIs as well, with @Transactional. What's the purpose of these annotations? It is a signal to the application server that certain class or method is transactional, indicating also how it is gonna behave in certain conditions, e.g. what if it's called inside a transaction etc.

An example:

@Transactional(Transactional.TxType.MANDATORY)
public void methodThatRequiresTransaction()
{
..
}

The method above will throw an exception if it is not called within a transaction.

@Transactional(Transactional.TxType.REQUIRES_NEW)
public void methodThatWillStartNewTransaction()
{
..
}

Interceptor will begin a new JTA transaction for the execution of this method, regardless whether it is called inside a running transaction or not. However, if it is called inside a transaction, that transaction will be suspended during the execution of this method.

See also:

Upvotes: 11

Related Questions