I would like to develop an application with CDI (I use Spring usually) to discover this technology.
I have read many articles about CDI to learn how it works. I have a simple question about transaction management (for persistence in database for example):
Is it mandatory to use EJB 3.1 to have transaction management or is it possible to have it with CDI only ?
Thanks.
The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.
Programmatic transaction management − This means that you have to manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain. Declarative transaction management − This means you separate transaction management from the business code.
The transaction management is one of the most important Java Persistence API aspects if you are using Hibernate or any other JPA provider you should know how valuable transaction management is. The JPA transaction determines when the new, modified or deleted entity is synchronised with a database.
TransactionManager: An interface which allows an application server to demarcate and control transactions. UserTransaction: This interface allows an application program to demarcate and control transactions explicitly.
No, you can do it with CDI. You simply need to create an interceptor that starts, commits or rollsback a transaction. It's really not that hard. In Java EE 7 there will be a @Transactional
for all managed beans (JSF, CDI, EJB, etc) that will be a CDI interceptor.
EDIT: If you'd like to take a look at some that are already done, Apache DeltaSpike and Seam 3 (no longer being developed) have interceptors to handle transactions.
Transaction management is a different API so it does not matter weather you use it with CDI or EJB.
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