Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern to Log all interactions with a database by using interceptor on bean
    text
    copied!<p>I want to be able to log the amount of time any query takes on my db. I'm using MyBatis to query the DB.</p> <p>Currently, I have a stateless bean that has methods on it like select, update, etc. and clients of my bean (through @EJB annotation) call these methods. On top of this I have a class level interceptor on the stateless db interaction bean that does my logging. </p> <p>With any call to the stateless db interaction bean i open a session, run the user's query, and then close the session, all in a try finally block. This is nice because then the EJB client doesn't have to worry about closing sessions or logging how long the query took. </p> <p>A problem arises when an EJB client wants to make two queries to the same db. I can't use an XA datasource because the vendor doesn't support it. </p> <p>I have a workaround for this where I implemented a similar stateless db interaction bean that doesn't open or close sessions in each select, update, etc. method, but rather through the use of an openSession() and closeSession() method. </p> <p>My concern is that this is still a stateless bean and it could get destroyed in the middle of a client's transaction causing a rollback. Is that the case? Should I make it a stateful bean? </p> <p>I also wrote an interceptor to actually call openSession() and closeSession() for the user so they can just annotate their class or method. </p> <p>Does this seem like a good pattern or is there a common pattern to handle this case that I am missing? Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload