I need to set some context to each database operation (I tried to use the Oracle package-level variables, but due to some problems with recompiling the package I will experiment with DBMS_SESSION and / or DBMS_APPLICATION_INFO ), so we can get specific information about the user, anywhere where necessary (procedures, triggers, etc.), instead of having dozens of database connections identified as "JBoss".
I wrote interceptor Java EE, which intercepts calls to @Stateless bean. It causes Oracle function to set a session context (see this question for some code examples How to determine whether a transaction interceptor in Java EE 6 is active ).
My first concern was related to the connection pool. At first I thought that the default spread @PersistenceContext, provided by the Java EE, will be sufficient to ensure that all work on the same connection / transaction / the EntityManager, and I had to just cancel all the end of my interceptor (in the finally block) before the connection is returned to the pool. It seemed a bit fragile, but I thought it might work.
Then I learned that Hibernate has hibernate.connection.release_mode property ( Hibernate the docs about hibernate.connection.release_mode , Documents Red Hat on org.hibernate.ConnectionReleaseMode ) and that the default behavior and is recommended when using JTA transaction is to release connections after each operator (although the docs say something about the re-acquisition of the same basic compound, which just confused me).
Now I'm not even sure I can safely install something in the interceptor, which will only be visible to this user / operation, without running the risk that someone else will grab the same connection in the middle of my business method and create a mess with my custom context. I understand that the variables of the Oracle database session is stored for the connection, rather than a transaction or @PersistenceContext (personal data base is not aware of the context of the persistence in the end, and the connection can be used for multiple transactions).
I'm going to give up, because it looks increasingly fragile, as I learned more about the details of the implementation of all relevant technologies. Is it possible to make this concept a user context to work, or should I try a totally different approach? And how can I test / debug my implementation, to ensure that there are no problems with concurrency? I have not found any useful event listener to monitor the behavior of the structure and creating a program for stress testing the server works too hard to invest in something that I'm not sure that will still work.
I am using JBoss AS 7.1, EJB 3.1, Oracle 10g database and JPA 2.0 (supported by Hibernate, although we do not use any the Hibernate-specific API) data.