Reputation: 9353
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.
Upvotes: 5
Views: 9103
Reputation: 3387
By the moment (until that Java EE 7 arrives) you could mix a CDI (No more @ManagedBean) with a EJB (transactional features) just like Adam Bien shows in his post:
@Stateless
@Named("helloService")
public class HelloService {
@EJB ClockService clockService;
public String getHello(){
return "Hello from EJB / CDI: " + clockService.currentTime();
}
}
Something good about this is that your EJB is exposed directly to the View Tier, no need of @Interceptor, but, don't abuse of this approach you could high coupling between View and Control tiers
From the JavaEE7 Spec: " Although CDI, JSF and EJB already all build on a common but very abstract concept called the managed bean, it seems that JSF Managed Beans might be dropped in favor of CDI and EJB might be retrofitted as a set of CDI services."
References:
Upvotes: 2
Reputation: 1057
Transaction management is a different API so it does not matter weather you use it with CDI or EJB.
Upvotes: 2
Reputation: 5378
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.
Upvotes: 8