title: Setting context in a JPA connection — is this safe?
body: <p>I need to set some <em>context before every database operation</em> (I have tried using Oracle's package-level variables, but due to some issues with package recompilation, I will experiment with <a href="http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sessio.htm#BHCCFGIF" rel="nofollow noreferrer">DBMS_SESSION</a> and/or <a href="http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_appinf.htm#CHECEIEB" rel="nofollow noreferrer">DBMS_APPLICATION_INFO</a>), so that we can get specific user info anywhere we need it (procedures, triggers, etc.), instead of having dozens of database connections identified as "JBoss".</p>
  
  <p>I have written a Java EE interceptor that intercepts calls to a @Stateless bean. It calls an Oracle function to set some session context (look at this question for some sample code <a href="https://stackoverflow.com/questions/14004504/how-to-tell-if-a-transaction-is-active-in-a-java-ee-6-interceptor">How to tell if a transaction is active in a Java EE 6 interceptor</a>).</p>
  
  <p><strong>My first worry was connection pooling</strong>. At first I thought that the default @PersistenceContext propagation provided by Java EE would be enough to guarantee that everything runs in the same connection/transaction/EntityManager, and I only had to unset everything at the end of my interceptor (in a <code>finally</code> block) before the connection is returned to the pool. It seemed a little fragile, but I thought it could work.</p>
  
  <p><strong>Then I found out that Hibernate has a property called hibernate.connection.release_mode</strong>
  (<a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#configuration-jdbc-properties" rel="nofollow noreferrer">Hibernate docs about hibernate.connection.release_mode</a>, <a href="https://access.redhat.com/knowledge/docs/en-US/JBoss_Enterprise_Web_Server/1.0/html/Hibernate_Core_Reference_Guide/transactions-connection-release.html" rel="nofollow noreferrer">Red Hat docs about org.hibernate.ConnectionReleaseMode</a>) and that the <strong>default and recommended</strong> behavior when using JTA transactions is to <strong>release the connection after every statement</strong> (although the docs say something about <em>re-acquiring the same underlying connection</em>, which just confused me).</p>
  
  <p>Now I'm not even sure I can reliably set something in an interceptor that will be visible only to this user/operation without the risk of somebody else grabbing the same connection in the middle of my business method and making a mess with my user context. As I understand it, Oracle database session variables are kept for a connection, not for a transaction or a @PersistenceContext (the database knows nothing about the persistence context after all, and a connection can be used for multiple transactions).</p>
  
  <p>I'm about to give up because this is looking more and more fragile as I learn more about the implementation details of all the technologies involved. <strong>Can this <em>user context</em> idea be made to work or should I try a totally different approach? And how could I test/debug my implementation to be sure there aren't any concurrency issues lurking around?</strong> I haven't found any useful event listener to monitor the framework behavior, and building a program to stress test the server is too much work to invest in something I'm not sure is supposed to work anyway.</p>
  
  <p>I'm using JBoss AS 7.1, EJB 3.1, an Oracle 10g database, and JPA 2.0 (backed by Hibernate, although we do not use any Hibernate-specific API).</p>