Reputation: 3683
Is it possible to use a single Transaction boundary for Hibernate Session and a plain JDBC query. ? The database and the datasource configurations are similar for both.
Upvotes: 2
Views: 1224
Reputation: 80176
Yes. Use HibernateTransactionManager
. Below is taken from its javadoc
This implementation is appropriate for applications that solely use Hibernate for transactional data access, but it also supports direct data source access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services that access Hibernate (including transactional caching) and services that use plain JDBC (without being aware of Hibernate)! Application code needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection or going through a TransactionAwareDataSourceProxy).
Note that to be able to register a DataSource's Connection for plain JDBC code, this instance needs to be aware of the DataSource (see setDataSource). The given DataSource should obviously match the one used by the given SessionFactory. To achieve this, configure both to the same JNDI DataSource, or preferably create the SessionFactory with LocalSessionFactoryBean and a local DataSource (which will be autodetected by this transaction manager).
Upvotes: 2