スプリングを使用したトランザクションの3つの方法
9222 ワード
1、プログラミング式事務管理
スプリングのプロファイル
JAvaコード:
2、注釈ベースのトランザクション(注釈はインタフェースを実現したserviceまたはdaoにのみ作用する)、クラスまたはメソッドに作用する
Springのプロファイルには、次のものが含まれます.
Attribute
Default
Description
transactionManager
Name of transaction manager to use. Only required if the name of the transaction manager is not
proxy
The default mode "proxy"processes annotated beans to be proxied using Spring's AOP framework (following proxy semantics, as discussed above, applying to method calls coming in through the proxy only). The alternative mode "aspectj"instead weaves the affected classes with Spring's AspectJ transaction aspect, modifying the target class byte code to apply to any kind of method call. AspectJ weaving requires spring-aspects.jar in the classpath as well as load-time weaving (or compile-time weaving) enabled. (See Section 7.8.4.5, “Spring configuration” for details on how to set up load-time weaving.)
false
Applies to proxy mode only. Controls what type of transactional proxies are created for classes annotated with the
Ordered.LOWEST_PRECEDENCE
Defines the order of the transaction advice that is applied to beans annotated with
コードに追加
ツールバーの
を選択します.
説明
列挙:
列挙:
ブールがた
読み書き型トランザクションvs.読取り専用トランザクション
int型(秒単位)
トランザクションタイムアウト
異常クラスのセットで、発生した場合にロールバックする必要があります.デフォルトではchecked exceptionsはロールバックされず、unchecked exceptions(
1組の
異常クラス名のセットです.発生した場合はロールバックする必要があります.
異常クラスのセットで、発生した場合はロールバックしない必要があります.
1組の
異常クラスのセットで、発生した場合はロールバックしない必要があります.
3、宣言取引
私のもう一つのブログを見てください:http://www.cnblogs.com/yangzhilong/archive/2013/02/04/2891819.html
スプリングのプロファイル
<!-- -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- -->
<bean id="defaultTransactionDefinition"
class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>
JAvaコード:
@Resource
private DataSourceTransactionManager transactionManager;
@Resource
private DefaultTransactionDefinition defaultTransactionDefinition;
public void delete(String id) throws Exception{
TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition);
try{
this.dao.delete(id);
transactionManager.commit(status);//
} catch(Exception e){
transactionManager.rollback(status);// ,
log.warn(e.getMessage());
throw e;
}
}
2、注釈ベースのトランザクション(注釈はインタフェースを実現したserviceまたはdaoにのみ作用する)、クラスまたはメソッドに作用する
Springのプロファイルには、次のものが含まれます.
<!-- -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven/>
のプロパティAttribute
Default
Description
transaction-manager
transactionManager
Name of transaction manager to use. Only required if the name of the transaction manager is not
transactionManager
, as in the example above. mode
proxy
The default mode "proxy"processes annotated beans to be proxied using Spring's AOP framework (following proxy semantics, as discussed above, applying to method calls coming in through the proxy only). The alternative mode "aspectj"instead weaves the affected classes with Spring's AspectJ transaction aspect, modifying the target class byte code to apply to any kind of method call. AspectJ weaving requires spring-aspects.jar in the classpath as well as load-time weaving (or compile-time weaving) enabled. (See Section 7.8.4.5, “Spring configuration” for details on how to set up load-time weaving.)
proxy-target-class
false
Applies to proxy mode only. Controls what type of transactional proxies are created for classes annotated with the
@Transactional
annotation. If the proxy-target-class
attribute is set to true
, then class-based proxies are created. If proxy-target-class
is false
or if the attribute is omitted, then standard JDK interface-based proxies are created. (See Section 7.6, “Proxying mechanisms” for a detailed examination of the different proxy types.) order
Ordered.LOWEST_PRECEDENCE
Defines the order of the transaction advice that is applied to beans annotated with
@Transactional
. (For more information about the rules related to ordering of AOP advice, see Section 7.2.4.7, “Advice ordering” .) No specified ordering means that the AOP subsystem determines the order of the advice. コードに追加
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
@Transactional
注記のプロパティツールバーの
を選択します.
説明
(propagation)
列挙:
Propagation
オプションの伝播設定 (isolation)
列挙:
Isolation
オプションの独立性レベル(デフォルト:ISOLATION_DEFAULT
) (readOnly)
ブールがた
読み書き型トランザクションvs.読取り専用トランザクション
(timeout)
int型(秒単位)
トランザクションタイムアウト
(rollbackFor)
Class
クラスの一組の例は、Throwable
のサブクラスでなければならない.異常クラスのセットで、発生した場合にロールバックする必要があります.デフォルトではchecked exceptionsはロールバックされず、unchecked exceptions(
RuntimeException
のサブクラス)のみがトランザクションロールバックされます. (rollbackForClassname)
1組の
Class
類の名前は、Throwable
の子類でなければならない.異常クラス名のセットです.発生した場合はロールバックする必要があります.
(noRollbackFor)
Class
クラスの一組の例は、Throwable
のサブクラスでなければならない.異常クラスのセットで、発生した場合はロールバックしない必要があります.
(noRollbackForClassname)
1組の
Class
類の名前は、Throwable
の子類でなければならない.異常クラスのセットで、発生した場合はロールバックしない必要があります.
3、宣言取引
私のもう一つのブログを見てください:http://www.cnblogs.com/yangzhilong/archive/2013/02/04/2891819.html