Spring提供了对事务的声明式事务管理,只需要在配置文件中做一些配置,即可把操作纳入到事务管理当中,解除了和代码的耦合。

  Spring声明式事务管理,核心实现就是基于Aop。

  Spring声明式事务管理是粗粒度的事务控制,只能给整个方法应用事务,不可以对方法的某几行应用事务。

Spring声明式事务管理器类:

Jdbc技术:DataSourceTransactionManager

Hibernate技术:HibernateTransactionManager

1.xml方式声明事务

  引入tx名称空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///student"/>
<property name="user" value="root"/>
<property name="password" value="juaner767"/>
<property name="initialPoolSize" value="3"/>
<property name="maxPoolSize" value="6"/>
<property name="maxStatements" value="100"/>
<property name="acquireIncrement" value="2"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="studentDao" class="com.juaner.spring.tx.StudentDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="studentService" class="com.juaner.spring.tx.StudentService">
<property name="studentDao" ref="studentDao"/>
</bean>
<!--spring声明式事务管理控制-->
<!--配置事务管理器类-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务增强(如何管理事务,只读、读写...)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*save*" read-only="false"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--aop配置,拦截哪些方法(切入点表达式,拦截上面的事务增强)-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.juaner.spring.tx.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>

2.注解方式声明事务

开启注解扫描

    <!--事务管理器类-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解扫描-->
<context:component-scan base-package="com.juaner.spring.tx"/> <!--注解方式实现事务-->
<tx:annotation-driven transaction-manager="txManager"/>

使用注解

    @Transactional
public void save(Student student){
this.studentDao.save(student);
int i = 1/0;
this.studentDao.save(student);
}

如果@Transactional

应用到方法上,该方法使用事务;

应用到类上,类的方法使用事务,

定义到父类上,执行父类的方法时使用事务;

3.事务属性

    @Transactional(
readOnly = false, //读写事务
timeout = -1 , //事务的超时时间,-1为无限制
noRollbackFor = ArithmeticException.class, //遇到指定的异常不回滚
isolation = Isolation.DEFAULT, //事务的隔离级别,此处使用后端数据库的默认隔离级别
propagation = Propagation.REQUIRED //事务的传播行为
)

其中,事务的传播行为propagation

Propagation.REQUIRED,业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到该事务,否则为自己创建一个新的事务。

Propagation.REQUIRES_NEW,不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法已经运行在一个事务中,则原有事务会被挂起,新的事务会被创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

Spring中的Jdbc事务管理的更多相关文章

  1. Spring 中的 JDBC 事务

    Spring 对 JDBC 的支持 JdbcTemplate 简介 •为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. • ...

  2. Spring中配置Hibernate事务管理

    <!-- transationManager --> <bean id="transactionManager" class="org.springfr ...

  3. CSDN上看到的一篇有关Spring JDBC事务管理的文章(内容比较全) (转)

    JDBC事务管理 Spring提供编程式的事务管理(Programmatic transaction manage- ment)与声明式的事务管理(Declarative transaction ma ...

  4. spring JDBC 事务管理

    spring JDBC 事务管理 一.Spring 中的JDBC Spring中封装了JDBC的ORM框架,可以用它来操作数据,不需要再使用外部的OEM框架(MyBatis),一些小的项目用它. 步骤 ...

  5. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  6. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  7. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  8. 【转】在Spring中基于JDBC进行数据访问时怎么控制超时

    http://www.myexception.cn/database/1651797.html 在Spring中基于JDBC进行数据访问时如何控制超时 超时分类 超时根据作用域可做如下层级划分: Tr ...

  9. spring框架总结(04)----介绍的是Spring中的JDBC模板

    1.1  Jdbc模板概述 它是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装.spring框架为我们提供了很多的操作模板类,入下图所示: 我们今天的主角在spring-jd ...

随机推荐

  1. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  2. Jq_选择器、效果函数

    JQuery 选择器 选择器                     实例                                   选取 *                         ...

  3. iOS开发 在scrollView上增加滑动手势(Pan)

    view上有一个scrollView,现在想在view上加一个Pan手势,需求是:当向下划的时候,整个view动,但是scrollView不动:其它情况下scrollView动而view不动. -(B ...

  4. php imagecreatetruecolor输出字符符或验证码

    $img = imagecreatetruecolor(100,100); //创建真彩图像资源 $color = imagecolorAllocate($img,200,200,200); //分配 ...

  5. vc++编译libtiff4.0.4

    目录 第1章简介    1 第2章命令行编译    2 2.1 编译    2 2.1.1 使用VC++2010编译    2 2.1.2 使用VC++6编译    4 2.2 生成的文件    5 ...

  6. CSS小结

    一.1. css必须写在<head></head>里面的<style></style>里面 2. css 由选择器 + 规则组成, 规则由属性和值组成 ...

  7. HTML5自学笔记[ 7 ]defer和async

    defer:给位于最前面的script标签设置defer="defer",外联js就会在onload触发之前才加载. async:给script标签设置async="as ...

  8. WindowsService(Windows服务)开发步骤附Demo 【转】

    转http://www.cnblogs.com/moretry/p/4149489.html 1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service ...

  9. web基础之hibernate(一篇)

    hibernate的一些基本的认识 1.       hibenate是一个框架(framework) 2.       hibernate是一个orm框架 3.       orm(object r ...

  10. hdu----(1075)What Are You Talking About(trie之查找)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...