热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Spring中事务(基于xml&&基于注解)

Spring事务一、基本概念1、事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务是恢复和并发控制的基

Spring事务

一、基本概念

1、事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务是恢复和并发控制的基本单位。

2、特性ACID:原子性:整体。 一致性:完成。隔离性:并发。持久性:结果。

3、隔离问题:

脏读:一个事务读到另一个事务没有提交的数据

不可重复读:一个事务读到另一个事务已提交的数据(update)

虚读(幻读):一个事务读到另一个事务已提交的数据(insert)

4、隔离级别:

      readuncommitted:读未提交。存在3个问题

      readcommitted:读已提交。解决脏读,存在2个问题

      repeatableread:可重复读。解决:脏读、不可重复读,存在1个问题。

      serializable:串行化。都解决,单事务。

5、Spring中事务的包:spring-tx-3.2.0.RELEASE.jar

6、事务的三个顶级接口:

 PlatformTransactionManager(平台事务管理器):spring要管理事务,必须使用事务管理器,进行事务配置时,必须配置事务管理器。

TransactionDefinition事务详情(事务定义、事务属性):spring用于确定事务具体详情,例如:隔离级别、是否只读、超时时间等。进行事务配置时,必须配置详情。spring将配置项封装到该对象实例。

TransactionStatus事务状态:spring用于记录当前事务运行状态。例如:是否有保存点,事务是否完成。spring底层根据状态进行相应操作。


7、事务管理器只是一个接口,开发的时候要使用它的实现类。

PlatformTransactionManager平台事务管理器 中两个常见的事务管理类

1、DataSourceTransactionManager,jdbc开发时事务管理器,采用JdbcTemplate

2、HibernateTransactionManager,hibernate开发时事务管理器,整合hibernate

【注意】事务管理器,通过“事务详情”,获得“事务状态”,从而管理事务。

事务详情 TransactionDefinition 中定义的一些信息:

1、PROPAGATION_REQUIRED, required , 必须  【默认值】支持当前事务,A如果有事务,B将使用该事务。    如果A没有事务,B将创建一个新的事务。

2、PROPAGATION_REQUIRES_NEW, requires_new ,必须新的。 如果A有事务,将A的事务挂起,B创建一个新的事务。    如果A没有事务,B创建一个新的事务。

3、PROPAGATION_NESTED,nested ,嵌套。A和B底层采用保存点机制,形成嵌套事务。

 

二、基于xml的事务配置

在Spring xml配置AOP自动生成代理,进行事务管理。

1、建表

create table account(

  id int primary key auto_increment,

  username varchar(50),

  money int

);

insert into account(username,money) values('dianer','100');

insert into account(username,money) values('Lily','100');

 

2、导入Jar包

Spring 4+1

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

com.springsource.org.apache.commons.logging-1.1.1.jar

AOP 4个

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

数据两个

spring-jdbc-3.2.0.RELEASE.jar

spring-tx-3.2.0.RELEASE.jar

驱动包

mysql-connector-java-5.1.28-bin.jar
连接池

com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

 

3、DAO接口

public interface AccountDAO {
//汇款是pay,收款是Receipt

public void pay(String outer,Integer money);
public void receipt(String inner,Integer money);

}


4、DAO接口的实现类

public class AccountDAOImpl extends JdbcDaoSupport implements AccountDAO {

/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#pay(java.lang.String, java.lang.Integer)
*/
@Override
public void pay(String outer, Integer money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set mOney= money - ? where username = ?", money,outer);

}

/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#receipt(java.lang.String, java.lang.Integer)
*/
@Override
public void receipt(String inner, Integer money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set mOney= money + ? where username = ?", money,inner);

}

}


5、Service接口

public interface Ser {

//转账服务

public void transfer(String outer,String inner,Integer money);
}


6、Service实现类

public class AccountSerImpl implements Ser {

/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.Ser#transfer(java.lang.String, java.lang.String, java.lang.Integer)
*/
private AccountDAO accountDao;



public AccountDAO getAccountDao() {
return accountDao;
}



public void setAccountDao(AccountDAO accountDao) {
this.accountDao = accountDao;
}



@Override
public void transfer(String outer, String inner, Integer money) {
// TODO Auto-generated method stub
accountDao.pay(outer, money);
//模拟故障
//int i=1/0;
accountDao.receipt(inner, money);
}

}


7、配置xml文件 

头部加上:

xmlns:tx=http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx

 http://www.springframework.org/schema/tx/spring-tx.xsd


这里采用的是读取properties文件的方式


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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">










































8、编写测试类

 

/**
*
*/
package com.Lily.SpringLearning.J_tx_xml;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* *
* @author LilyLee
* @date 2017年6月26日
* @time 下午9:02:32
* @Version 1.0
* @email lilylee_1213@foxmail.com
*
*/
public class txTest {

@Test
public void test() {
String xmlPath = "com/Lily/SpringLearning/J_tx_xml/applicationContext.xml";
ApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext(xmlPath);
Ser accountService = (Ser) applicationContext.getBean("accountService");
accountService.transfer("dianer", "Lily", 6);
}

}

这里出了一个小插曲,第一次运行的时候报错,因为

Ser accountService =  (Ser)applicationContext.getBean("accountService");

前面应该是Service的接口,而不是实现类,这个要注意。

 

三、基于注解的事务

注解加在两个地方

1、Service的实现类@Transactional(propagation=Propagation.REQUIRED , isolation =Isolation.DEFAULT)

2、修改xml,  ,将管理器交给Spring,底层使用CGLIB代理。

 

/**
*
*/
package com.Lily.SpringLearning.J_tx_anno;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* *
* @author LilyLee
* @date 2017年6月26日
* @time 下午8:37:28
* @Version 1.0
* @email lilylee_1213@foxmail.com
*
*/

@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)

public class AccountSerImpl implements Ser {

/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.Ser#transfer(java.lang.String, java.lang.String, java.lang.Integer)
*/
private AccountDAO accountDao;



public AccountDAO getAccountDao() {
return accountDao;
}



public void setAccountDao(AccountDAO accountDao) {
this.accountDao = accountDao;
}



@Override
public void transfer(String outer, String inner, Integer money) {
// TODO Auto-generated method stub
accountDao.pay(outer, money);
//模拟故障
//int i=1/0;
accountDao.receipt(inner, money);
}

}

 


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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
































 


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 基于事件驱动的并发编程及其消息通信机制的同步与异步、阻塞与非阻塞、IO模型的分类
    本文介绍了基于事件驱动的并发编程中的消息通信机制,包括同步和异步的概念及其区别,阻塞和非阻塞的状态,以及IO模型的分类。同步阻塞IO、同步非阻塞IO、异步阻塞IO和异步非阻塞IO等不同的IO模型被详细解释。这些概念和模型对于理解并发编程中的消息通信和IO操作具有重要意义。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
author-avatar
韭花帖_420
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有