Springにおける事務属性の紹介及び宣言式事務管理


Springにおける事務管理は主にプログラミング事務管理と声明式事務管理があります.ここでは主に声明式事務管理を紹介します.Springでは、主にAOPを通じて声明的な事務管理を行う.Springの事務に対するサポートを知るには、まず事務の属性を知る必要があります.Springにおける事務の属性は主に以下のような点に分けられています.
    1、伝播行為(Propgation behavior)
       対応する定数と説明が見つかります.以下のいくつかのリストがあります.
      
PROPAAGATION_MANDATORY:方法は現存する事務の中で行わなければなりません.でないと、異常が発生します.
      
PROPAAGATION_NESTEP:埋め込みビジネスで行う
      
PROPAAGATION_NEVER:事務中に行うべきではなく、あれば異常をなくす.
      
PROPAAGATION_NOT_SUPPORTED:事務中に行うべきではなく、既存の事務を一時停止する.
      
PROPAAGATION_REQUIRED:今の事務をサポートします.ないなら新しい事務を作ります.
      
PROPAAGATION_REQUIRES_NEW:新たなビジネスを立ち上げるには、現存している一つの仕事を一時停止します.
      
PROPAAGATION_SUPPORTS:現在の事務をサポートしておりますが、ない場合は非事務で行います.
   
    2、隔離レベル(Isolation level)
       一つのアプリケーションでは、複数のトランザクションが同時に行われる可能性があります.これらのトランザクションは互いに他のトランザクションの存在を知らないはずです.例えば、アプリケーション全体が一つのトランザクションしか存在しません.
      
Dirty read(汚い読み):ある事務はすでにデータを更新しました.もう一つの事務はこの時点で同じデータを読みました.いくつかの原因で前の事務が転覆した場合、後の事務で読み込むデータは間違っています.
      
Non-repeable read(重複して読まない):一つの事務の二回の照会では、トランザクションが一致しないのは、二つの照会過程の間に一つの事務更新の元のデータが挿入されているからかもしれません.
      
Photom read:一つの事務の二回の調査ではデータのペン数が一致しませんでした.
上記の問題を解決する方法の一つは、ある事務が進行中に更新または照会中のデータをロックすることです.しかし、効率的な問題が発生し、他の事務は現在の事務のロックが解除されるまで待つ必要があります.しかし、需要によっては、トランザクション時に完全にデータをロックする必要はなく、隔離レベルは、実際のニーズに応じて、データのロックを設定することができます.以下はいくつかの隔離層のパラメータ説明です.
      
ISO LATION_DEFAULT:ベースのデータベースを使用して設定された分離レベル
      
ISO LATION_READ_COMMITTED:他の事務が提出したデータフィールドを実行して読み込むと、汚れを防ぐことができます.
      
ISO LATION_READ_UNCOMMITTED:他のパラレルビジネスのデータを読み込むと、汚れ読み、繰り返し読み、幻読みなどの問題が発生します.
      
ISO LATION_REPEATABLE_READ:複数回の読み出しを要求するデータは同じでなければいけません.事務自体がデータを更新しない限り、汚い読みや繰り返しの問題を防止できます.
      
ISO LATION_SERIALIZABLE:完全な隔離レベルで、すべての問題を防止し、データ対応のテーブルをロックし、効率的な問題があります.
実際には、事務の伝播特性に対して、対応する隔離レベルを設定することができる.Springの中で一番多く使われているのがPROPAAGATIOINです.REQUIREDという伝播挙動.この意味は、アプリケーションにもう一つの事務が存在すると、もう一つの事務が入ってきたら、この事務に参加します.もし事務が存在しないなら、新しい事務を開始します.
事務伝播特性設定のプロファイルを見てください.

<?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.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<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="sessionFactory" />
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice> 
	
	<aop:config>
		<aop:pointcut id="allDaoMethod" expression="execution (* org.whatisjava.dao..*.*(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod"/>
	</aop:config>
	</beans>
       
<tx:method>には、属性設定があり、伝播行為、隔離層、読み取り専用、タイムアウトなどに対応する「propagation」、「isolation」、「timeout」、「read-only」などがあります.ここで設定されている伝播属性は「REQUID」であり、それに対応するデフォルトの分離層は「DEFAULT」、「timereadmore 1」です.これらのパラメータによって、異なるパラメータ設定を選択することもできます.

...
<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
			<tx:method name="del*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
			<tx:method name="update*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice> 
....
これはxmlプロファイルに基づくトランザクション属性の伝播制御です.また、注釈方式に基づいてもいいです.下記のコードを見てください.

package org.whatisjava.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.whatisjava.dao.UserDao;
import org.whatisjava.po.User;

public class UserDaoBean extends HibernateDaoSupport implements UserDao {

	@Transactional(propagation = Propagation.REQUIRED)
	public void delUser(Integer id) {
		getHibernateTemplate().delete((User)getHibernateTemplate().get(User.class, id));
	}

	@Transactional(readOnly = true)
	public User findUser(Integer id) {
		return (User)getHibernateTemplate().get(User.class, id);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void insertUser(User user) {
		getHibernateTemplate().save(user);
	}

	@Transactional(readOnly = true)
	public List listUser() {
		return getHibernateTemplate().find("from User user");
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void updateUser(User user) {
		getHibernateTemplate().update(user);
	}

}

これらの注釈の設定を有効にするには、設定ファイルに最後の行を追加する必要があります.

<?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.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<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="sessionFactory" />
	</bean>
	
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

     
はい、以上がスプリングの中の事務の属性の紹介とSpringの中でどのように事務を声明で管理しますか?