【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這是花費最高代價但是最可靠的事務隔離級別。事務被處理爲順序執行。

     除了防止髒讀,不可重複讀外,還避免了幻像讀。

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