Spring事务管理之最实用的注解创建事务【Spring入门】

事务管理:最通俗的奖,就是把一些业务逻辑看成一个事务,事务具有原子性,一个事务中的业务逻辑要么全都执行成功,或者全都不执行。

用一个例子来讲解,就是去银行转账,A给B转1000元。第一步,A扣除1000元,第二步,B增加1000元。这两步必须在同一个事务中,不能只执行一步。

Spring事务管理可分为声明式事务管理以及编程式事务管理。

编程式事务管理需要在被管理事务的代码中加入处理事务的逻辑,使得代码不易维护,侵入性很强,但是可以显示的调用beginTransaction()、commit()、rollback等方法,处理粒度更细。不怎么常用,所以这里展示代码。

声明式事务管理是在事务方法或类外加@Transactional注解或使用增强代理类来实现,这里展示最常用的注解式。

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

    <!--开启自动扫描-->
    <context:component-scan base-package="com.yykj"/>
    <!--加载jdbc配置文件-->
    <context:property-placeholder location="jdbc.properties"/>

    <!-- 阿里 druid 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <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 功能 -->
        <!-- 1800 秒,也就是 30 分钟 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
        <!-- 关闭 abanded 连接时输出错误日志 -->
        <property name="logAbandoned" value="${logAbandoned}"/>
    </bean>

    <!--开启事务管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--jdbc模板,也是在spring容器中的,这样对数据库的操作才能受到spring的事务管理,不然Spring事务管理无效-->
    <bean  id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
再看被事务管理的方法:

package com.yykj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountDao accountDao;

    @Transactional//注解方法
    public void transfer() {
        accountDao.outMoney("aaa", 10.0);
        int x = 4/0;
        accountDao.inMoney("bbb", 10.0);
    }
}

这只是部分代码,也是事务管理要关注的代码,事务一般加在业务逻辑代码上。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值