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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--    引入properties文件-->
    <context:property-placeholder location="jdbc.properties"/>
<!--进行扫包-->
    <context:component-scan base-package="com.beyondzy.spring"></context:component-scan>
<!--    注册事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    注册jdbc模块-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    注册数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${url}"></property>
        <property name="driverClassName" value="${driverName}"></property>
    </bean>
    <bean id="template" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>
</beans>

使用默认方式进行事务管理

 public void transfer(String to, String from, int money) {
        //设置默认事务定义
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        //设置事务状态
        TransactionStatus transactionStatus = manager.getTransaction(definition);

        String sql_to = "update user set money = money - ? where name = ?";
        String sql_from = "update user set money = money + ? where name = ?";
        template.update(sql_to,money,to);
        template.update(sql_from,money,from);
        //回滚到事务状态
        manager.rollback(transactionStatus);
    }

使用模块的方式进行事务管理

 public void transfer1(String to, String from, int money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                String sql_to = "update user set money = money - ? where name = ?";
                String sql_from = "update user set money = money + ? where name = ?";
                template.update(sql_to,money,to);
                template.update(sql_from,money,from);
            }
        });
    }

aop进行事务管理

  1. 实现MethodInterceptor,创建通知类
package com.beyondzy.spring.aop;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

//方法拦截器
public class TxAdvice implements MethodInterceptor {
    private PlatformTransactionManager manager;

    public void setManager(PlatformTransactionManager manager) {
        this.manager = manager;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        //设置默认事务定义
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        //设置事务状态
        TransactionStatus transactionStatus = manager.getTransaction(definition);
        Object returnval = null;
        try{
            returnval = methodInvocation.getMethod().invoke(methodInvocation.getThis(), methodInvocation.getMethod());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        manager.rollback(transactionStatus);

        return returnval;
    }
}

  1. 配置事务
<bean id="txAdvice" class="com.beyondzy.spring.aop.TxAdvice">
        <property name="manager" ref="transactionManager"></property>
    </bean>
    <aop:config>
        <aop:pointcut id="" expression=""/>
        <aop:advisor pointcut="" advice-ref="txAdvice"></aop:advisor>
    </aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值