目录

1.分类

事务控制的方式:

  • 编程式事务控制:编程灵活,但是开发繁琐,每次都要开启、回滚等操作

    • jdbc:Conn.setAutoCommite(false); //设置手动事务控制
    • hibernate:Session.beginTransaction(); //开启事务
  • 声明式事务控制:使用配置文件实现,使用时添加配置,不需要时候移除配置即可。核心是aop,所以控制粒度在方法。也就是对方法来应用事务,不能对方法中的某几行代码实现。对于事务的控制应该放到Server层来处理。

    • jdbc:DataSourceTransactionManager

    • hibernate:


2.Spring对jadc事务管理

2.1xml方式

2.1.1首先定义Dao对象和Server对象

package per.liyue.spring.jdbc_trancation_xml;

import org.springframework.jdbc.core.JdbcTemplate;

/**
* Created by liyue on 2016/11/30.
*/
public class RoleDao
{
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
} public void save(String sql)
{
jdbcTemplate.update(sql);
}
}
package per.liyue.spring.jdbc_trancation_xml;

/**
* Created by liyue on 2016/11/30.
*/
public class RoleService
{
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操作
roleDao.save(sql);
}
}

2.1.2配置文件实现事务管理

配置中要注意:

  • 配置文件中对于事务的命名空间(xmlns:tx="http://www.springframework.org/schema/tx")要引入
  • 对于jdbc来说核心是通过org.springframework.jdbc.datasource.DataSourceTransactionManager来实现事务控制
  • 对于DataSourceTransactionManager的实现还需要配置tx:advice来指明对于哪些方法有哪些权限的操作
  • 对于拦截方法可以用*来模糊匹配
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.jdbc的基本配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="roleDao" class="per.liyue.spring.jdbc_trancation_xml.RoleDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="roleService" class="per.liyue.spring.jdbc_trancation_xml.RoleService">
<property name="roleDao" ref="roleDao"></property>
</bean> <!--2.事务配置--> <!--2.1事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--2.2事务控制 transaction-manager:jdbc的事务管理器 method:配置需要管理的方法和属性,这里可以用*来模糊匹配 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
</tx:attributes>
</tx:advice> <!--2.3事务AOP aop:advisor:指定事务控制和切入点 -->
<aop:config>
<aop:pointcut id="pt"
expression="execution(* per.liyue.spring.jdbc_trancation_xml.RoleService.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>

2.2注解方式

注解方式实现相对简单很多!

需要改动的点:

  • 开启注解
  • 指定注解的事务管理类
  • 对各个类增加注解(@Repository,@Service,@Resource)
  • 对于需要事务管理的位置增加注解@Transactional:
    • 对方法增加:对方法实现事务管理

    • 对类增加:对类实现事务管理

2.2.1对象类

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; /**
* Created by liyue on 2016/11/30.
*/
@Repositorypublic
class RoleDao
{
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
} public void save(String sql)
{
jdbcTemplate.update(sql);
}
}
package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /**
* Created by liyue on 2016/11/30.
*/
@Servicepublic
class RoleService
{
@Resource
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} @Transactional
public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操
roleDao.save(sql);
}
}

2.2.2配置文件

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.jdbc的基本配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="roleDao" class="per.liyue.spring.jdbc_trancation_annotation.RoleDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="roleService" class="per.liyue.spring.jdbc_trancation_annotation.RoleService">
<property name="roleDao" ref="roleDao"></property>
</bean> <!--2.事务配置--> <!--2.1事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--2.2开启注解扫描-->
<context:component-scan
base-package="per.liyue.spring.jdbc_trancation_annotation"></context:component-scan> <!--2.3开启注解事务模式-->
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>

2.3事务的属性

事务对于Dao的操作,这里举例如:

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /**
* Created by liyue on 2016/11/30.
*/
@Servicepublic
class RoleService
{
@Resource
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} @Transactional(readOnly = false, //只读
timeout = -1, //超时,但是最终是由底层数据库来决定
noRollbackFor = ArithmeticException.class, //对于指定异常不执行回滚操作,这里的示例对于数学异常不会滚 )
public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操作
roleDao.save(sql);
}
}

propagation:

isolation:


3.

