spring宣告式事務管理配置

qq_34302506發表於2018-03-08
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-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.xsd 
     
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- 載入配置檔案 -->
    <context:property-placeholder location="config.properties" />
    <!-- 指定spring註解注入層 -->
    <context:component-scan base-package="com.gnc" />
    <!-- 資料庫連線池管理 -->
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${db.driverClass}"></property>
        <property name="jdbcUrl" value="${db.jdbcUrl}"></property>
        <property name="user" value="${db.user}"></property>
        <property name="password" value="${db.password}"></property>


        <property name="initialPoolSize" value="${db.initialPoolSize}"></property>

        <!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
        <property name="maxIdleTime" value="${db.maxIdleTime}"></property>
        <!--連線池中保留的最大連線數。Default: 15 -->
        <property name="maxPoolSize" value="${db.maxPoolSize}"></property>
        <property name="minPoolSize" value="${db.minPoolSize}"></property>

        <!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
        <property name="acquireIncrement" value="${db.acquireIncrement}"></property>
        <!--兩次連線中間隔時間,單位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="${db.acquireRetryDelay}"></property>
        <!--定義在從資料庫獲取新連線失敗後重復嘗試的次數。Default: 30 -->
        <property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}"></property>
        <!--獲取連線失敗將會引起所有等待連線池來獲取連線的執行緒丟擲異常。但是資料來源仍有效保留,並在下次呼叫getConnection()的時候繼續嘗試獲取連線。如果設為true,那麼在嘗試 
            獲取連線失敗後該資料來源將申明已斷開並永久關閉。Default: false -->
        <property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}"></property>
    </bean>

    <!-- ================================事務相關控制================================================= -->

    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="c3p0DataSource"></property>
    </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="update*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="select*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="pc"
            expression="execution(public * com.gnc.service.*.*(..))" /> <!--把事務控制在Service層 -->
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    </aop:config>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="c3p0DataSource" />
        <property name="configLocation" value="MyBatis-Configuration.xml" />
    </bean>


    <bean class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.gnc.mapper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>



</beans>

相關文章