How to intercept the generated Hibernate SQL file?

For a security system that works like an older brother (like observable mandatory access control), we need to intercept and process all the selection commands that sleep mode generates. We save the user, timestamp, and sql selection in the database to allow profiling with other tools. The information allows you to determine what the user tried to see. For selection operators, predefined properties are valuable. We need a complete SQL statement containing all the parameters.

Is there any listener or interceptor where we can team up and deal with all these things? The biggest unsolved problem is the collection of operator parameters.

thanks

+6
java hibernate
source share
2 answers

Actual parameter values ​​become available (as far as I know) when the org.hibernate package logging level is set to DEBUG and with hibernate.show_sql a set of properties. Use JDBCAppender if you want the log output in the database itself.

Alternatively, you can look at the log4jdbc project , which states the following:

In the registered release for prepared statements, binding arguments are automatically inserted into the SQL output. This greatly improves readability and debugging for many cases.

If this does not work, you can find out if P6Spy can in your situation . On WebLogic Server, equivalent functionality is achieved using WebLogic JDBC Spy , which is out of the box with WebLogic JDBC drivers for specific databases. Both of them are written to System.out, and not to the database (unless I am mistaken), so this may not be so useful.

+3
source share

You can use Interceptor.prepareSQL() (3.1+) to intercept prepared statements.

I do not think that you can get the actual parameters without falling into the abstraction layer. A possible solution would be to use a JDBC proxy driver (see P6Spy ).

Hope this helps.

+3
source share

All Articles