首页 > 其他分享 >Spring事务的三种实现方式

Spring事务的三种实现方式

时间:2022-10-10 21:35:19浏览次数:62  
标签:事务 http Spring MyException www springframework 三种 org

spring中事务的三种实现方式
1.编程式事务管理
过时了,一般不用,略

2.声明式事务管理
2.1基于 TransactionProxyFactoryBean的声明式事务管理
1创建异常类

public class MyException extends Exception {

public MyException() {
super();
}

public MyException(String message) {
super(message);
}
}

2 在service层中的转账方法中模拟出异常效果

public void transfer(Integer from, Integer to, Double money) throws MyException{

cardInfoDao.decreaseMoney(from,money);

boolean flag=true;

if(flag)
{
throw new MyException("转账出现异常"); //不要选择try catch 选择 throws
}
cardInfoDao.increaseMoney(to,money);
}

3 在配置文件中创建事务对象和其代理对象

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


<!-- 事务代理工厂 -->
<!-- 生成事务代理对象 -->
<bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager"></property>
<property name="target" ref="cardInfoService"></property>
<property name="transactionAttributes">
<props>
<!-- 主要 key 是方法
ISOLATION_DEFAULT 事务的隔离级别
PROPAGATION_REQUIRED 传播行为

-->

<!-- -Exception 表示发生指定异常回滚,+Exception 表示发生指定异常提交 -->
<prop key="transfer">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-MyException</prop>
</props>
</property>

</bean>

注:Spring事务的隔离级别

1、ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别. 另外四个与JDBC的隔离级别相对应
2、ISOLATION_READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。 这种隔离级别会产生脏读,不可重复读和幻像读。
3、ISOLATION_READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据
4、ISOLATION_REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
5、ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 除了防止脏读,不可重复读外,还避免了幻像读。
4 因为有了代理,所以获取accountServiceImpl的时候,拿的是其代理对象

@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/ApplicationContext.xml");

ICardInfoService cardInfoService= classPathXmlApplicationContext.getBean("serviceProxy",ICardInfoService.class);

try {
cardInfoService.transfer(1,2,200.0);
} catch (MyException e) {
e.printStackTrace();
}


}

//预习的内容

2.2基于 @Transactional 注解的声明式事务管理
修改配置文件,添加

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

<tx:annotation-driven transaction-manager="txManager"/>
在service层转账方法中添加注解
//添加事务注解
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollbackFor=MyException.class)
public void transfer(Integer from, Integer to, Double money) throws MyException{

cardInfoDao.decreaseMoney(from,money);

boolean flag=true;

if(flag)
{
throw new MyException("转账出现异常a"); //不要选择try catch 选择 throws
}
cardInfoDao.increaseMoney(to,money);
}

3.创建配置文件

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

<context:annotation-config></context:annotation-config>

<context:component-scan base-package="com.test" />

<!-- 导入db.properties -->
<context:property-placeholder location="db.properties" />

<!-- 创建数据连接池 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
</bean>

<!-- 创建JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="comboPooledDataSource" />
</bean>

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

<!-- 开启事务的注解 -->
<tx:annotation-driven transaction-manager="txManager"/>

</beans>

4.测试

@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/ApplicationContext.xml");

AccountService accountService= classPathXmlApplicationContext.getBean("accountServiceImpl",AccountService.class);

try {
accountService.transfer(1,2,250.0);
} catch (MyException e) {
e.printStackTrace();
}


}

标签:事务,http,Spring,MyException,www,springframework,三种,org
From: https://blog.51cto.com/u_15707781/5745333

相关文章

  • SpringBoot-JavaMailSender接口实战
    相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送,在SpringBoot的Starter模块中也为此提供了自动化配置。下面通过实例来讲解如......
  • spring day02 xml开发总结以及注解开发总结
    第三方资源配置管理管理DataSource连接池对象【第一步】添加Druid连接池依赖<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><......
  • Spring Boot应用启动的方式:3种
    Java8SpringBoot2.7.3-- 本文简单展示3种SpringBoot应用启动的方式:SpringApplication类和SpringApplicationBuilder类中的方法。 建立SpringBoot(版本2.......
  • java spring 纯注解开发
     创建核心容器有两个方法如下图     获取Bean对象方法有三种     BeanFactory与FactoryBean区别    spring纯注解由哪些常见的 ......
  • SpringBoot线程池工具类
    1.@Component注解将ThreadPoolUtil注入spring容器2.容器启动后会首先执行@PostConstruct注解的initProcessorThreadPool方法,该方法初始化线程池配置3.CountDownLatchco......
  • Tkinter布局管理器(三种方法详解)
    当我们在开发一个GUI程序的时候,布局管理发挥着非常重要的作用,它指的是通过管理控件在窗口中的位置(排版),从而实现对窗口和控件布局的目的。一个优秀的图形用户界面,更像是艺......
  • Spring Tool Suite 4(STS)的下载安装
    SpringToolSuite(sts)简介SpringToolSuite(sts)就是一个基于Eclipse的开发环境,用于开发Spring应用程序。它提供了一个现成的使用环境来实现,调试,运行,和部署你的Spr......
  • SpringBoot启动报错:Parameter 0 of method hmset in com.qcby.rbac.util.RedisUtils r
    SpringBoot启动报错,报错信息如下:  报错是由于A类中定义了含参数的构造函数,Spring自动构造和注入时未为该Bean传入参数,引起报错。查了很多资料,最后发现,我是因为注释......
  • SpringBoot(一) - SpringBoot 初识
    1、创建SpringBoot项目1.1使用SpringInitializr的Web页面创建项目创建网址:https://start.spring.io/1.2使用IDEA创建省略;2、第一个hello2.1代码在xxxApplica......
  • SpringBoot配置文件的优先级
    配置文件优先级(1)命令行参数;(2)java:comp/env的JNDI属性(当前J2EE应用的环境);(3)JAVA系统的环境属性;(4)操作系统的环境变量;(5)JAR包外部的application-xxx.properties或applica......