Spring框架中的事务管理

事务应用:分为编程式事务(在代码逻辑编辑事务处理代码)和声明式事务(在xml中配置事务)。

Spring中事务分为全局事务在服务器中应用和局部事务在程序中应用。

Spring中事务管理接口:

PlatformTransactionManager:代表事务管理器,是Spring事务管理的核心接口,提供操作事务的方法(开启事务,提交事务,事务回滚)。

TransactionDefinition:描述事务隔离等级,事务传播(方法与方法之间互相调用时如何控制事务),超时时间。

TransactionStatus:描述事务状态。

在Spring中进行xml事务管理的步骤:

1.配置数据源

<!-- 指明数据源使用的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
	<property name="driverClassName" value="${db.driver}"/>
	<property name="url" value="${db.url}"/>
	<property name="username" value="${db.username}"/>
	<property name="password" value="${db.password}"/>
</bean>

db.properties属性文件配置如下:

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/firstdb?characterEncoding=utf8
db.username=root
db.password=root

2.配置事务管理器

<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

3.配置事务增强操作

<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 事务属性定义 -->
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="update*"/>
<tx:method name="del*"/>
</tx:attributes>
</tx:advice>
<tx:method/>标签中属性:

name:描述需要被增强的方法名,可以使用*通配符(get*表示所有以get开头的方法)。

propagation:说明传播类型。

isolation:事务隔离等级。

timeout:设置超时时间(秒为单位)。

read-only:只读,默认为false。

rollback-for:设置自动回滚事务的异常类型。

4.配置增强切面

<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="aopid" expression="execution(* service.ServiceClass.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aopid"/>
</aop:config>
<aop:ponitcut/>:配置需要增强的类
<aop:advisor/>:配置事务管理(增强和切面的整合)

如果要使用注解方式配置事务管理,则在xml中如下配置:

<!-- 注解方式的事务管理 -->
<tx:annotation-driven transaction-manager="txManager"/>
ServiceClass.java:被代理类

package service;

import javax.sql.DataSource;

import org.springframework.transaction.annotation.Transactional;


public class ServiceClass{
	
	private DataSource dataSource;//通过注入的形式能够让dataSource拿到数据源

	
	public DataSource getDataSource() {
		return dataSource;
	}


	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public void add(String name, int age) {
		System.out.println(dataSource);
		System.out.println("执行添加操作:"+name+" "+age);
		
	}

	
	public void reRs(int id) {
		
		System.out.println("执行返回结果:"+id);
	}

}
applicationContext.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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    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-4.1.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 
<!-- 指明数据源使用的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
	<property name="driverClassName" value="${db.driver}"/>
	<property name="url" value="${db.url}"/>
	<property name="username" value="${db.username}"/>
	<property name="password" value="${db.password}"/>
</bean>
<!-- 配置事务管理器 -->
<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="get*" read-only="true"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="update*"/>
<tx:method name="del*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="aopid" expression="execution(* service.ServiceClass.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aopid"/>
</aop:config>
<bean id="service" class="service.ServiceClass">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解方式的事务管理 -->
<!--  <tx:annotation-driven transaction-manager="txManager"/> -->
</beans>

Test4.java:测试类

package service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test4 {

	public static void main(String[] args) {
		//通过读取xml文件创建ClassPathXmlApplicationContext对象
		ApplicationContext context = new ClassPathXmlApplicationContext("service/applicationContext.xml");
		//通过context对象获取对应名称的bean动态代理对象
		ServiceClass serviceClass = (ServiceClass) context.getBean("service");
		//通过对象调用函数
		serviceClass.add("zhangsan", 18);
		serviceClass.reRs(24);
	}

}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值