【spring 7】spring和Hibernate的整合:声明式事务

一、声明式事务简介

Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。
声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过等价的基于标注的方式),便可以将事务规则应用到业务逻辑中。因为事务管理本身就是一个典型的横切逻辑,正是 AOP 的用武之地。Spring 开发团队也意识到了这一点,为声明式事务提供了简单而强大的支持。

二、实习解析

applicationContext-common配置文件(配置spring声明事务):

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>			
		</property>
	</bean>
	
	<!-- 那些类那些方法使用事务 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* com.angel.usermanager.manager.*.*(..))"/>
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
	</aop:config>
	
	<!-- 事务的传播特性 -->	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="del*" propagation="REQUIRED"/>
			<tx:method name="modify*" propagation="REQUIRED"/>
			<tx:method name="*" propagation="REQUIRED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
</beans>
</span>
事务的传播特性:

1,PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启
2,PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3,PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4,PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5,PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6,PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常
7,PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行

事务的隔离级别:

1,ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。另外四个与JDBC的隔离级别相对应:

2,ISOLATION_READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
3,ISOLATION_READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据
4,ISOLATION_REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
5,ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读。

applicationContext-beans配置文件(属性注入):

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<bean id="userManager" class="com.angel.usermanager.manager.UserManagerImpl">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="logManager" ref="logManager" />
		<property name="log" ref="logModel"/>
	</bean>

	<bean id="logManager" class="com.angel.usermanager.manager.LogManagerImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 注入实体 -->
	<bean id="userModel" class="com.angel.usermanager.domain.User"></bean>
	<bean id="logModel" class="com.angel.usermanager.domain.Log"></bean>
</beans>
</span>
UserManagerImpl类:

<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.usermanager.manager;

import java.util.Date;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.angel.usermanager.domain.Log;
import com.angel.usermanager.domain.User;

<span style="color:#ff0000;">public class UserManagerImpl extends HibernateDaoSupport implements UserManager </span>{

	private LogManager logManager;
	private Log log;

	public void addUser(User user) throws Exception {
		<span style="color:#ff0000;">this.getHibernateTemplate().save(user);</span>
		log.setType("操作日志");
		log.setTime(new Date());
		log.setDetail("Angel8");

		logManager.addLog(log);

		throw new Exception();
	}

	public void setLogManager(LogManager logManager) {
		this.logManager = logManager;
	}

	public void setLog(Log log) {
		this.log = log;
	}

}
</span>

三、总结

声明式事务在配置文件中进行统一配置,避免了在每个类的方法中逐一实现事务管理。但是,声明式事务通过在调用方法的前面和后面进行事务的开启和关闭,所以,它是一个作用到方法级别的管理,而编程式事务因为比较灵活,所以是代码块级别的事务管理!

原文地址:https://www.cnblogs.com/hhx626/p/6010305.html