Spring事务管理——AOP注解事务管理

数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成。

 

一个数据库事务通常包含了一个序列的对数据库的读/写操作。它的存在包含有以下两个目的:

为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同时提供了数据库即使在异常状态下仍能保持一致性的方法。 当多个应用程序在并发访问数据库时,可以在这些应用程序之间提供一个隔离方法,以防止彼此的操作互相干扰。

当事务被提交给了DBMS(数据库管理系统),则DBMS(数据库管理系统)需要确保该事务中的所有操作都成功完成且其结果被永久保存在数据库中,如果事务中有的操作没有成功完成,则事务中的所有操作都需要被回滚,回到事务执行前的状态;同时,该事务对数据库或者其他事务的执行无影响,所有的事务都好像在独立的运行。

特性

并非任意的对数据库的操作序列都是数据库事务。数据库事务拥有以下四个特性,习惯上被称之为ACID特性。

原子性(Atomicity):事务作为一个整体被执行,包含在其中的对数据库的操作要么全部被执行,要么都不执行。
一致性(Consistency):事务应确保数据库的状态从一个一致状态转变为另一个一致状态。一致状态的含义是数据库中的数据应满足完整性约束。
隔离性(Isolation):多个事务并发执行时,一个事务的执行不应影响其他事务的执行。
持久性(Durability):已被提交的事务对数据库的修改应该永久保存在数据库中。

Spring下由三种途径对事物进行管理:编程式事务管理声明式事务管理AOP事务管理。其中AOP事务管理又分AOP注解事务管理AOP XML配置两种

AOP注解事务管理

spring.xml(spring的配置文件,不一定叫spring.xml)文件中的配置:

1.配置数据源(我使用的是tddl,改为自己的数据源)

<!-- datasource begin -->
<bean id="tddlGroupDataSource" class="com.taobao.tddl.jdbc.group.TGroupDataSource"
      init-method="init">
    <property name="appName" value="${alibaba.intl.apaas.db.app.name}"/>
    <property name="dbGroupKey" value="${alibaba.intl.apaas.db.group.key}"/>
</bean>
<!-- datasource end -->

2. 配置sping的事务管理

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

    <!--  Transaction begin  -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="tddlGroupDataSource" />
    </bean>
    <!-- transaction end -->

</beans>

<tx:annotation-driven transaction-manager="transactionManager" />这句话加上的话,在spring获取bean的时候,要使用接口!

3.配置SqlMapClient(我的持久层使用了ibatis,所以需要配置这个)

<bean id="apaasSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="biz/sql-map-config.xml"/>
        <property name="dataSource" ref="tddlGroupDataSource"/>
</bean>

sql-map-config.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
    <settings cacheModelsEnabled="true" enhancementEnabled="true"
        errorTracingEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
        maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
    <sqlMap resource="sqlmap/sqlmap-apaas-label.xml" />
    <!-- <sqlMap resource="sqlmap/sqlmap-apaas-label-sorce.xml" /> -->

</sqlMapConfig>

sqlmap-apaas-label-sorce.xml配置就不写了,就是ibatis的jdbc操作。

4.配置dao和service

<bean id="appLabelDao" class="com.alibaba.intl.apaas.common.monitor.dao.impl.AppLabelDaoImpl"
      parent="baseSqlMapClientDAO"/>
<bean id="appLabelServiceBean" class="com.alibaba.intl.apaas.common.monitor.service.impl.AppLabelServiceImpl">
    <property name="appLabelDao">
        <ref bean="appLabelDao"/>
    </property>
</bean>

AppLabelServiceImpl的Java类的配置:

public class AppLabelServiceImpl implements AppLabelService {
    private AppLabelDao appLabelDao;
      @Transactional
    public Integer insertAppLabel() {
                appLabelDao.jdbcOperate1();
                appLabelDao.jdbcOperate2();
                appLabelDao.jdbcOperate3();
                appLabelDao.jdbcOperate4();
    }
}

这里使用@Transactional这个注解,这个注解的原理和具体使用方式我会在别的文章里面介绍。 在方法insertAppLabel上添加@Transactional这个注解,声明该方法要在一个事务中进行处理。 这个方法一共有四个jdbc操作jdbcOperate1jdbcOperate2jdbcOperate3jdbcOperate4,如果在整个insertAppLabel方法的任何地方抛出runtimeException,就会执行rollback。即所有操作将被回滚。

测试类:

public class AppLabelServiceImplTest {
    private static AppLabelService service;
    @BeforeClass
    public static void beforeClass() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:biz/sca-appinfo-provider-context.xml");
        service = (AppLabelService)context.getBean("appLabelServiceBean");
    }
    @Test
    public void testInsert(){
        service.insertAppLabel( );
    }
}

PS:service = (AppLabelService)context.getBean("appLabelServiceBean");此处应该注意,一定要用AppLabelService这个接口,二不能用AppLabelServiceImpl这个实现类。 因为Spring文档中有写:Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理,如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理。所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理。

如果不使用AppLabelService而使用AppLabelServiceImpl的话,在执行程序的时候就会报错:classCastException

这里,service也不能是new出来的,必须要用spring进行管理。

参考资料:

Spring Transaction Management

 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring事务回滚是在Spring框架中提供的一种机制,用于在事务发生异常或错误时撤销已执行的操作,使数据回滚到事务开始之前的状态。在Spring中,有几种方式可以实现事务回滚。 首先是编程式事务,这种方式需要在代码中手动开启事务、手动提交和手动回滚。虽然可以灵活控制事务的执行,但代码会变得冗长和重复。 其次是声明式事务,通过配置SpringAop来实现事务的控制,大大简化了编码的复杂性。需要注意的是,切入点表达式必须正确配置。 还有注解事务,直接在Service层的方法上加上@Transactional注解即可实现事务控制。这种方式相对简单,是我个人比较喜欢使用的方式。 通常情况下,事务回滚的原因是由于抛出了RuntimeException异常。在声明式事务注解事务中,当被切面切中或者是加了注解的方法中抛出了RuntimeException异常时,Spring会进行事务回滚。但如果抛出的异常不属于运行时异常,比如IO异常,事务是不会回滚的。 常见的导致事务不回滚的原因有以下几种: 1. 声明式事务配置切入点表达式写错,没有切中Service中的方法。 2. Service方法中捕获了异常,但只是打印了异常信息而未手动抛出RuntimeException异常。 3. Service方法中抛出的异常不属于运行时异常,因为Spring默认情况下只会回滚运行时异常。 以上是关于Spring事务回滚的一些介绍和常见原因。希望对您有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring事务管理——回滚(rollback-for)控制](https://blog.csdn.net/ryelqy/article/details/80019106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [spring事务回滚机制你懂得多少?](https://blog.csdn.net/weixin_45985053/article/details/125958535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值