37.Spring-事务控制.md的更多相关文章

  1. SSM框架之Spring(5)JdbcTemplate及spring事务控制

    Spring(5)JdbcTemplate及spring事务控制 ##1.JdbcTmeplate 它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.spring ...

  2. 【Spring】Spring 事务控制

    Spring 事务控制 Spring 事务控制介绍 JavaEE 体系进行分层开发,事务控制位于业务层,Spring 提供了分层设计业务层的事务处理解决方案. Spring 的事务控制都是基于 AOP ...

  3. spring 事务控制 设置手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

    //假设这是一个service类的片段 try{ //出现异常 } catch (Exception e) { e.printStackTrace(); //设置手动回滚 TransactionAsp ...

  4. Spring事务控制和回滚

    1在一个项目中ssh结构,spring2.5,事务控制采用的是tx拦截器的方式. 自己写了个 int a=1/0;异常抛出了,但是事务还是提交了,怎么搞都不行. 现将看到的一些事务控制总结下来: 事务 ...

  5. Spring事务控制和传递性理解

    1.在同一类方法间相互调用,如果调用方无事务控制,被调用方有事务控制,则被调用方也无事务 原因:外部经过spring容器调用service的方法事务才生效,service类内部方法间相互调用事务不生效 ...

  6. Spring事务控制

    我们在实际业务场景中,经常会遇到数据频繁修改读取的问题.在同一时刻,不同的业务逻辑对同一个表数据进行修改,这种冲突很可能造成数据不可挽回的错乱,所以我们需要用事务来对数据进行管理. 1. 到底什么是事 ...

  7. 阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备

    创建一个工程,只搭建环境不做配置.等配置的时候把这个项目相关的代码再复制到新项目里面 jar包的打包方式 导入包 事务控制也是基于AOP的.所以这里导入aspectjweaver 复制jdbcTemp ...

  8. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  9. Spring的事务控制-基于注解的方式

    模拟转账操作,即Jone减少500,tom增加500 如果有疑问请访问spring事务控制-基于xml方式 1.创建数据表 2.创建Account实体类 public class Account { ...

随机推荐

  1. 在Struts.xml中的result元素指的是:指定动作类的动作方法执行完后的结果视图.

    result结果集 上一篇文章主要讲Struts2框架(4)---Action类访问servlet这篇主要讲result结果集 在Struts.xml中的result元素指的是:指定动作类的动作方法执 ...

  2. 各机器学习方法代码(OpenCV2)

    #include <iostream> #include <math.h> #include <string> #include "cv.h" ...

  3. 整理面试问题iOS

    1.如何添加手势操作. 我们以在view上来举例 //创建一个view UIView *tapView=[UIView new]; tapView.frame=CGRectMake(, , kWidt ...

  4. DataTable与DataSet之间的转换Class

    using System;using System.Collections.Generic;using System.Data;using System.Linq; namespace Convert ...

  5. 移动Web端资源整合

    meta篇 viewreport 视窗宽度 <meta name="viewport" content="width=device-width,initial-sc ...

  6. 一个nginx服务器配置多站点

    有时候你想在一台服务器上为不同的域名运行不同的站点.比如www.siteA.com作为博客,www.siteB.com作为论坛.你可以把两个域名的IP都解析到你的服务器上,但是没法在Nginx的根目录 ...

  7. PythonStudy——函数的参数 Function argument

    形参与实参 # 参数介绍:# 函数为什么要有参数:因为内部的函数体需要外部的数据# 怎么定义函数的参数:在定义函数阶段,函数名后面()中来定义函数的参数# 怎么使用函数的参数:在函数体中用定义的参数名 ...

  8. python学习路程1

    常用的转义字符还有: \n 表示换行 \t 表示一个制表符 \\ 表示 \ 字符本身 任务 请将下面两行内容用Python的字符串表示并打印出来: Python was started in 1989 ...

  9. python从含有汉字和数字的字符串中提取数字部分

    我遇到的问题是:我想要从字符串“1小时12分钟”中(其中两个数字是变化的)截取出1和12. 切片的方法比较简单,但不适合变化的字符串. filter(str.isdigit,"1小时12分钟 ...

  10. cordova插件列表

    主要来源为http://blog.csdn.net/github_39500961/article/details/76270299 1.获取当前应用的版本号 cordova plugin add c ...