
创建 service,搭建 dao,完成对象创建和注入关系
service 注入 dao,在 dao 注入 JdbcTemplate,在 JdbcTemplate 注入 DataSource@Servicepublic class UserService { //注入 dao @autowired private UserDao userDao; }@Repositorypublic class UserDaoImpl implements UserDao { @autowired private JdbcTemplate jdbcTemplate; }在 dao 创建两个方法:多钱和少钱的方法,在 service 创建方法(转账的方法)
@Repositorypublic class UserDaoImpl implements UserDao { @autowired private JdbcTemplate jdbcTemplate; //lucy 转账 100 给 mary //少钱 @OverrIDe public voID reduceMoney() { String sql = "update t_account set money=money-? where username=?"; jdbcTemplate.update(sql,100,"lucy"); } //多钱 @OverrIDe public voID addMoney() { String sql = "update t_account set money=money+? where username=?"; jdbcTemplate.update(sql,"mary"); } }@Servicepublic class UserService { //注入 dao @autowired private UserDao userDao; //转账的方法 public voID accountMoney() { //lucy 少 100 userDao.reduceMoney(); //mary 多 100 userDao.addMoney(); } }上面代码,如果正常执行没有问题的,但是如果代码执行过程中出现异常,有问题
//转账的方法public voID accountMoney() { //lucy少100 userDao.reduceMoney(); //模拟异常 int i = 10/0; //mary多100 userDao.addMoney();}上面问题如何解决呢?使用事务进行解决
事务 *** 作过程
public voID accountMoney() { try { //第一步 开启事务 //第二步 进行业务 *** 作 //lucy少100 userDao.reduceMoney(); //模拟异常 int i = 10/0; //mary多100 userDao.addMoney(); //第三步 没有发生异常,提交事务 } catch(Exception e) { //第四步 出现异常,事务回滚 }}事务 *** 作(Spring 事务管理介绍)事务添加到 JavaEE 三层结构里面 Service 层(业务逻辑层)在 Spring 进行事务管理 *** 作(1)有两种方式:编程式事务管理和声明式事务管理(使用)声明式事务管理基于注解方式(使用)基于 xml 配置文件方式在 Spring 进行声明式事务管理,底层使用 AOP 原理Spring 事务管理 API提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类事务 *** 作(注解声明式事务管理)
在 spring 配置文件配置事务管理器
<!--创建事务管理器--><bean ID="transactionManager" > <!--注入数据源--> <property name="dataSource" ref="dataSource"></property></bean>在 spring 配置文件,开启事务注解在 spring 配置文件引入名称空间 tx(即 transaction)
<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: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/context http://www.springframework.org/schema/context/spring-context.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">开启事务注解
<!--开启事务注解--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>在 service 类上面(或者 service 类里面方法上面)添加事务注解@Transactional,这个注解添加到类上面,也可以添加方法上面如果把这个注解添加类上面,这个类里面所有的方法都添加事务如果把这个注解添加方法上面,为这个方法添加事务@Service@Transactionalpublic class UserService {事务 *** 作(声明式事务管理参数配置)在 service 类上面添加注解@Transactional,在这个注解里面可以配置事务相关参数propagation:事务传播行为
多事务方法直接进行调用,这个过程中事务 是如何进行管理的
@Service@Transactional(propagation = Propagation.required)public class UserService {}ioslation:事务隔离级别事务有特性成为隔离性,多事务 *** 作之间不会产生影响。不考虑隔离性产生很多问题有三个读问题:脏读、不可重复读、虚(幻)读脏读:一个未提交事务读取到另一个未提交事务的数据不可重复读:一个未提交事务读取到另一提交事务修改数据
虚读:一个未提交事务读取到另一提交事务添加数据
解决:通过设置事务隔离级别,解决读问题
@Service@Transactional(propagation = Propagation.required,isolation = Isolation.REPEATEABLE_READ)public class UserService {}timeout:超时时间事务需要在一定时间内进行提交,如果不提交进行回滚默认值是 -1 ,设置时间以秒单位进行计算Readonly:是否只读读:查询 *** 作,写:添加修改删除 *** 作Readonly 默认值 false,表示可以查询,可以添加修改删除 *** 作设置 Readonly 值是 true,设置成 true 之后,只能查询rollbackFor:回滚设置出现哪些异常进行事务回滚norollbackFor:不回滚设置出现哪些异常不进行事务回滚事务 *** 作(XML 声明式事务管理)在 spring 配置文件中进行配置第一步 配置事务管理器第二步 配置通知第三步 配置切入点和切面<!--1 创建事务管理器--> <bean ID="transactionManager" > <!--注入数据源--> <property name="dataSource" ref="dataSource"></property></bean><!--2 配置通知--> <tx:advice ID="txadvice"> <!--配置事务参数--> <tx:attributes> <!--指定哪种规则的方法上面添加事务--> <tx:method name="accountMoney" propagation="required"/> <!--<tx:method name="account*"/>--> </tx:attributes></tx:advice><!--3 配置切入点和切面--> <aop:config> <!--配置切入点--> <aop:pointcut ID="pt" Expression="execution(* com.nemo.Spring5.service.UserService.*(..))"/> <!--配置切面--> <aop:advisor advice-ref="txadvice" pointcut-ref="pt"/></aop:config>事务 *** 作(完全注解声明式事务管理)创建配置类,使用配置类替代 xml 配置文件
@Configuration //配置类@ComponentScan(basePackages = "com.nemo") //组件扫描@EnableTransactionManagement //开启事务public class TxConfig { //创建数据库连接池 @Bean public DruIDDataSource getDruIDDataSource() { DruIDDataSource dataSource = new DruIDDataSource(); dataSource.setDriverClassname("com.MysqL.jdbc.Driver"); dataSource.setUrl("jdbc:MysqL:///user_db"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } //创建 JdbcTemplate 对象 @Bean public JdbcTemplate getJdbcTemplate(DataSource dataSource) { //到 ioc 容器中根据类型找到 dataSource JdbcTemplate jdbcTemplate = new JdbcTemplate(); //注入 dataSource jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } //创建事务管理器 @Bean public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager; } } 总结 以上是内存溢出为你收集整理的【Spring】事务 *** 作全部内容,希望文章能够帮你解决【Spring】事务 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)