两个操作要么同时成功,要么同时失败;
事务的一致性;
以前学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. 第n次考试

    题目: 1.堆方块  [题目描述] 给定N个方块,排成一行,将它们编号1到N. 再给出P个操作: M i j表示将i所在的那一堆移到j所在那一堆的顶上. C i表示一个询问,询问i下面有多少个方块. ...

  2. 使用Condition配合await()和signal()实现等待/通知

    关键字Synchronized与wait()和notify()/notifyAll()结合可以实现“等待/通知”模式, Lock类的子类ReentrantLock也可以实现同样的功能,但需要借助Con ...

  3. Oracle导出空表解决办法

    在oracle 11g 中,发现传统的exp不能导出空的表 oracle 11g 新增了一个参数:deferred_segment_creation,含义是段延迟创建,默认是true.具体是什么意思呢 ...

  4. dom4j+XPath

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException

    最近在使用poi操作excel时发现一个问题,详细如下: Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethod ...

  6. Java中统计字符串中各个字符出现的次数

    import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo ...

  7. valgrind- 内存泄漏-how to install and use

    1.how to install my host computer is ARM, U need to Attention yours... valgrind下载: http://valgrind.o ...

  8. specified属性

  9. 任务三 简单程序测试及 GitHub Issues 的使用

    我提交的Issue 我被提出的Issue 在使用Issue的过程中我发现提出的Issue不能指派任务人和问题类型,被提出的Issue可以. 碰到最多的问题是测试程序的过程中, 比如用户未按指定格式输入 ...

  10. HDU - 6311:Cover(欧拉回路,最少的一笔画覆盖无向图)

    The Wall has down and the King in the north has to send his soldiers to sentinel. The North can be r ...