How to configure Spring to make JPA (Hibernate) and JDBC (JdbcTemplate or MyBatis) share the same transaction -
i have single datasource, use spring 3.0.3, hibernate 3.5.1 jpa provider , use mybatis 3.0.2 queries , app runs on tomcat 6. have hibernatedao , mybatisdao, when call both same method annotated @transactional looks don't share same transaction, different connections.
how can make them do?
i've tried getting connection datasourceutils.getconnection(datasource) , 1 used mybatis strange thought problem in mybatis config , can't use jpatransactionmanager. calling multiple times datasoruceutils.getconnection gives same connection always, ok.
after googling i've tried spring-instrument-tomcat's classloader (although don't know if tomcat uses :))
partial applicationcontext
<bean class="org.apache.commons.dbcp.basicdatasource" destroy-method="close" id="datasource"> <property name="driverclassname" value="${database.driverclassname}"/> <property name="url" value="${database.url}"/> <property name="username" value="${database.username}"/> <property name="password" value="${database.password}"/> </bean> <bean class="org.springframework.orm.jpa.jpatransactionmanager" id="transactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory"/> </bean> <tx:annotation-driven mode="aspectj" transaction-manager="transactionmanager"/> <bean class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" id="entitymanagerfactory"> <property name="datasource" ref="datasource"/> </bean> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="classpath:meta-inf/mybatis/mybatis-config.xml" /> </bean>
partial mybatis config
<settings> <setting name="cacheenabled" value="false" /> <setting name="usegeneratedkeys" value="false" /> <setting name="defaultexecutortype" value="reuse" /> <setting name="lazyloadingenabled" value="false"/> </settings>
partial persistence.xml
<persistence-unit name="persistenceunit" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider>
i've found solution here: what transaction manager should use jbdc template when using jpa ?
i'm using jpatransactionmanager , not 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 transaction (i.e. plain jdbc code working same datasource). allows mixing services access jpa , services use plain jdbc (without being aware of jpa)! application code needs stick same simple connection lookup pattern datasourcetransactionmanager (i.e. datasourceutils.getconnection(javax.sql.datasource) or going through transactionawaredatasourceproxy). note requires vendor-specific jpadialect configured.
after i've added jpavendoradapter entitymanagerfactory config works, both jdbctemplate query , mybatis runs in same transaction expected. based on javadoc guess jpadialect should enough it's 4 a.m. here won't try :)
<bean class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" id="entitymanagerfactory"> <property name="persistenceunitname" value="persistenceunit"/> <property name="datasource" ref="datasource"/> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="showsql" value="true" /> <property name="generateddl" value="true" /> <property name="databaseplatform" value="org.hibernate.dialect.postgresqldialect" /> </bean> </property> </bean>
Comments
Post a Comment