Spring学习---(4)XML方式实现Spring声明式事务管理
2018-02-28 18:04
597 查看
写在前面:
Spring的声明式事务管理有两种方式,一种是XML配置,一种是注解方式。这也是由于Spring的事务管理底层是AOP实现,即写一次,在运行时,动态植入业务方法中。(AOP编程参考:http://blog.csdn.net/may_3/article/details/79396729)
例如:在JDBC中,设置手动控制事务 Conn.setAutoCommite(false);
例如:在Hibernate中,开启一个事务 Session.beginTransaction();特点:可以实现细粒度的事务控制(比如,对方法中的某几行添加事务),但每次都要开启事务,提交事务,回滚等,开放繁琐
(2)声明式事务控制:是由Spring提供的对事务的管理,只要在配置文件中配置即可。其核心是基于AOP的编程。
特点:粗粒度的事务控制,只能给整个方法应用事务,不能为特定的某几行应用事务。这也是因为AOP只能拦截到类的某个方法。
Spring提供的声明式事务管理器类:
Jdbc技术:DataSourceTransactionManager
Hibernate:HIbernateTransactionManager
(2)配置事务增强
(3)AOP配置,配置拦截哪些方法(切入点表达式)+应用上面的事务增强配置
下面为大家贴上案例代码:
DeptDao代码:package tx;
import org.springframework.jdbc.core.JdbcTemplate;
public class DeptDao {
//接收容器注入的JdbcTemplate对象
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void save(Dept dept) {
String sql1="insert into dept1 (deptname) values(?)";
jdbcTemplate.update(sql1,dept.getDeptName());
}
}
Dept代码:
在这里只是为了模拟一下数据package tx;
public class Dept {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
DeptService代码:package tx;
public class DeptService {
private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void save(Dept dept) {
deptDao.save(dept);
//异常位置,观察是否会混滚,且是否会继续提交
int i=1/0;
deptDao.save(dept);
}
}
测试类APP代码:package tx;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class APP {
private ApplicationContext ac = new ClassPathXmlApplicationContext("tx/bean.xml");
@Test
public void testApp() {
Dept dept = new Dept();
dept.setDeptName("Java研发");
DeptService deptService = (DeptService) ac.getBean("deptService");
deptService.save(dept);
}
}
bean.xml:(同样是重点,配置了管理器类等)
<?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:p="http://www.springframework.org/schema/p"
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<!-- 数据源c3p0连接池对象 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo0228"></property>
<property name="user" value="root"></property>
<property name="password" value="admin123"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="3"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- Template对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--Dao实例 -->
<bean id="deptDao" class="tx.DeptDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="deptService" class="tx.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- Spring声明式事务管理配置 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*save*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* tx.DeptService.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
程序结果:
在Junit测试后,正常会报 java.lang.ArithmeticException: /by zero异常,而且此时,数据库中不会添加一条新的数据,这就证明了我们的事务处理成功。
Spring的声明式事务管理有两种方式,一种是XML配置,一种是注解方式。这也是由于Spring的事务管理底层是AOP实现,即写一次,在运行时,动态植入业务方法中。(AOP编程参考:http://blog.csdn.net/may_3/article/details/79396729)
事务控制:
(1)编程式事务控制:自己手动的控制事务,可以例如:在JDBC中,设置手动控制事务 Conn.setAutoCommite(false);
例如:在Hibernate中,开启一个事务 Session.beginTransaction();特点:可以实现细粒度的事务控制(比如,对方法中的某几行添加事务),但每次都要开启事务,提交事务,回滚等,开放繁琐
(2)声明式事务控制:是由Spring提供的对事务的管理,只要在配置文件中配置即可。其核心是基于AOP的编程。
特点:粗粒度的事务控制,只能给整个方法应用事务,不能为特定的某几行应用事务。这也是因为AOP只能拦截到类的某个方法。
Spring提供的声明式事务管理器类:
Jdbc技术:DataSourceTransactionManager
Hibernate:HIbernateTransactionManager
Spring声明式事务控制配置步骤:
(1)配置事务管理器类(2)配置事务增强
(3)AOP配置,配置拦截哪些方法(切入点表达式)+应用上面的事务增强配置
Spring声明式事务控制案例:
这个案例模拟了在Service中,当出现了异常的情况下,已经提交的事务是否会回滚。下面为大家贴上案例代码:
DeptDao代码:package tx;
import org.springframework.jdbc.core.JdbcTemplate;
public class DeptDao {
//接收容器注入的JdbcTemplate对象
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void save(Dept dept) {
String sql1="insert into dept1 (deptname) values(?)";
jdbcTemplate.update(sql1,dept.getDeptName());
}
}
Dept代码:
在这里只是为了模拟一下数据package tx;
public class Dept {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
DeptService代码:package tx;
public class DeptService {
private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void save(Dept dept) {
deptDao.save(dept);
//异常位置,观察是否会混滚,且是否会继续提交
int i=1/0;
deptDao.save(dept);
}
}
测试类APP代码:package tx;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class APP {
private ApplicationContext ac = new ClassPathXmlApplicationContext("tx/bean.xml");
@Test
public void testApp() {
Dept dept = new Dept();
dept.setDeptName("Java研发");
DeptService deptService = (DeptService) ac.getBean("deptService");
deptService.save(dept);
}
}
bean.xml:(同样是重点,配置了管理器类等)
<?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:p="http://www.springframework.org/schema/p"
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<!-- 数据源c3p0连接池对象 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo0228"></property>
<property name="user" value="root"></property>
<property name="password" value="admin123"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="3"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- Template对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--Dao实例 -->
<bean id="deptDao" class="tx.DeptDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="deptService" class="tx.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- Spring声明式事务管理配置 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*save*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* tx.DeptService.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
程序结果:
在Junit测试后,正常会报 java.lang.ArithmeticException: /by zero异常,而且此时,数据库中不会添加一条新的数据,这就证明了我们的事务处理成功。
相关文章推荐
- Spring学习---(5)注解方式实现Spring声明式事务管理
- xml方式实现spring的声明式事务管理及对jdbc操作的支持
- spring中声明式事务管理控制介绍以及使用XML方式实现
- Xml配置实现Spring_Hibernate中的声明式事务管理
- spring事务系列(3)--声明式事务管理(xml方式)
- Spring声明式事务管理(基于注解方式实现)
- Spring之AOP声明式事务管理(采用xml的配置方式更好)
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- Spring声明式事务管理(基于注解方式实现)
- spring学习(十一)—事务管理(注解方式实现)
- spring中注解方式实现声明式事务管理
- spring框架学习(七)spring管理事务方式之xml配置
- spring学习(十)—事务管理(配置文件方式实现)
- Spring 声明式事务管理----基于AspectJ的XML方式
- 学习记录 - 声明式事务管理方式:基于AspectJ的XML方式和基于注解的方式
- spring 声明式事务管理注解方式实现
- XML方式实现声明式事务管理
- Spring事务分析(2)--基于声明式的事务管理实现分析
- spring的事务管理有几种方式实现,如何实现
- Spring学习之Spring的声明式事务管理详解