两个操作要么同时成功,要么同时失败;
事务的一致性;
以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作;

用来保证一致性,即service方法里的多个dao操作,要么同时成功,要么同时失败;

下面模拟用户转账,a用户转账给b用户200元;需要事务管理;
项目结构:
 
1.代码:                    
com.cy.entity.Account.java;
package com.cy.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; /**
* 账户实体
* @author CY
*
*/
@Entity
@Table(name="t_account")
public class Account { @Id
@GeneratedValue
private Integer id; @Column(length=50)
private String userName; private float balance; //余额 public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public float getBalance() {
return balance;
} public void setBalance(float balance) {
this.balance = balance;
} }

账户dao接口:com.cy.dao.AccountDao.java:

package com.cy.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import com.cy.entity.Account; /**
* 账户Dao接口
* JpaRepository<T, ID>第二个参数是主键类型
* @author CY
*
*/
public interface AccountDao extends JpaRepository<Account, Integer>{ }

账户service接口:com.cy.service.AccountService.java:

package com.cy.service;

/**
* 账户Service接口
* @author CY
*
*/
public interface AccountService { /**
* 从fromUser转账到toUser,account钱;
* @param fromUser
* @param toUser
* @param account
*/
public void transferAccounts(int fromUser, int toUser, float account);
}

账户接口实现类:com.cy.service.impl.AccountServiceImpl.java;

package com.cy.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.cy.dao.AccountDao;
import com.cy.entity.Account;
import com.cy.service.AccountService; /**
* 账户service实现类
* @author CY
*
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService{ @Resource
private AccountDao accountDao; /**
* 从A用户转账到B用户account元;
* 也就是两个操作:
* A用户减去accout元,B用户加上account元
*/
@Override
@Transactional
public void transferAccounts(int fromUser, int toUser, float account) {
Account a = accountDao.getOne(fromUser);
a.setBalance(a.getBalance() - account);
accountDao.save(a); Account b = accountDao.getOne(toUser);
b.setBalance(b.getBalance() + account);
int i = 1/0; //这里制造个异常
accountDao.save(b);
} }

com.cy.controller.AccountController.java来模拟用户转账:

这里返回json数据格式,成功ok,失败no

package com.cy.controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cy.service.AccountService; /**
* 账户Controller层
* @author CY
*
*/
@RestController
@RequestMapping("/account")
public class AccountController { @Resource
private AccountService accountService; @RequestMapping("/transfer")
public String transferAccount(){
try{
accountService.transferAccounts(1, 2, 200);
return "ok";
}catch(Exception e){
return "no";
}
}
}

2.测试:

启动项目,查看新建的表t_account:

mysql> desc t_account;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| balance | float | NO | | NULL | |
| user_name | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+

弄点数据:

mysql> select * from t_account;
+----+---------+-----------+
| id | balance | user_name |
+----+---------+-----------+
| 1 | 700 | zhangsan |
| 2 | 300 | lisi |
+----+---------+-----------+

浏览器http://localhost/account/transfer:

1)显示no,说明转账失败;

2)数据库数据,zhangsan仍然是700,lisi仍然是300,保证了数据一致性;

查看发出的sql:

Hibernate: select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.user_name as user_nam3_0_0_ from t_account account0_ where account0_.id=?
Hibernate: select account0_.id as id1_0_0_, account0_.balance as balance2_0_0_, account0_.user_name as user_nam3_0_0_ from t_account account0_ where account0_.id=?
没有执行保存,全部弄完了才保存;

说明:

这里使用的是:import javax.transaction.Transactional;(这个是jpa规范)
使用import org.springframework.transaction.annotation.Transactional也行;(这个spring实现了jpa规范)

spring boot学习(6) SpringBoot 之事务管理的更多相关文章

  1. Spring Boot学习——数据库操作及事务管理

    本文讲解使用Spring-Data-Jpa操作数据库. JPA定义了一系列对象持久化的标准. 一.在项目中使用Spring-Data-Jpa 1. 配置文件application.properties ...

  2. spring boot中的声明式事务管理及编程式事务管理

    这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...

  3. spring框架学习笔记7:事务管理及案例

    Spring提供了一套管理项目中的事务的机制 以前写过一篇简单的介绍事务的随笔:http://www.cnblogs.com/xuyiqing/p/8430214.html 还有一篇Hibernate ...

  4. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  5. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  6. spring boot 学习(十)SpringBoot配置发送Email

    SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...

  7. spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)

    第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...

  8. spring boot学习(2) SpringBoot 项目属性配置

    第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...

  9. 【Spring Boot学习之五】切面日志管理

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.log4j 常见方式:log4j.properties + org.apache.log4j.Logger比如:l ...

随机推荐

  1. python 数据科学

    参考资料: https://www.yiibai.com/python_data_science/python_bubble_charts.html

  2. 玩转X-CTR100 l STM32F4 l HC-SR04超声波测距

    我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 超声波测距模块HC-SR04可以测量2cm~40 ...

  3. webbench源码学习-->命令行选项解析函数getopt和getopt_long函数

    对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...

  4. pip删除依赖、配置虚拟环境

    问题:跑openpose代码的时候,出现问题 tensorpack 0.8.6 requires tqdm>4.11.1, which is not installed.tf-pose 0.1. ...

  5. fork调用实验-缓冲区相关

    先看下面一段代码: #include <unistd.h> #include <stdio.h> ; char buf[] = "a write to stdout\ ...

  6. magento如何在首页显示产品

    1.首先现在magento后台创建一个新的分类,记下这个分类的 ID 号码.使用这个新建的分类来管理你的首页产品,这个分类设置为前台不可见.这样就不会显示在你的分类菜单中了,但使用代码调用的时候却会显 ...

  7. 作为一名IT从业者,你在工作和学习中,遇到哪些问题

    版权声明:襄阳雷哥的版权声明 https://blog.csdn.net/FansUnion/article/details/28448975 大家都是IT从业者,遇到的问题多少与类似. 假设能把这些 ...

  8. android 学习过程中登陆失效的个人理解

    今天在学习的过程中,要做登陆失效的功能,所以就找了些资料.好好看了一下.研究了一番,慢慢的做出来了! 比方:你在一个手机端登陆了账号,在另外的一个手机端也登陆了账号,此时.前一个手机端的账号会提示登陆 ...

  9. GaugeControl 之 DigitalGauge

    https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraGaugesWinGaugesDigitalDigitalGau ...

  10. 常见MQ流行度比较

    MQ的流行度跟它的特性和应用场景密切相关,站在当下来看,kafka最火,rabbitmq用的也很多,ActiveMQ作为经典mq选择用它入门的也不少.