Spring boot app, need to set a database session variable before every transaction

2020-04-01 spring hibernate spring-boot jpa

I have a spring boot application that uses Hibernate/JPA entities. I need to run a bit of SQL to set a database session variable prior to each transaction. This session variable controls row level security at the database layer and so the SQL will use a web session parameter which I can retrieve from the spring web session context. I will need this bit of sql to run essentially before every transaction.

I'd like to be able to do this in a way that does not effect every additional piece of business logic. i.e. I'd rather not have to call the method manually from every repository class.

I saw the ConnectionPreparer class in the spring-data-jdbc-ext project (https://github.com/spring-projects/spring-data-jdbc-ext/blob/master/spring-data-jdbc-core/src/main/java/org/springframework/data/jdbc/support/ConnectionPreparer.java). However it appears this class is only called when the connection is first instantiated, not prior to every transaction.