title: How can I intercept JTA transactions events and get a reference to the current EntityManager associated with the transaction
body: <p><strong>Long story short:</strong> We develop and maintain a library that can be used in other projects using JavaEE7/CDI/JPA. Applications will run under Glassfish-4.0 and use Hibernate's JPA implementation for an underlying PostgreSQL persistence. This is part of a long term migration effort to rewrite old applications that were written in Spring/Struts/Hibernate into the new world of JavaEE7/CDI/JTA.</p>
  
  <p><strong>The problem:</strong> For audit purposes, our library needs to intercept all database transactions and include custom SQL statements before the user statements are executed. At this point, the current username and IP address need to be inserted into a temporary database variable (vendor specific feature) so that a database trigger can read them to create the audit trail for any row modification. <a href="https://stackoverflow.com/questions/13172524/passing-user-id-to-postgresql-triggers">This particular post was very helpful providing alternatives</a>, and our team went down the trigger road due to a previously established legacy.</p>
  
  <p><strong>HOWEVER:</strong> We are <strong>deeply</strong> disappointed at how JTA handles transaction events. There are numerous ways to intercept transactions, but this particular case seems to be down right impossible. In the old architecture, using Spring's transaction manager, we simply used a <a href="http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html" rel="nofollow noreferrer">Hibernate Interceptor</a> implementing <strong>Interceptor.afterTransactionBegin(...)</strong>. Reading up on the <a href="http://www.oracle.com/technetwork/java/javaee/tech/jta-138684.html" rel="nofollow noreferrer">official JTA-1.2 spec</a>, we found that it does have support for <strong>Synchronization.beforeCompletion</strong> and <strong>Synchronization.afterCompletion</strong>. After several hours of debugging sessions we clearly noted that Hibernate's implementation of JTA is using these facilities. But JTA seems to be lacking events like <strong>beforeBegin</strong> and <strong>afterBegin</strong> (which IMHO seems to be a lack of common sense). And since there are no facilities to intercept those, Hibernate complies fully with JTA and it simply won't. Period.</p>
  
  <p>No matter what we do, we can't find a way. We tried, for instance, to intercept <strong>@Transactional</strong> annotations and run our code just after the container's JTA impl does its job to open the transaction. But we lack the ability to dynamically acquire the EntityManager associated with that particular transaction. Remember: this is a library, not the web application itself. It cannot make any assumptions about which Persistence Units are declared and used by the application. And, as far as we can tell, we need to know which specific Persistent Unit name to inject it into our code. We are trying to provide an audit facility to other temas that is as transparent as possible.</p>
  
  <p>So we humbly ask for help. If anyone out there has a solution, workaround, whatever opinion, we'll be glad to hear it.</p>