Is it possible to place a hook at the low level of the database connection or data source and execute a query just before any other query is executed?
Hello.
I'd like to be able to intercept all database queries in hibernate and inject execute one simple query before the original query gets sent to the database: SET @SomeSessionVariable = 123346;
.
Since hibernate uses a lifecycle for its managed entities, I find it a challenge to achieve what I need.
In my spring boot application, I'd like to audit data changes using triggers. I want to be able to associate a change with the currently logged in user in the spring application and the only way to do that is to send the id of that user to the database along with any queries, but just before the query gets executed so that the trigger can see the variable in the current connection session.
I'm using MySQL and it seems like there's no built in way of doing this. Postgresql does seem to support sending client contexts to the DBMS.
The application is quite huge to refactor to manually send the id.
Do you know any other place I could place a global hook in hibernate configuration to be able to intercept both native and managed queries?