【Spring】Spring事务

      Spring支持两种方式实现事务:编程式事务、声明式事务。

【编程式事务】

      编程式事务使用TransactionTemplate或者直接使用底层的PlatformTransactionManager

      以Hibernate为例:

1.添加事务的业务层类

package com.bjpowernode.usermgr.manager;

import java.util.Date;

import org.hibernate.Session;

import com.bjpowernode.usermgr.domain.Log;
import com.bjpowernode.usermgr.domain.User;
import com.bjpowernode.usermgr.util.HibernateUtils;

public class UserManagerImpl implements UserManager {

	@Override
	public void addUser(User user) {
		Session session = null;
		try {
			// 获取session
			session = HibernateUtils.getSessionFactory().getCurrentSession();

			// 开始事务
			session.beginTransaction();

			// 添加用户
			session.save(user);

			// 添加日志־
			Log log = new Log();
			log.setType("操作日志");
			log.setTime(new Date());
			log.setDetail("***");
			LogManager logManager = new LogManagerImpl();
			logManager.addLog(log);
			Integer.parseInt("sfjsklf");

			// 提交事务
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}


}

2.一些配置

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_1?characterEncoding=utf-8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>

		<property name="hibernate.current_session_context_class">thread</property>

		<mapping resource="com/bjpowernode/usermgr/domain/User.hbm.xml" />
		<mapping resource="com/bjpowernode/usermgr/domain/Log.hbm.xml" />
	</session-factory>
</hibernate-configuration>

3.HibernateUtils类:

package com.bjpowernode.usermgr.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

	private static SessionFactory factory;
	
	private HibernateUtils() {
	}
	
	static {
		try {
			Configuration cfg = new Configuration().configure();
			factory = cfg.buildSessionFactory();
		}catch(Exception e) {
			e.printStackTrace();
			throw new java.lang.RuntimeException(e);
		}	
	}
	
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	
	public static Session getSession() {
		return factory.openSession();
	}
	
	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
}
4.User类和Log类略

【声明式事务】

      声明式事务是建立在AOP之上的,本质是对方法前后进行拦截,然后在目标方法开始之前创建一个事务,在执行完方法之后根据执行情况提交事务或者回滚事务。

声明式事务可以用两类方式添加,第一是针对小型项目,就是@注解方式,在方法之前添加事务。第二也是比较普遍用的,在xml配置事务添加的条件,这样比较方便,而且事务是指定同意规则下添加的。

一、@Transactional注解

1.源码如下:

/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.transaction.TransactionDefinition;

/**
 * Describes transaction attributes on a method or class.
 * 
 * <p>
 * This annotation type is generally directly comparable to Spring's
 * {@link org.springframework.transaction.interceptor.RuleBasedTransactionAttribute}
 * class, and in fact {@link AnnotationTransactionAttributeSource} will directly
 * convert the data to the latter class, so that Spring's transaction support
 * code does not have to know about annotations. If no rules are relevant to the
 * exception, it will be treated like
 * {@link org.springframework.transaction.interceptor.DefaultTransactionAttribute}
 * (rolling back on runtime exceptions).
 * 
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.2
 * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute
 * @see org.springframework.transaction.interceptor.RuleBasedTransactionAttribute
 */
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {

	// 可选的事务传播行为设置
	Propagation propagation() default Propagation.REQUIRED;

	// 可选的事务隔离级别设置
	Isolation isolation() default Isolation.DEFAULT;

	// 事务超时时间设置
	int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;

	// 读写或只读事务,默认读写
	boolean readOnly() default false;

	// 导致事务回滚的异常类数组,必须继承自Throwable
	Class<? extends Throwable>[] rollbackFor() default {};

	// 导致事务回滚的异常类名字数组,必须继承自Throwable
	String[] rollbackForClassName() default {};

	// 不会导致事务回滚的异常类数组,必须继承自Throwable
	Class<? extends Throwable>[] noRollbackFor() default {};

	// 不会导致事务回滚的异常类名字数组,必须继承Throwable
	String[] noRollbackForClassName() default {};

}

2.用法:

      @Transactional注解可以作用于接口、接口方法、类、类方法上,但是一般我们都会注解在类或者类方法上,一般是业务层的类和方法,并且@Transactional注解只被应用到public方法上。

3.示例:

//①注解在类上,这是类中所有public方法都具有该类型的事务
@Transactional
public class StudentBeanImpl extends BaseBeanImpl<Student> implements
		StudentBean {

	//①注解在方法上,只有来自外部的方法调用才会被捕获并且引起事务行为,本类中的方法调用会被忽略事务行为
	@Transactional
	@Override
	public boolean saveStudents(Student student, String databaseName,
			String authorityDataBaseName, String companyNumber) {
		boolean userflag = false;
		try {
			// 1. 将数据存入基础库*********************************************
			// 2.将数据存入authority库,大段代码略******************************
			// 3.将数据存入cloud库,大段代码略**********************************
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return userflag;
	}


}

二、xml配置,配置也有好几种方式,下面选用使用tx标签配置的拦截器

      除了一些项目中用到的类的注入以及数据库连接的配置外,其他的一些公共行为都在applicationContext-common.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: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配置com.bjpowernode.usermgr.manager包下所有方法都使用该事务配置 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.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>

【事务传播特性】

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这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。

     除了防止脏读,不可重复读外,还避免了幻像读。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章