Spring中管理数据库事务的配置使用详解

一、使用xml进行配置

1、导入Spring框架需要的基础业务数据包

Spring中管理数据库事务的配置使用详解_第1张图片

2、配置通知

配置Service层中的哪些方法需要使用到事务,一般开发使用组的形式,即如下
Spring中管理数据库事务的配置使用详解_第2张图片

3、将通知织入目标

Spring中管理数据库事务的配置使用详解_第3张图片

4、完整的applicationContext.xml


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    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-4.2.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


    <context:property-placeholder location="classpath:db.properties" />


    
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource" >property>
bean>

<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
    <property name="transactionManager" ref="transactionManager" >property>
bean>


<tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
        
        <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="transferMoney" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    tx:attributes>
tx:advice>



<aop:config  >
    
    <aop:pointcut expression="execution(* com.onex.service.imp.*ServiceImp.*(..))" id="txPc"/>
    
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
aop:config>

    

    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}">property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="user" value="${jdbc.user}">property>
    bean>

    <bean name="accountDao" class="com.onex.dao.imp.AccountDaoImp">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

    <bean name="accountService" class="com.onex.service.imp.AccountServiceImp">
        <property name="accountDao" ref="accountDao">property>
    bean>

beans>

二、使用注解配置

1、在xml中开启注解管理aop事务

image.png

2、在相应的Service层上进行使用注解

表示该方法配置的事务
方法上使用.png

表示该类下所有的方法都是用该事务(与方法不冲突)
类名上全局使用.png

3、完整的applicationContext.xml中的配置


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    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-4.2.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


    <context:property-placeholder location="classpath:db.properties" />

    
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}">property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="user" value="${jdbc.user}">property>
    bean>


<context:property-placeholder location="classpath:db.properties"  />


<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource" >property>
bean>

<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
    <property name="transactionManager" ref="transactionManager" >property>
bean>


<tx:annotation-driven/>

    <bean name="accountDao" class="com.onex.dao.imp.AccountDaoImp">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

    <bean name="accountService" class="com.onex.service.imp.AccountServiceImp">
        <property name="accountDao" ref="accountDao">property>
    bean>

beans>

你可能感兴趣的:(Spring中管理数据库事务的配置使用详解)