2012-02-09 38 views
1

我想建立使用Hibernate和MyBatis的与Spring集成应用程序中使用Hibernate和MyBatis的。在原型中,我必须运行它们,但不要放弃它。我的Spring的应用上下文是:如何在同一应用程序

<?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> 

这样,每个人都有他自己的事务管理器和会话工厂。这是错误的,因为在嵌套事务可以运行多个事务,例如:

  1. Hibernate事务
  2. Hibernate事务
  3. MyBatis的交易
  4. Hibernate事务

    如果MyBatis的交易例外,它会回滚,但不会休眠。

    他读进来INT这个论坛上关于如何Hibernate和MyBatis的之间共享交易线索,但我不明白。

    有人能告诉我一些链接,或修复这方面的消息吗?

    请原谅我的英语。这很糟糕。

    在此先感谢。

    Pedro J.Fdez。 马德里。西班牙。

+0

从我的理解,你应该在你的服务水平不是你的DAO水平做您的交易管理,只要你的服务冒泡异常正确的事务管理应该适当为您服务类的工作,使用你的Hibernate DAO应与之关联到使用Mybatis的服务类。您应该知道,如果您使用Hibernate缓存和HQL,由于查询缓存如何与Hibernate一起工作,最终会导致数据不一致。 – Joe 2012-02-10 03:34:13

回答

3

使用谷歌搜索我找到了一个解决方案。大问题,简单的解决方案。

<!-- 
<bean id="txManagerMyBatis" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
--> 
<tx:advice id="txAdviceMyBatis" transaction-manager="txManagerHibernate"> 

基本上,评论德MyBatis的事务管理器,并把它贴到冬眠之一。

我希望这可以帮助别人。

相关问题