一、Spring 事务

  Spring提供对事务支持的核心包是spring-tx-4.3.6.RELEASE包。

  该包类有三个核心接口,提供对事务的支持:

  1.1PlatformTransactionManager

  该接口是Spring提供的平台管理器,主要用于事务管理。

  该接口提供了如下方法:

  TranscationStatus getTransaction(TransactionDefinition definition);用于获取事务状态信息。

  void commit(TransactionStatus status):提交事务

  void rollback(TransactionStatus status):事务回滚

  此接口的具体实现类:

  org.springframework.jdbc.datasource.DataSourceTransactionManager(用于配置JDBC)。

  1.2TransactionDefinition

  TransactionDefinition是事务定义的对象,包含定义事务规则,获取事务信息等操作。

  String getName();获取事务对象名称。

  int getLsolationLeve();获取事务隔离级别。

  int getPropagationBehavior();获取事务传播行为。

  int getTimeout();获取事务超时时间。

  boolean isReadOnly()判断事务是否只读。

  

  1.3TransactionStatus

  TransactionStatus是事务的状态,包含方法:

  void flush();刷新事务

  boolean isCompleted();判断事务是否完成。

  boolean isNewTranscation();判断是否为新事务。

  boolean isRollbackOnly();判断事务是否回滚。

  void setRollbackOnly:设置事务回滚。

二、声明式事务管理

  声明式事务管理式通过AOP将事务管理作为一个切面,然后将切面(事务管理)织入目标类中。

  先看一个失败案例,未配置事务,进行数据库操作失败情况:

  Account.java

public class Account {
private Integer id;
private String username;
private Double 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 Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
} @Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
} }

AccountDao.java(定义数据库相关操作)

import java.util.List;

public interface AccountDao {
public int addAccount(Account account);
public int updateAccount(Account account);
public int deleteAccount(int id);
public Account findAccountById(int id);
public List<Account> findAllAccounts();
public void transfer(String outUser, String inUser, Double money);//转账
}

AccountDaoImpl.java (AccountDao的实现类)

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; public class AccountDaoImpl implements AccountDao{
private JdbcTemplate jt; public void setJt(JdbcTemplate jt) {
this.jt = jt;
} @Override //添加用户
public int addAccount(Account account) {
String sql = "insert into account(username,balance) value(?,?)";
Object[] obj = new Object[] {
account.getUsername(),
account.getBalance()
};
int num = jt.update(sql,obj);
return num;
} @Override //更新用户
public int updateAccount(Account account) {
String sql = "update account set username=?,balance=?,where id = ?";
Object[] obj = new Object[] {
account.getUsername(),
account.getBalance(),
account.getId()
};
int num = jt.update(sql,obj);
return num;
} @Override //删除用户
public int deleteAccount(int id) {
String sql = "delete from account where id = ?";
int num = jt.update(sql,id);
return num;
} @Override //按ID查找
public Account findAccountById(int id) {
String sql = "select * from account where id = ?";
//设置映射,即表中一行记录对应一个Accout对象(数据库中列名要和对象属性名一致)
RowMapper<Account> rp = new BeanPropertyRowMapper<Account>(Account.class);
return this.jt.queryForObject(sql, rp, id);
} @Override//查找所有对象
public List<Account> findAllAccounts() {
String sql = "select * from account";
RowMapper<Account> rp = new BeanPropertyRowMapper<Account>(Account.class);
return this.jt.query(sql,rp);
} @Override //转账
public void transfer(String outUser, String inUser, Double money) {
//转账
this.jt.update("update account set balance = balance - ?" +
"where username = ?", money, outUser);
int i = 1/0; //模拟意外情况。
//收账
this.jt.update("update account set balance = balance + ?" +
"where username = ?",money,inUser);
}
}

beans.xml

<?xml  version="1.0"  encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" > <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/spring" />
<property name = "username" value = "root" />
<property name = "password" value = "123456" />
</bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref = "dataSource" />
</bean> <bean id = "accountDao" class = "com.spring.db.AccountDaoImpl">
<property name="jt" ref = "jdbcTemplate"></property>
</bean> </beans>

