2009-08-13

about SqlMapClientFactoryBean of Spring Framework

SqlMapClientBean of springframework creates SqlMapClient with EXTERNAL transaction management by default and internally wrap the given data source to TransactionAwareDataSourceProxy.

So, the typical setup for iBATIS SqlMapClient bean in spring configuration would be like the following.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
 <property name="username" value="_user_" />
 <property name="password" value="_passwd_" />
 <property name="defaultAutoCommit" value="true" />
 <property name="maxActive" value="10" />
 <property name="maxIdle" value="10" />
 <property name="minIdle" value="0" />
 <property name="initialSize" value="10" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 <property name="defaultTimeout" value="120" />
</bean>
 
<tx:annotation-driven 
 transaction-manager="transactionManager"
 mode="proxy"
 proxy-target-class="true"
/>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
 <description>default SqlMapClient bean for DAOs</description>
 <property name="configLocation" value="classpath:_pathto_/sql-map.xml" />
  
 <property name="dataSource" ref="dataSource" />
 <!-- Default setting is specified for clarity. -->
 <property name="useTransactionAwareDataSource" value="true"/>
 <!-- Default setting is specified for clarity. -->
 <property name="transactionConfigClass">
  <value type="java.lang.Class">com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
 </property>

</bean>

At the above sample, the default setup for useTransactionAwareDataSource and transactionConfigClass properties is specified for clarity.

ExternalTransactionConfig would set the "SetAutoCommitAllowed" property of SqlMapClient to false, in contrast to the iBATIS default, to always keep the original autoCommit value as provided by the connection pool.

For more information, refer API documentation of SqlMapClientFactoryBean, specially setDataSource, setUseTransactionAwareDataSource, and setTransactionConfigClass methods.

0 comments:

Post a Comment