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

EJB 3.1 Interceptors -- Same Transaction, Same Principal

Few days ago I’ve created a simple interceptors spike-solution at Github. Its purpose was to show my colleagues that in the EJB’s interceptors you can implement a code that exists in the same transaction and the same security context as the called EJB business method. Moreover, you can benefit from all the dependency injection you need. This project uses the following resources injected by the Application Server: TransactionSynchronizationRegistry – used for getting current transaction key and checking if we’re in the same transaction (I’ve written about it e....

November 11, 2012 · 2 min

After the Spring Core 3.0 Exam

Yesterday I took the SpringSource Certified Spring 3.0 Professional exam and passed it successfully with 94%. Below you can find some information about my preparation process, the exam form and the type of questions. So, here we go. Preparation I attended the Spring 3 Core training and described it here. This was the entry point for my learning process. I’ve done a lot of notes during the training, so even after three months, the slides + notes were pretty decent source of knowledge....

July 24, 2012 · 3 min

Am I in the same transaction? Am I using the same PersistenceContext?

Recently, I’ve bumped into few posts on StackOverflow where people tend to compare container managed EntityManager instances (so the one injected by the container) by invoking EntityManager#toString() method. I’ve felt that it’s fundamentally wrong to compare EntityManager instances without knowing how they’re managed by the JPA provider or the Server Application. And what if this behaviour differs between Application Server vendors? The JPA provider provides an implementation of EntityManager – that’s obvious....

November 23, 2011 · 4 min

Get current JTA transaction status from CMT EJB

You probably know that if you use BMT (Bean Managed Transactions) you can get information about current transaction status by using UserTransaction interface (which implementation can be fetched either by JNDI or using dependency injection) and executing it’s getStatus() method. If you use CMT (Container Managed Transactions) you cannot use UserTransaction interface. Instead, you can manage your transaction through SessionContext interface. This interface gives you two places that hooks to the current transaction: setRollbackOnly(-) and getRollbackOnly()....

September 1, 2011 · 1 min