测试:

  

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; public class JDBCTemplateTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
AccountDao accountDao = (AccountDao)ac.getBean("accountDao");
Account hcf = new Account();
Account zrx = new Account();
//设置两个用户
hcf.setUsername("hcf");
hcf.setBalance(1000.0); zrx.setUsername("zrx");
zrx.setBalance(1000.0);
//将用户添加到数据库
accountDao.addAccount(hcf);
accountDao.addAccount(zrx);
//zrx向hcf转账500
accountDao.transfer("zrx", "hcf", 500.0);
System.out.println("成功");
}
}

转账方法中人为添加一个错误,诸侯出现了转账的金额扣除了,但是收账方没有收到金额。

这种情况显然是我们不允许的。

这时就可以通过事务来实现,把转账功能作为一个事务。

即使中途出现错误没有将钱转给收账方,会发生回滚转账方的钱也不会扣除。

  

  2.1基于XML的声明式事务

  首先我们需要如下jar包:

  

  先了解下xml中配置事务的元素:

  <tx:advice>配置事务通知,

  属性transaction-manager:指定事务管理器。

    <tx:attributes>:<tx:advice>的子元素,用于配置事务细节。

      <tx:method>:<tx:attributes>的子元素,用于配置会务相关信息。

      属性:name:指定与事务关联的方法,可采用通配符*。

         propagation:指定事务传播行为。

         isolation;指定事务隔离级别,默认为REFAULT。

         read-only:指定事务是否只读。

  

  我们只需在benas.xml中配置事务相关信息,然后通过切面的配置,将目标方法织入即可。

<?xml  version="1.0"  encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" > <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/spring" />
<property name = "username" value = "root" />
<property name = "password" value = "123456" />
</bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref = "dataSource" />
</bean> <bean id = "accountDao" class = "com.spring.db.AccountDaoImpl">
<property name="jt" ref = "jdbcTemplate"></property>
</bean> <!-- 事务管理 -->
<!-- 配置事务管理,并制定数据源 -->
<bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "dataSource" />
</bean>
<!-- 配置事务切面,将事务作为一个切面织入方法 -->
<tx:advice id = "txAdvice" transaction-manager = "transactionManager">
<tx:attributes>
<!-- *代表任意方法 传播行为 隔离级别 是否只读 -->
<tx:method name = "*" propagation = "REQUIRED" isolation = "DEFAULT" read-only ="false"/>
</tx:attributes>
</tx:advice> <!-- 配置切面,制定切面,切入点 -->
<aop:config>
<aop:pointcut expression = "execution(* com.spring.db.*.*(..))" id = "txPointCut"/>
<aop:advisor advice-ref = "txAdvice" pointcut-ref = "txPointCut"/>
</aop:config> </beans>

在xml中添加事务配置,我们再来进行转账操作。

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; public class JDBCTemplateTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
AccountDao accountDao = (AccountDao)ac.getBean("accountDao");
Account hcf = new Account();
Account zrx = new Account(); //zrx向hcf转账500
accountDao.transfer("zrx", "hcf", 500.0);
System.out.println("成功");
}
}

再次运行出现错误后数据库中的balance并没有被扣除。

转账和收账作为一个整体,一荣俱荣,一损俱损。

  2.2基于Annotation的声明式事务

    1)在xml中配置事务注解驱动

    <tx:annotation-driven transaction-manager="transactionManager"/>

    2)在需要使用事务的类(对类中所有方法有效)或方法(只对方法有效)上添加@Transactional注解。

    

   @Transactional注解属性:

   transactionManager:指定事务管理器。

   isolation:指定事务隔离级。

   noRollbackFor:用于指定遇到异常时强制不执行回滚。

   noRollbackForClassName:指定遇到多个异常时强制不执行回滚。

   该属性可指定多个异常类名。

   propagation:指定事务传播行为。

   read-only:设置事务是否只读,默认为false。

   rollbackFor:用于指定遇到特定异常时强制发生回滚。

   rollbackForClassName:指定遇到多个异常时强制执行回滚。

   timeout:指定事务超时时长。

  beans.xml

  

