Nov 3, 2018

How to flush to database in Java EE

JPA 2.1 introduced the concept of unsynchronized persistence context which allows fine grained control over flushing of the JPA Entity Manager i.e. by explicitly calling EntityManager#joinTransaction. Previously, this was defaulted to end of JTA transaction e.g. in a typical Stateless EJB, the entity manager would flush its state to the DB at the end of a method (which starts and ends a transaction by default). 

Possible in the pre Java EE 5 and 6 can be tweaked to achieve the same result as attained by the Unsynchronized Persistence Context in Java EE 7


@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
 public void flush(){
   //dummy method to trigger transaction and flush EM state to DB
 }