spirng整合mybatis的事务(接口开发配置)

spirng整合mybatis的事务注意事项:
下面是配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    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.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <!-- 自动扫描 (去除对controller的管理) -->
    <context:component-scan base-package="com.ming.shiro"
        use-default-filters="true">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!-- Spring中引入其他配置文件 -->
    <!-- <import resource="classpath*:/spring/job-timer.xml" /> -->

    <!-- 引入c3p0配置文件 -->
    <!-- <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean> -->
    <!--c3p0连接池  -->
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}" />
        <property name="jdbcUrl" value="${url}" />
        <property name="user" value="${username}" />
        <property name="password" value="${password}" />
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <property name="minPoolSize" value="${minPoolSize}" />
        <property name="initialPoolSize" value="${initialPoolSize}" />
        <property name="maxIdleTime" value="${maxIdleTime}" />
        <property name="acquireIncrement" value="${acquireIncrement}" />
        <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" /> 关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务
        <property name="autoCommitOnClose" value="false" />
    </bean> -->
    <!-- 引入DruidDataSource配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location" value="classpath:dbConfig.properties" /> </bean>
    <!-- 阿里 druid 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="driverClassName" value="${driverClassName}" />
        <property name="filters" value="${filters}" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="${maxActive}" />
        <!-- 初始化连接数量 -->
        <property name="initialSize" value="${initialSize}" />
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${minIdle}" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testWhileIdle" value="${testWhileIdle}" />
        <property name="testOnBorrow" value="${testOnBorrow}" />
        <property name="testOnReturn" value="${testOnReturn}" />
        <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
        <!-- 打开 removeAbandoned 功能 -->
        <property name="removeAbandoned" value="${removeAbandoned}" />
        <!-- 1800 秒,也就是 30 分钟 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
        <!-- 关闭 abanded 连接时输出错误日志 -->
        <property name="logAbandoned" value="${logAbandoned}" />
        <!--设定不默认提交(datasource交給org.mybatis.spring.SqlSessionFactoryBean,这个参数没有任何作用) -->
        <property name="defaultAutoCommit" value="${defaultAutoCommit}" />
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <!-- 接口和配置文件不在同一目录下时需要配置两个参数 mapperLocations(映射文件位置) basePackage(接口位置) -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 classpath:找不到的情况下可以配置为classpath*: -->
        <property name="mapperLocations" value="classpath*:com/ming/shiro/mapper/*.xml"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- DAO接口所在包名,Spring会自动查找其下的类(spring管理dao接口,注入dao可以按名称注入首字母小写) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置接口存储的包,用来扫描mapper接口 -->
        <property name="basePackage" value="com.ming.shiro.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--事务配置 -->
    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.RuntimeException" />
            <tx:method name="create*" propagation="REQUIRED" read-only="false" 
                rollback-for="java.lang.RuntimeException" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="get*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config proxy-target-class="true">
        <aop:pointcut id="pc"
            expression="execution(* com.ming.shiro.service.impl.*.*(..))" /> <!-- 把事务控制在Service层 -->
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    </aop:config>

</beans>  

关键点在这里:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 classpath:找不到的情况下可以配置为classpath*: -->
        <property name="mapperLocations" value="classpath*:com/ming/shiro/mapper/*.xml" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

将连接池交给org.mybatis.spring.SqlSessionFactoryBean管理,并通过其生成的实体为SqlSessionFactory类,并不是SqlSessionFactoryBean,这样生成的连接池工厂类(我认为是bug),这个工厂类生成的session一旦没有明确的事务管理就会自动提交,并且无论怎么设置连接池datasource的:

没有任何作用,我认为可能在生成SqlSessionFactory的底层将自动提交置为true,所以导致设置无效;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值