JPA or Query Interception
How do I intercept que开发者_高级运维ry being executed ? Basically, I have to intercept each and every query executed via EntityManger and log "NATIVE" sql to application server log file for analysis? can you please let me know is there any way to intro-spect before and after so I can log query and time it took to execute ?
Keep in mind that JPA engine could be hibernate or Open JPA so regardless of JPA implementation what is generic way to introspect query on serverside ?
Any help is greatly appreciated..
Thanks in Advance,
Bhavesh
if you are using hibernate you can use a hibernate interceptor, this intercepts all queries fired.
Session s = sessionFactory.openSession(new MyInterceptor());
this way any query fired from this session object will be intercepted, you can adjust the query as you please & also stop the query all together.
alternatively you could log all queries for a specific file (using log4j).
this requires you to set show_sql
to true in hibernate cfg file.
I dont see anything about debugging/logging in the JPA specification except for the fact that external properties that a implementation uses will need to use their own namespace (for e.g. com.hibernate.log).
Since the JPA provider needs to be mentioned in persistence.xml, can you not put the implementation specific properties in the same file (atleast thats how it works for Hibernate) ?
Every good DBMS should provide (too long) query logging feature.
But if you want to stay with Java try using JDBC Logger or Log4JDBC. Remember that full query logging may hit performance of your application, so do it in production carefully.
精彩评论