一、简述事物处理

1、事物处理的基本概念

1)提交:所有操作步骤都被完整执行后,称该事物被提交

2)回滚:某步操作执行失败,所有操作都没被提交,则事物必须被回滚

2、事物处理的特性(ACID)

1)原子性

2)一致性

3)隔离性

4)持久性

二、事物处理的3种方式

1、关系型数据库的事物处理

1)Begin Transaction(启动事务处理)

2)Commit或RollBack(提交或回滚)

3)End Transaction(提交事务处理)

2、传统的JDBC事务处理

package com.gc.action;

import org.junit.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement; /**
* Created by sky on 16-7-18.
*/
public class JdbcHelloWorld {
DataSource dataSource;
//获取数据源
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} @Test
public void test() {
Connection conn = null;
Statement stmt = null; try {//获取数据库连接
dataSource.getConnection();
//开始启动事务
//Sets this connection's auto-commit mode to the given state.
conn.setAutoCommit(false);
//Creates a Statement object for sending SQL statements to the database.
conn.createStatement();
//执行相应的操作
stmt.executeUpdate("INSERT INTO hello VALUES (1, 'gf', 'HelloWorld')");
//执行成功则提交事务
conn.commit();
} catch (SQLException e1) {
if (conn != null) {
try {
//执行不成功则回滚
conn.rollback();
} catch (SQLException e2) {
System.out.println("数据库连接有异常" + e2);
}
}
} finally {
//加入stmt不为空,则关闭stmt
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e3) {
System.out.println("执行操作有异常" + e3);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e4) {
System.out.println("数据连接有异常" + e4);
}
}
} }
}

3、分布式事务处理

原子性、多个事务管理器合作

三、Spring的事务处理

1、Spring事务处理的概述

Spring中事务处理实际上是基于动态AOP机制实现

package org.springframework.transaction;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus; public interface PlatformTransactionManager { 
//目前的事务
TransactionStatus getTransaction(TransactionDefinition var1) throws TransactionException; 
void commit(TransactionStatus var1) throws TransactionException;
void rollback(TransactionStatus var1) throws TransactionException;
}
TransactionDefinition代表着事务处理的一些属性定义
package org.springframework.transaction;

        public interface TransactionDefinition {
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_MANDATORY = 2;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
int ISOLATION_DEFAULT = -1;
int ISOLATION_READ_UNCOMMITTED = 1;
int ISOLATION_READ_COMMITTED = 2;
int ISOLATION_REPEATABLE_READ = 4;
int ISOLATION_SERIALIZABLE = 8;
int TIMEOUT_DEFAULT = -1;
//获得事务的传播行为
int getPropagationBehavior();
//获得事务的隔离层次
int getIsolationLevel();
//获得事务是否超时
int getTimeout();
//判断是否为只读事务
boolean isReadOnly();
//返回一个事务的名字
String getName();
}
TransactionStatus代表了目前的事务
package org.springframework.transaction;

        import java.io.Flushable;
import org.springframework.transaction.SavepointManager; public interface TransactionStatus extends SavepointManager, Flushable {
//判断是否是一个事务
boolean isNewTransaction(); boolean hasSavepoint();
//设定为只读事务
void setRollbackOnly();
//判断是否为只读事务
boolean isRollbackOnly(); void flush();
//判断一个事务是否完成
boolean isCompleted();
}

第6章 Spring的事物处理的更多相关文章

  1. spring的事物实现

    Spring的事物主要有三个接口 PlatformTransactionManager. 根据TransactionDefinition配置的事物信息创建事物 TransactionDefinitio ...

  2. Spring定义事物通知tx:advice

    <aop:config proxy-target-class="false">    <aop:advisor advice-ref="txAdvice ...

  3. Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述

    第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...

  4. 2017.2.28 activiti实战--第七章--Spring容器集成应用实例(五)普通表单

    学习资料:<Activiti实战> 第七章  Spring容器集成应用实例(五)普通表单 第六章中介绍了动态表单.外置表单.这里讲解第三种表单:普通表单. 普通表单的特点: 把表单内容写在 ...

  5. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

  6. Spring学习指南-第二章-Spring框架基础(完)

    第二章 Spring框架基础 面向接口编程的设计方法 ​ 在上一章中,我们看到了一个依赖于其他类的POJO类包含了对其依赖项的具体类的引用.例如,FixedDepositController 类包含 ...

  7. Spring入门篇——第7章 Spring对AspectJ的支持

    第7章 Spring对AspectJ的支持 介绍Spring对AspectJ的支持 7-1 AspectJ介绍及Pointcut注解应用 实例 完成了在xml文件的配置 7-2 Advice定义及实例 ...

  8. Spring入门篇——第6章 Spring AOP的API介绍

    第6章 Spring AOP的API介绍 主要介绍Spring AOP中常用的API. 6-1 Spring AOP API的Pointcut.advice概念及应用 映射方法是sa开头的所有方法 如 ...

  9. Spring入门篇——第5章 Spring AOP基本概念

    第5章 Spring AOP基本概念 本章介绍Spring中AOP的基本概念和应用. 5-1 AOP基本概念及特点 5-2 配置切面aspect ref:引用另外一个Bean 5-3 配置切入点Poi ...

随机推荐

  1. NOIp2016 游记

    DAY -2 不要问我为什么现在就开了一篇博客. 本来想起个NOIp2016爆零记或者NOIp2016退役记之类的,但是感觉现在不能乱立flag了.所以就叫游记算了. 前几场模拟赛崩了一场又一场,RP ...

  2. Linux下Nginx负载 iis问题

    使用以下NGINX配置负载IIS upstream 192.168.119.128{ server 192.168.119.1:8081; server 192.168.119.1:8082; } s ...

  3. ubuntu 下应用 Python 和 SL4A 的 Android 应用程序搭建您自己的android研发环境

    转载自:http://code.qtuba.com/article-50680.html 最近在看<head first python>,书中有讲python在android中进行开发的章 ...

  4. List集合及子类

    List集合特点:有序(存储和取出的元素一致):可重复 1.添加功能 void add(int index,Object element):在指定位置添加元素 2.获取功能 Object get(in ...

  5. JS,删除数据时候,多次确认后才删除。

    function delfun(){ if(window.confirm("请仔细核对无误,删除本数据后不能恢复.")){ if(window.confirm("请再次确 ...

  6. thinkphp使用ajax

    thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 一.thinkphp使用ajax返回数 ...

  7. phpstorm 使用技巧

    专题1 专题2 专题3 专题4 快捷键

  8. kafka(logstash) + elasticsearch 构建日志分析处理系统

    第一版:logstash + es 第二版:kafka 替换 logstash的方案

  9. Ubuntu虚拟机中断后重启网络断接错误解决方案

    因为该死的windows自动更新,所以vmplayer经常会被强制关闭. 但重新启动后,会发生不能连接到网络的情况显示: waiting for the network configuration…… ...

  10. icomoon图标的使用

    这里的图标可以自己定义 网址:https://icomoon.io/app/#/select/ 定义完后,自己下载下来 引用:    <link href="css/style.css ...