song405528077
猫哆哩的歌声
2016-07-02 16:32

Mybatis + SpringData-Jpa 事物提交问题

现在有两个Maven工程,工程A使用mybatis,单独配置了一套transactionManager,
工程B使用SpringData-Jpa,也单独配置了一套transactionManager,两个工程使用mysql同一数据库。
在工程B中引用了工程A,然后通过调用工程A的deleteService发现事物不能提交,查询数据是ok的,并且deleteService的sql都可以正常打印,只是没有提交事物。
但是工程A单独运行时没有该问题

以下是工程A数据源配置文件信息:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 引入properties配置文件 -->
<context:property-placeholder
    ignore-unresolvable="true" location="classpath*:logic.properties" />

<!-- 扫描路径 -->
<context:component-scan base-package="logic">
</context:component-scan>

<context:annotation-config />

<!-- 可通过注解设置Spring Task -->
<task:annotation-driven />

<aop:aspectj-autoproxy proxy-target-class="true">
</aop:aspectj-autoproxy>

<bean id="taskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="${task.core_pool_size}" />
    <property name="maxPoolSize" value="${task.max_pool_size}" />
    <property name="queueCapacity" value="${task.queue_capacity}" />
    <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.classname}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="60000" />
    <property name="maxIdle" value="20" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="180" />
    <property name="minIdle" value="3" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="validationQuery" value="SELECT NOW()" />
    <property name="validationQueryTimeout" value="1" />
    <property name="timeBetweenEvictionRunsMillis" value="30000" />
    <property name="numTestsPerEvictionRun" value="20" />
    <property name="defaultAutoCommit" value="false" />
</bean>

<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath*:logic/*/mapper/xml/*.xml" />
</bean>

<!-- 事务管理 -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 开启注解管理事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sessionFactory" />
    <property name="basePackage" value="logic.*.mapper" />
</bean>

以下是工程B数据源配置文件信息:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<bean id="mainDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.classname}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="60000" />
    <property name="maxIdle" value="20" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="180" />
    <property name="minIdle" value="3" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="validationQuery" value="SELECT NOW()" />
    <property name="validationQueryTimeout" value="1" />
    <property name="timeBetweenEvictionRunsMillis" value="30000" />
    <property name="numTestsPerEvictionRun" value="20" />
    <property name="defaultAutoCommit" value="false" />
</bean>

<cache:annotation-driven cache-manager="cacheManager" />

<bean id="ehCacheManager"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/ehcache.xml" />
    <property name="shared" value="true" />
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehCacheManager" />
</bean>

<bean id="adminEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="mainDataSource" />
    <property name="packagesToScan" value="admin.entity" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="database" value="MYSQL" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

<!-- SPRING - JPA -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="adminTransactionManager">
    <property name="entityManagerFactory" ref="adminEntityManagerFactory" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

<tx:annotation-driven transaction-manager="adminTransactionManager" />

<jpa:repositories base-package="admin.repository"
    entity-manager-factory-ref="adminEntityManagerFactory"
    transaction-manager-ref="adminTransactionManager"></jpa:repositories>

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