Spring的事务控制实现方法

 更新时间:2022年07月29日 09:10:51   作者:兴奋の大公猴  
这篇文章主要为大家详细介绍了Spring的事务控制实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Spring的事务控制实现,供大家参考,具体内容如下

提示:这里使用的是xml的方式配置事务的

前言

例:当银行转账的时候,如果转账和收款的一方出现问题,那么这次的转账则不成功,此处如果没有事务管理,那么可能出现一方已经转账成功,另一方却没有收款的问题。为了避免此问题,应当使用到事务管理。

提示:以下是本篇文章正文内容,下面案例可供参考

一、Spring声明式事务控制

示例:

二、使用步骤

1.xml配置

配置如下(示例):

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

    <!--  1.加载jdbc.properties  -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--  2.配置数据源对象  -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="accountDao" class="com.codejams.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

<!--    <bean class="com.codejams.controller.DemoTest">-->
<!--        <property name="accountService" ref="accountService"/>-->
<!--    </bean>-->

    <!--目标对象  内部的方法就是切点-->
    <bean id="accountService" class="com.codejams.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--通知  事务的增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--设置事务的属性信息的-->
        <tx:attributes>
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的aop织入-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.codejams.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>

2.AccountDaoImpl代码

代码如下(示例):

package com.codejams.dao.impl;

import com.codejams.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }

    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}

3.AccountServiceImpl代码

代码如下(示例):

package com.codejams.service.impl;

import com.codejams.dao.AccountDao;
import com.codejams.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;

public class AccountServiceImpl implements AccountService {

    //@Autowired
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}

结果展示

手动在这里制造一个异常

此时数据库的状态为两人均为5000

执行后,可以看见这里报了异常,并且数据库的两人的money都没有更改

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 在启动后台 jar包时,使用指定的 application.yml操作

    在启动后台 jar包时,使用指定的 application.yml操作

    这篇文章主要介绍了在启动后台 jar包时,使用指定的 application.yml操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用

    Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用

    这篇文章主要介绍了Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Java算法之位图的概念和实现详解

    Java算法之位图的概念和实现详解

    这篇文章主要介绍了Java算法之位图的概念和实现详解,位图可以利用每一位来对应一个值,比如可以利用int类型的数去存储0~31这个集合的数字,如果该集合内的数字存在,则把对应的位设置位1默认为0,需要的朋友可以参考下
    2023-10-10
  • 详解SpringBoot之集成Spring AOP

    详解SpringBoot之集成Spring AOP

    本篇文章主要介绍了详解SpringBoot之集成Spring AOP,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • @Transactional注解异常报错之多数据源详解

    @Transactional注解异常报错之多数据源详解

    这篇文章主要介绍了@Transactional注解异常报错之多数据源详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Sping中如何处理@Bean注解bean同名的问题

    Sping中如何处理@Bean注解bean同名的问题

    这篇文章主要介绍了Sping中如何处理@Bean注解bean同名的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 使用Idea或Datagrip导入excel数据的方法

    使用Idea或Datagrip导入excel数据的方法

    这篇文章主要介绍了使用Idea或Datagrip导入excel数据的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Java中Comparator升序降序的具体使用

    Java中Comparator升序降序的具体使用

    本文主要介绍了Java中Comparator升序降序的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java实现二维码功能的实例代码

    Java实现二维码功能的实例代码

    今天这篇文章,主要是利用Java实现二维码功能,本文思路清晰,需要的朋友参考下
    2017-02-02
  • Spring注解之@Value详解

    Spring注解之@Value详解

    这篇文章主要介绍了Spring注解之@Value详解,@Value可以修饰属性、方法、参数、注释类型,编译器会将 @Value注解的信息保留在 .class 文件中,并且能被虚拟机读取,需要的朋友可以参考下
    2024-01-01

最新评论