37.Spring-事务控制.md
目录
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的更多相关文章
- SSM框架之Spring(5)JdbcTemplate及spring事务控制
Spring(5)JdbcTemplate及spring事务控制 ##1.JdbcTmeplate 它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.spring ...
- 【Spring】Spring 事务控制
Spring 事务控制 Spring 事务控制介绍 JavaEE 体系进行分层开发,事务控制位于业务层,Spring 提供了分层设计业务层的事务处理解决方案. Spring 的事务控制都是基于 AOP ...
- spring 事务控制 设置手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//假设这是一个service类的片段 try{ //出现异常 } catch (Exception e) { e.printStackTrace(); //设置手动回滚 TransactionAsp ...
- Spring事务控制和回滚
1在一个项目中ssh结构,spring2.5,事务控制采用的是tx拦截器的方式. 自己写了个 int a=1/0;异常抛出了,但是事务还是提交了,怎么搞都不行. 现将看到的一些事务控制总结下来: 事务 ...
- Spring事务控制和传递性理解
1.在同一类方法间相互调用,如果调用方无事务控制,被调用方有事务控制,则被调用方也无事务 原因:外部经过spring容器调用service的方法事务才生效,service类内部方法间相互调用事务不生效 ...
- Spring事务控制
我们在实际业务场景中,经常会遇到数据频繁修改读取的问题.在同一时刻,不同的业务逻辑对同一个表数据进行修改,这种冲突很可能造成数据不可挽回的错乱,所以我们需要用事务来对数据进行管理. 1. 到底什么是事 ...
- 阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备
创建一个工程,只搭建环境不做配置.等配置的时候把这个项目相关的代码再复制到新项目里面 jar包的打包方式 导入包 事务控制也是基于AOP的.所以这里导入aspectjweaver 复制jdbcTemp ...
- 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 ...
- Spring的事务控制-基于注解的方式
模拟转账操作,即Jone减少500,tom增加500 如果有疑问请访问spring事务控制-基于xml方式 1.创建数据表 2.创建Account实体类 public class Account { ...
随机推荐
- L2-018. 多项式A除以B*
L2-018. 多项式A除以B 参考博客 #include <iostream> #include <map> #include <cmath> #include ...
- 2018年4月中旬的PTA(三)
C高级第三次PTA作业(1) 题目6-1 输出月份英文名 1.设计思路 (1)算法(子函数) 第一步:定义字符型一级指针子函数名getmonth,形参整型n. 第二步:定义长度为12的字符数组指针mo ...
- 编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”
如题: scripts/extract-cert.c::: fatal error: openssl/bio.h: No such file or directory compilation term ...
- websocket 心跳重连
websocket 的基本使用: var ws = new WebSocket(url); ws.onclose = function () { //something reconnect(); // ...
- 第十二章 NIO
12.NIO 12.1 Java NIO 概述 1课时 12.2 Java NIO.2 之Path.Paths 与 Files 的使用 1课时 12.3 自动资源管理 1课时 12.4 缓冲区(Buf ...
- Git 2.x 中git push时遇到 push.default 警告的解决方法
近在学习使用 git&GitHub,然后今天遇到了一个问题.在执行 git add 和 git commit 操作之后,再进行 git push 操作,出现了如下提示: $ git push ...
- mongodb集群配置分片集群
测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...
- MySQL Point in Time Recovery the Right Way
In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...
- Metadata in HTML
[本文内容大部分来自MDN中文版:https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Introduction_to_HTML/The_head_m ...
- C#编程经验-function advanced params
function advanced params:outrefparamsnot useful,to use them is a burden,so i dont use themdefects:ou ...