5 Replies Latest reply on Jun 2, 2011 6:27 AM by adamw

    How to close an open session with ENVERS?

    shyenuganti

      Here I have another question. If I close the session after inserting into the main table, the Envers is not doing the auditing saying THE SESSION IS ALREADY CLOSED. If I leave the session open without closing, the auditing is working.

       

      So will ENVERS close the open session after inserting into the audit tables? Is there any way to tell ENVERS to close the session after auditing?

        • 1. Re: How to close an open session with ENVERS?
          adamw

          Hmm, which version of Envers and Hibernate are you using? I think Envers should open a temporary session.


          Also, I'm not a Spring user, but is there a possibility to first commit the transaction, and then close the session?

           

          Adam

          • 2. Re: How to close an open session with ENVERS?
            shyenuganti

            Hibernate 3.5.5. I think ENVERS comes as part of the core in this version.

             

            Here is what I am doing:

             

            DAO Code:

            -------------------------------------------------------

            try{

                 session.openSession();

                 Object obj = session.get(...);

                 obj.set(...)

                 sesion.flush();

            }catch{

                 ...

            } finally{

                 session.close();

             

            ---------------------------------------------------

             

            If I close the session in the finally() block, ENVERS is not able to do the auditing. If I dont close it, it works fine !!

             

            I am using just Hibernate. Not with Spring. I am using a data source with CMT.

             

            How can I close the open session once I come out of the DAO ? Is there any way that I can tell ENVERS to close any oepn sessions once it finishes its tasks?

            • 3. Re: How to close an open session with ENVERS?
              shyenuganti

              Adam,

               

              If Envers uses temporary session to create audit records, why does it complain if I close the session manually ??

              • 4. Re: How to close an open session with ENVERS?
                adamw

                Please check with a newer version of Envers (3.6).

                 

                Adam

                • 5. Re: How to close an open session with ENVERS?
                  adamw

                  Ah, and how are you managing trasactions?

                  Basically here you should do it inside the session scope, that is start a TX when you open the sesison and commit before you close it.

                   

                  Adam