<?xml  version="1.0"  encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" > <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/spring" />
<property name = "username" value = "root" />
<property name = "password" value = "123456" />
</bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref = "dataSource" />
</bean> <bean id = "accountDao" class = "com.spring.db.AccountDaoImpl">
<property name="jt" ref = "jdbcTemplate"></property>
</bean> <!-- 事务管理 --> <!-- 配置事务管理,并制定数据源 -->
<bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "dataSource" />
</bean>
<!-- 配置事务驱动 -->
<tx:annotation-driven transaction-manager = "transactionManager" /> </beans>

将转账方法加上注解:

@Override //转账
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT, readOnly = false)
public void transfer(String outUser, String inUser, Double money) {
//转账
this.jt.update("update account set balance = balance - ?" +
"where username = ?", money, outUser);
int i = 1/0; //模拟意外情况。
//收账
this.jt.update("update account set balance = balance + ?" +
"where username = ?",money,inUser);
}

   

1.1(Spring学习笔记)Spring-事务基础的更多相关文章

  1. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  2. Spring学习笔记一:基础概念

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774310.html    一:Spring是什么 Spring的主要作用是作为对象的容器. 传统编程中,我们 ...

  3. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  4. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  5. spring学习笔记---数据库事务并发与锁详解

    多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...

  6. Spring学习笔记——Spring事务仅仅对执行时异常回滚

    我们在使用Spring时候一般都知道事务在遇到异常的时候会回滚.岂不知Spring的事务默认仅仅有在发生执行时异常即:RunTimeException时才会发生事务,假设一个方法抛出Exception ...

  7. 多事务运行并发问题spring学习笔记——数据库事务并发与锁详解

    多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...

  8. 1.4(Spring学习笔记)Spring-JDBC基础

    一.Spring JDBC相关类 1.1 DriverManagerDataSource DriverManagerDataSource主要包含数据库连接地址,用户名,密码. 属性及含义如下配置所示: ...

  9. Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...

  10. Spring学习笔记--Spring简介

    1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...

随机推荐

  1. 如何解决DuplicateFileException: Duplicate files copied in APK问题

    问题:有重复的文件存在APK里 解决方案:在Module里的build.gradle中设置忽略此重复文件即可.

  2. ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库

    ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...

  3. tomcat编码配置

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" ...

  4. JS遮罩层弹框效果

    对于前端开发者来说,js是不可缺少的语言.现在我开始把我日常积累的一些js效果或者通过搜索自己总结的一些效果分享给大家,希望能够帮助大家一起进步,也希望大家能够多多支持! 1.今天我先分享一个遮罩层弹 ...

  5. Nginx使用教程----提高Nginx网络吞吐量之buffers优化

    请求缓冲区在NGINX请求处理中起着重要作用. 在接收到请求时,NGINX将其写入这些缓冲区. 这些缓冲区中的数据可作为NGINX变量使用,例如$request_body. 如果缓冲区与请求大小相比较 ...

  6. Bootstrap 栅格系统 理解与总结

    Bootstrap 栅格系统 学习总结 Bootstrap框架是如今最流行的前端框架之一,Bootstrap功能强大,简单易学,很符合实际应用场景. 只是Bootstrap的内容较多,新手往往不能很快 ...

  7. bzoj2442&&codevs4654 单调队列优化dp

    这道题也是一道单调队列 很明显满足各种性质 f[i]表示i不选前面k-1个都选的最小损失 维护的是个单增队列 q[head]是队列最小值 代码十分简介 注意longlong就okay #include ...

  8. ajax获取django的csrf_token

    ''' 方法一: data: { 'teamid': teamid, csrfmiddlewaretoken: '{{ csrf_token }}' //data: {name: 'john', cs ...

  9. 美团网技术团队分享的MySQL索引及慢查询优化教程

    MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位 ...

  10. git web 服务器的搭建【转】

    转自:http://blog.csdn.net/transformer_han/article/details/6450200 目录(?)[-] git服务器搭建过程 需求 硬件需求一台Ubuntu或 ...