My question was specific to some problem scenario where Transaction being started from Spring Container by using EntityManager
then I am getting hibernate session out of EntityManager
.
The overall flow:
In the application
entityManager.createNativeQuery(searchQuery)
is there to select some data.I need to get the hibernate session out of
entityManager
then in the session I want to setnls_comp
/nls_sort
or I can execute the query in theentityManager
itself. e.g. alter session setnls_comp=linguistic
.After its being done I want to disable these option once again back to its previous state. Most probably by closing the hibernate session or some mechanism. Is it the right approach?
Should I start another transaction for the 2 alter command also (I tried it gave some exception). Please let me know how to achieve it and how hibernate session are being managed inside a spring initiated transaction or I can achieve the enable/disable with entityManager
itself.
This is how you can do it:
You don't need to call session.close(), thins will be taken care of Spring automatically when closing the current Transaction. It's rare that you have to close the session manually.
If you want to execute a logic for the current settings, it's better to write an AOP aspect to execute 1. and 2. then let the call go through only to reset it back to the previous values in the finally block. You can define your own annotation to mark all the service methods you'd like to have this logic enabled. Spring pointcuts support annotation-based selection anyway.