java - 跨 Hibernate session 和普通 jdbc 的单个事务

标签 java spring hibernate jdbc transactions

是否可以对 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/

相关文章:

java - 在android中使用JSON将字符串编码为UTF-8

java - Spring中如何saveOrUpdateAll(4.2.5.RELEASE)

java - 实体管理器最佳实践

java - 如何在 OpenXava 中创建下拉列表?

Java 泛型 - 具有 "extends"类型集合参数的方法拒绝有效参数?

java - 使用正则表达式 Java 获取重叠模式

java - 在 vaadin 框架上的可编辑网格中添加 DateField

java - Spring Boot 中的复杂身份验证

java - 如何发现隐式多重根

hibernate - 如何使用 Hibernate 中的序列作为 XML 映射中的属性