Asynchronous CDI Events

Few days ago, during our regular code review, one of my colleagues raised a question what would happen – and if it’s even possible – when a CDI Observer (so a method with parameter annotated @Observes) would be invoked multiple times at the same time for different event instances. In other words, after producing few events, is it possible that the following method will be processed by more than one thread at the same time:...

May 13, 2013 · 10 min

Entities as Local Interface Parameters Can Harm You

Let’s start with a short quiz. TxMergeBean is a SLSB that uses CMT and EntityManager. Assume the following local and remote business interfaces: public interface TxMergeCommon { void methodA(); void methodB(MeineEntity entity); } @Local public interface TxMergeLocal {} @Remote public interface TxMergeRemote {} and the following SLSB: @Stateless public class TxMergeBean implements TxMergeRemote, TxMergeLocal { @PersistenceContext private EntityManager em; @EJB private TxMergeLocal self; @Resource private SessionContext sctx; public void methodA() { MeineEntity entity = new MeineEntity("methodA"); em....

March 14, 2013 · 4 min

JPA and CMT -- Why Catching Persistence Exception is Not Enough?

Being in EJB and JPA world using CMT (Container Managed Transactions) is very comfortable. Just define few annotations to demarcate transaction boundary (or use the defaults) and that’s it – no fiddling with manual begin, commit or rollback operations. One way to rollback your transaction is to throw non-application exception (or application exception with rollback = true) from your EJB’s business method. It seems simple: if during some operation there is a possibility that an exception will be thrown and you don’t want to rollback your tx than you should just catch this exception and you’re fine....

March 10, 2013 · 5 min

Types of EntityManagers -- Application-managed EntityManager

JPA specification defines few types of EntityManagers / Persistence Contexts. We can have: extended and transactional-scoped EntityManagers, container-managed or application-managed EntityManagers. JTA or resource-local EntityManager, Besides the above distinction, we also have two main contexts in which EntityManager / Persistence Context can exist – Java EE and Java SE. Not every option is available for Java EE and not every is possible in Java SE. In the rest of the post I refer to the Java EE environment....

November 21, 2012 · 9 min