読者です 読者をやめる 読者になる 読者になる

トランザクションを試してみる(設定ファイル編)


設定地獄にはまりかけましたが、何とか設定ファイルに書くことでトランザクション
することができました。

追加したコンポーネントは、workerAdapterFactoryとrequiredTx。
多分これであっていると思う。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lucy-config PUBLIC "-//LUCY//DTD LUCY//EN" "http://www.lucy.org/dtd/lucy-config.dtd">
<lucy-config>

	<include>"dbfluteBeans.xml"</include>
	
 	<component name="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" instance="singleton">
		<inject method="setXaDataSourceClassName">"org.h2.jdbcx.JdbcDataSource"</inject>
		<inject method="setUniqueResourceName">"jdbc/h2/xaDatasource"</inject>
		<inject method="setPoolSize">20</inject>
		<inject method="setBorrowConnectionTimeout">60</inject>
		<inject method="setXaProperties"><![CDATA[
			p = dataSource.XaProperties;
			p["URL"]="jdbc:h2:tcp://127.0.0.1:9095/t2db";
			p["User"]="sa";
			p["Password"]="";
			p;
		]]></inject>
		<destroy method="close"/>
	</component>
	
	<component name="workerAdapterFactory" class="org.t2framework.lucy.tx.interceptor.DefaultWorkerAdapterFactory" />
	 
	<component name="txMan" class="com.atomikos.icatch.jta.UserTransactionManager" instance="singleton">
		<inject method="setForceShutdown">true</inject>
		<init method="init"/>
		<destroy method="close"/>
	</component>

	<component name="usrTx" class="com.atomikos.icatch.jta.UserTransactionImp" instance="singleton">
		<inject method="setTransactionTimeout">300</inject>
	</component>

	<component name="txPro" class="org.t2framework.lucy.tx.JTATransactionProcesser" instance="singleton">
		<inject method="setTxManager">txMan</inject>
		<inject method="setUserTx">usrTx</inject>
	</component>
  	
	<!-- Transaction -->
  	<component name="requiredTx" class="org.t2framework.lucy.tx.interceptor.RequiredInterceptor" />

	<!-- Service -->
	<component name="employee" class="org.t2framework.samples.todo.service.impl.EmployeeServiceImpl" instance="singleton">
		<aspect pointcut="update">requiredTx</aspect>
	</component>
	
	<!-- Page(@Injectを使ってインジェクトするようにした) -->
	<component class="org.t2framework.samples.todo.page.EmployeePage" instance="request" />
	
</lucy-config>


そのときのログ。
コミット編

10:39:00 [http-8080-1] INFO  atomikos - createCompositeTransaction ( 300000 ): created new ROOT transaction with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - addParticipant ( 192.168.0.3.tm0000100004192.168.0.3.tm1 ) for transaction 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - XAResource.start ( 192.168.0.3.tm0000100004192.168.0.3.tm1 , XAResource.TMNOFLAGS ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - XAResource.end ( 192.168.0.3.tm0000100004192.168.0.3.tm1 , XAResource.TMSUCCESS ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100004
10:39:00 [http-8080-1] INFO  atomikos - commit() done (by application) of transaction 192.168.0.3.tm0000100004
10:39:00 [Atomikos:2] INFO  atomikos - XAResource.prepare ( 192.168.0.3.tm0000100004192.168.0.3.tm1 ) returning OK on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:39:00 [Atomikos:3] INFO  atomikos - XAResource.commit ( 192.168.0.3.tm0000100004192.168.0.3.tm1 , false ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa

ロールバック編

10:37:02 [http-8080-1] INFO  atomikos - createCompositeTransaction ( 300000 ): created new ROOT transaction with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - addParticipant ( 192.168.0.3.tm0000100003192.168.0.3.tm1 ) for transaction 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - XAResource.start ( 192.168.0.3.tm0000100003192.168.0.3.tm1 , XAResource.TMNOFLAGS ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - XAResource.end ( 192.168.0.3.tm0000100003192.168.0.3.tm1 , XAResource.TMSUCCESS ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - setRollbackOnly() called for transaction 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [http-8080-1] INFO  atomikos - getCompositeTransaction()  returning instance with id 192.168.0.3.tm0000100003
10:37:02 [Atomikos:3] INFO  atomikos - XAResource.rollback ( 192.168.0.3.tm0000100003192.168.0.3.tm1 ) called on resource jdbc/h2/xaDatasource represented by XAResource instance xads1: url=jdbc:h2:tcp://127.0.0.1:9095/t2db user=sa
10:37:02 [http-8080-1] INFO  atomikos - rollback() done of transaction 192.168.0.3.tm0000100003



設定地獄に陥ったのは、コンポーネントの登録やインジェクションを
アノテーションで書いてたり、設定ファイルに書いてたりしたから。

特に複数人で開発するときには、ちゃんとしたルールを決めて書く必要がありそう。