I want to build an application using Hibernate and MyBatis integrate with Spring. In the prototype i’ve got to run them, but not toghether. My application context of Spring is:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : applicationContext-spring.xml
Created on : 26 de diciembre de 2012, 15:49
Author : Pedro Fdez
Description:
Fichero de configuración de Spring
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
<context:annotation-config />
<context:component-scan base-package="com.administracion.model.dao.implementations" />
<tx:annotation-driven transaction-manager="txManagerHibernate"/>
<aop:aspectj-autoproxy />
<!-- ............................ -->
<!-- Configuración de datasource -->
<!-- ............................ -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- .......................... -->
<!-- Configuración de Hibernate -->
<!-- .......................... -->
<!-- SessionFactory de Hibernate -->
<bean id="sessionFactoryHibernate"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.administracion.model.pojos</value>
</list>
</property>
</bean>
<!-- Gestor transaccional de Hibernate -->
<bean id="txManagerHibernate"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryHibernate"/>
</bean>
<!-- ........................ -->
<!-- Configuración Mybatis -->
<!-- ........................ -->
<!-- Gestor transaccional de MyBatis -->
<bean id="txManagerMyBatis"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdviceMyBatis" transaction-manager="txManagerMyBatis">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointCut"
expression="execution(* com.administracion.model.dao.interfaces.*.*(..))" />
<aop:advisor advice-ref="txAdviceMyBatis" pointcut-ref="transactionPointCut" />
</aop:config>
<!-- SessionFactory de MyBatis -->
<bean id="sqlSessionFactoryMyBatis" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:conf/mybatis/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- MapperFactory de Mybatis -->
<bean id="profesionMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryMyBatis" />
<property name="mapperInterface" value="com.administracion.model.dao.mappers.IProfesionMapper" />
</bean>
<bean id="profesionService" class="com.administracion.model.dao.implementations.ProfesionDaoImpl">
<property name="profesionMapper" ref="profesionMapper" />
</bean>
<!-- Declaramos la exportación del servicio vía RMI -->
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="registryPort" value="${rmi.port.default}"/>
<!-- Interface del servicio que exportamos -->
<property name="serviceInterface" value="com.administracion.model.dao.interfaces.IProfesionDao"/>
<!-- Nombre con que el servicio se va a llamar desde afuera -->
<property name="serviceName" value="ProfesionService"/>
<!-- Nombre del bean de la implementación que le hemos dado en el contexto de spring -->
<property name="service" ref="profesionService"/>
</bean>
</beans>
In this way, each one have his own transaction manager and session factory. It’s wrong because in a nested transaction can run several transactions, for example:
- hibernate transaction
- hibernate transaction
- mybatis transaction
-
hibernate transaction
If mybatis transaction make an exception, it does rollback, but not the hibernate one.
He’s readen int this forum a thread about how to share transaction between Hibernate and MyBatis, but I don’t understand it.
Can Somebody tell me about some link, or any information for fix this, please?
Excuse me for my English. It’s very bad.
Thanks in advance.
Pedro J.Fdez.
Madrid. Spain.
Googling I’ve found a solution for it. Big problems, simple solutions.
Basically, to comment de MyBatis transaction manager and to stick it to hibernate one.
I hope this help somebody.