<datasource>是<transactionManager>的一部分,为SQL Map数据源设置了一系列参数。目前SQL Map架构只提供三个DataSource Factory,但您也可以添加自己的实现。下面详细地讨论DataSourceFactory的三个实现及其例子。
SimpleDataSourceFactory
SimpleDataSourceFactory为DataSource提供了一个基本的实现,适用于在没有J2EE容器提供DataSource的情况。它基于iBatis的SimpleDataSource连接池实现。
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="org.postgresql.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:postgresql://server:5432/dbname"/>
<property name="JDBC.Username" value="user"/>
<property name="JDBC.Password" value="password"/>
<!-- OPTIONAL PROPERTIES BELOW -->
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="10000"/>
<property name="Pool.PingQuery" value="select * from dual"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="0"/>
<property name="Pool.PingConnectionsNotUsedFor" value="0"/>
</dataSource>
</transactionManager>
DbcpDataSourceFactory实现使用Jakarta DBCP(Database Connection Pool)的DataSource API提供连接池服务。
<transactionManager type="JDBC"> <dataSource type="DBCP"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> <!-- OPTIONAL PROPERTIES BELOW --> <property name="Pool.MaximumActiveConnections" value="10"/> <property name="Pool.MaximumIdleConnections" value="5"/> <property name="Pool.MaximumWait" value="60000"/> <!-- Use of the validation query can be problematic. If you have difficulty, try without it. --> <property name="Pool.ValidationQuery" value="select * from ACCOUNT"/> <property name="Pool.LogAbandoned" value="false"/> <property name="Pool.RemoveAbandoned" value="false"/> <property name="Pool.RemoveAbandonedTimeout" value="50000"/> </datasource> </transactionManager>
JndiDataSourceFactory在应用容器内部从JNDI Context中查找DataSource实现
<transactionManager type="JDBC" >
<dataSource type="JNDI">
<property name="DataSource" value="java:comp/env/jdbc/jpetstore"/>
</dataSource>
</transactionManager>