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 { ...
随机推荐
- CentOS7 cannot find a valid baseurl for repo base
找到文件夹: cd /etc/sysconfig/network-scripts/ 然后找ifcfg 开头的文件,挨个打开 里面有下面那些代码前三行的就是,添加后面DNS两行,保持并退出,然后继续执行 ...
- [LeetCode&Python] Problem 925. Long Pressed Name
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might ...
- <a>标签里的函数事件写法的实战建议
如果在实际应用中确实是要用到标签来响应onclick事件的, 那么就建议使用下面三种方法 <a href="javascript:void(0);" onclick=&quo ...
- SUID、SGID、粘滞位
粘滞位(Stikybit) +t,只有用户自己可以删除自己创建文件,其他用户只能查看,不能删除. 1:创建两个用户 useradd oo ...
- Docker安装(一)
环境:CentOS release 6.9 (Final) 1.检查环境是否支持安装docker 1)系统内核是否是3.8或更高版本 uname -a (这个安装不了,内核版本不够) Linux ...
- 4、Linux常用命令
1.查看端口占用(8080) lsof -i:8080 2.杀死进程PID kill -9 41726 3.查看防火墙状态 firewall-cmd --state 4.停止防火墙 systemctl ...
- MySQL数据实时增量同步到Kafka - Flume
转载自:https://www.cnblogs.com/yucy/p/7845105.html MySQL数据实时增量同步到Kafka - Flume 写在前面的话 需求,将MySQL里的数据实时 ...
- EF LIKE 查询
<!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="Model" Ali ...
- bananapi+OLED做的一个打地鼠游戏,c语言编程
说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...
- C风格字符串和C++string对象的相互转化
一.C风格的字符串转化为C++的string对象 C++中,string 类能够自动将C 风格的字符串转换成string 对象 #include <iostream> #include ...