Tuesday, March 17, 2009

iBATIS Datasource Configuration (plus Spring)

On a project, we originally used iBATIS "raw" and had the prototypical datasource defined like this:


sqlMap.xml
    <transactionManager type="JDBC">     
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${ibatis.mysql.driver}"/>
<property name="JDBC.ConnectionURL" value="${ibatis.mysql.url}/resourcePlanning" /> jdbc:mysql://localhost:3306/powerSource" />
<property name="JDBC.Username" value="${ibatis.mysql.user}"/>
<property name="JDBC.Password" value="${ibatis.mysql.password}"/>

<property name="Pool.PingQuery" value="select 1"/>
<property name="Pool.PingEnabled" value="true"/>
<property name="Pool.PingConnectionsOlderThan" value="${ibatis.timeout}"/>

</dataSource>
</transactionManager>

Upon improving our application architecture to use true DAOs, we concurrently upgraded to use Spring DI in our web app and subsequently took advantage of using Spring 'template' wrappers. However, changing the configuration was quite a lesson learned on my part:

Step 1: Remove transactionManager from iBATIS

Step 2: Create new 'datasource' bean using Spring
spring.xml
    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="url" value="${ibatis.mysql.url}/resourcePlanning"/>
<property name="username" value="${ibatis.mysql.user}"/>
<property name="password" value="${ibatis.mysql.password}"/>
<property name="driverClassName" value="${ibatis.mysql.driver}"/>


<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select 1"/>

<property name="logAbandoned" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="3600"/>

<property name="maxWait" value="20"/>
<property name="maxIdle" value="10"/>
<property name="maxActive" value="-1"/>

</bean>


Step 3: Create new Spring-wrapped SqlMapClient using new 'datasource' bean

spring.xml
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:com/kpe/resourcePlanning/domain/dao/ibatis/sqlMap.xml</value>
</property>

<property name="transactionConfigClass">
<value>com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig</value>
</property>

<property name="dataSource">
<ref bean="dataSource"/>
</property>

</bean>

No comments: