title: Is there a way to force Hibernate to use new connection for next transaction within a Spring application?
body: <p>In Spring applications (that use Spring's declarative transaction management) when using JPA/Hibernate, when a transaction is first started within a request, Hibernate session is created and a database connection will be obtained from a datasource in order to execute this first transaction. Any subsequent transaction within the request will reuse the session and the connection. Is there a way to force Hibernate (or Spring's transaction management system) to obtain a new connection from the datasource for the subsequent transaction?</p>
  
  <p><strong>Background info and what I am trying to achieve</strong></p>
  
  <p>I'm using a custom implementation of AbstractRoutingDatasource to route database requests between a master and a replicated slave (read-only) database. I've created custom annotation with datasource routing information and AOP Aspect that intercepts the methods annotated with my custom annotation in order create a framework where one could add additional datasource routing annotation in addition to @Transactional in order to select a datasource (master or slave) on which the transaction should be executed. Aspect informs the routing datasource that next connection requested within current thread should go to a specific datasource (as defined in the annotation) before transaction starts. The problem is this only works on the first transaction because Hibernate caches connection and does not request new one for next transaction.</p>