是否可以对 Hibernate session 和普通 JDBC 查询使用单个事务边界。 ? 两者的数据库和数据源配置相似。
最佳答案
是的。使用HibernateTransactionManager。以下摘自其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).
关于java - 跨 Hibernate session 和普通 jdbc 的单个事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351243/