栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何配置Spring使JPA(Hibernate)和JDBC(JdbcTemplate或MyBatis)共享同一事务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何配置Spring使JPA(Hibernate)和JDBC(JdbcTemplate或MyBatis)共享同一事务

我使用的是JpaTransactionManager,而不是DataSourceTransactionManager。
JavaDoc http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/jpa/JpaTransactionManager.html

This transaction manager also supports direct DataSource access within a transaction (i.e. plain JDBC pre working with the same DataSource). This allows for mixing services which access JPA and services which use plain JDBC (without being aware of JPA)! Application pre needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection(javax.sql.DataSource) or going through a TransactionAwareDataSourceProxy). Note that this requires a vendor-specific JpaDialect to be configured.

在我将jpaVendorAdapter添加到我的entityManagerFactory配置中之后,一切正常,JdbcTemplate查询和MyBatis都按预期在同一事务中运行。基于JavaDoc,我猜jpaDialect应该足够了,但是现在是4点,所以我现在就不尝试了:)

<bean  id="entityManagerFactory">    <property name="persistenceUnitName" value="persistenceUnit"/>    <property name="dataSource" ref="dataSource"/>    <property name="jpaVendorAdapter">        <bean > <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />        </bean>    </property></bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号