事务管理的小案例

创建数据库:

create database transaction1;
use transaction1;
create table account(id int primary key auto_increment,
username varchar(30),
money int);

insert into account(username,money) values('fly',80);
insert into account(username,money) values('wang',100);

导入jar包

-   核心:4+1
-   aop : 4 (aop联盟、spring aop、aspectj规范、spring aspect)
-   数据库:2  (jdbc/tx)
-   驱动:mysql
-   连接池:c3p0

这里写图片描述

创建DAO:

package com.fly.transaction.dao;

public interface AccountDao {

    void out(String user,int money);

    void in(String user,int money);
}
package com.fly.transaction.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class TransactionDao extends JdbcDaoSupport implements AccountDao{

    @Override
    public void out(String user, int money) {
        this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,user);
    }

    @Override
    public void in(String user, int money) {
        this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,user);
    }

}

创建Service:

package com.fly.transaction.service;

public interface TransactionService {

    void transfer(String outer, String inner, int money);
}
package com.fly.transaction.service;

import com.fly.transaction.dao.TransactionDao;


public class TransactionServiceImp implements TransactionService {

    private TransactionDao dao;

    public void setTransactionDao(TransactionDao dao) {
        this.dao = dao;
    }


    @Override
    public void transfer(String outer, String inner, int money) {
        dao.out(outer, money);

        dao.in(inner, money);
    }

}

配置文件applicationContext:

<?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: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/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 1 datasource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transaction1"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>

    <!--  这里注意id不能乱写,最好写TransactionDao首字母小写即transactionDao  -->
    <bean id="transactionDao" class="com.fly.transaction.dao.TransactionDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 3 service -->
    <bean id="accountService" class="com.fly.transaction.service.TransactionServiceImp">
        <property name="transactionDao" ref="transactionDao"></property>
    </bean>

</beans>

测试类:

package com.fly.transaction.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fly.transaction.service.TransactionServiceImp;

public class TestDemo {

    @Test
    public void test(){
        String xmlPath  = "appcationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
        TransactionServiceImp bean = (TransactionServiceImp) context.getBean("accountService");
        bean.transfer("fly", "wang", 10);
    }
}

工厂bean 生成代理:半自动

spring提供 管理事务的代理工厂bean TransactionProxyFactoryBean
1.getBean() 获得代理对象
2.spring 配置一个代理

spring配置

<?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: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/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 1 datasource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transaction1"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>

    <!-- 2 dao  -->
    <bean id="transactionDao" class="com.fly.transaction.dao.TransactionDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <!-- 4 service 代理对象 
        4.1 proxyInterfaces 接口 
        4.2 target 目标类
        4.3 transactionManager 事务管理器
        4.4 transactionAttributes 事务属性(事务详情)
            prop.key :确定哪些方法使用当前事务配置
            prop.text:用于配置事务详情
                格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
                    传播行为        隔离级别    是否只读        异常回滚        异常提交
                例如:
                    <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 默认传播行为,和隔离级别
                    <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 只读
                    <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop>  有异常扔提交
    -->

    <!-- 3 service -->
    <bean id="proxyAccountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="proxyInterfaces" value="com.fly.transaction.service.TransactionService"></property>
        <property name="target" ref="transactionServiceImp"></property>
        <property name="transactionManager" ref="txManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
            </props>
        </property>
    </bean>


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

    <bean id="transactionServiceImp" class="com.fly.transaction.service.TransactionServiceImp">
        <property name="transactionDao" ref="transactionDao"></property>
    </bean>


</beans>

再次测试:

package com.fly.transaction.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fly.transaction.service.TransactionService;

public class TestDemo {

    @Test
    public void test(){
        String xmlPath  = "appcationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
        // 接口
        TransactionService bean = (TransactionService) context.getBean("proxyAccountService");
        bean.transfer("fly", "wang", 10);
    }
}

AOP 配置基于xml
在spring xml 配置aop 自动生成代理,进行事务的管理
1.配置管理器
2.配置事务详情
3.配置aop

<!-- 4 事务管理 -->
    <!-- 4.1 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 4.2 事务详情(事务通知)  , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
        <tx:attributes> 用于配置事务详情(属性属性)
            <tx:method name=""/> 详情具体配置
                propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
                isolation 隔离级别
    -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/>
    </aop:config>

AOP配置基于注解
1.配置事务管理器,将并事务管理器交予spring
2.在目标类或目标方法添加注解即可 @Transactional

spring配置

<!-- 4 事务管理 -->
    <!-- 4.1 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 4.2 将管理器交予spring 
        * transaction-manager 配置事务管理器
        * proxy-target-class
            true : 底层强制使用cglib 代理
    -->
    <tx:annotation-driven transaction-manager="txManager"/>

service 层

@Transactional
public class AccountServiceImpl implements AccountService {

事务详情配置
这里写图片描述

@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值