2 Replies Latest reply on Jun 29, 2017 9:45 PM by marcuscf

    Setting context for a JPA connection - is this safe?

    marcuscf Newbie



      I am using JBoss AS 7.1.x and I need to set some user information (in a database-specific way) before every operation and reset it just before the connection is returned to the connection pool. I intend to use this information to generate better logging in  database triggers, so I will be able to see who did every operation, instead of logging everything as performed by "JBoss" (or other generic name).


      We use JPA and some JDBC in @Stateless beans.


      What worries me is:

      Could the connection be released between two JPA queries in the same transaction, and used for something else by the container? (the Hibernate documentation says that yes, a connection can be released in this situation, but it is not clear whether it might be used by another transaction or thread) I guess plain JDBC is safe if we set everything at the beginning and reset everything in a finally block (preferably in a @AroundInvoke interceptor, because I want it to be easy to use), but I'm not sure about JPA.


      There's much more detail in this Stack Overflow question (too bad I couldn't get a definite answer there):



      So, can this approach work reliably? If yes, would it be future-proof and portable? If not, do you have another suggestion as how to pass extra information to database triggers?


      Thank you very much.