首页 > 代码库 > ibatis配置连接池

ibatis配置连接池

iBatis的连接设置

持久层中间件iBatis连接数据库的方式有3种,连接数据库的方式是JDBC,可以通过在XML文件中配置数据库连接,也可以在properties文件中配置。下面列出transactionManagertypeJDBC3种连接配置。

 

dataSourcetypeSIMPLE的连接配置

以下是配置数据库连接参数的properties文件sql-map-config.properties的内容:

    ## SimpleDataSource properties

    ## Use only if useSimpleConfiguration=true

 

    # DMDBMS

    SimpleDriver=dm.jdbc.driver.DmDriver

    SimpleUrl=jdbc:dm://localhost:12345/SYSTEM

    SimpleUsername=SYSDBA

SimplePassword=SYSDBA

 

以下是配置文件sql-map-config.xml的内容:

 

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 
 3 <!DOCTYPE sqlMapConfig
 4 
 5     PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
 6 
 7     "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
 8 
 9  
10 
11 <sqlMapConfig>
12 
13     <properties resource="res/config/sql-map-config.properties" />
14 
15     <settings cacheModelsEnabled="true" enhancementEnabled="true"
16 
17         maxSessions="64" maxTransactions="20" maxRequests="128"
18 
19         useStatementNamespaces="true" />
20 
21     <transactionManager type="JDBC">
22 
23         <dataSource type="SIMPLE">
24 
25             <property value="$" name="JDBC.Driver" />
26 
27             <property value="$" name="JDBC.ConnectionURL" />
28 
29             <property value="$" name="JDBC.Username" />
30 
31             <property value="$" name="JDBC.Password" />
32 
33             <property value="15" name="Pool.MaximumActiveConnections" />
34 
35             <property value="15" name="Pool.MaximumIdleConnections" />
36 
37           <property value="1000" name="Pool.MaximumWait" />
38 
39         </dataSource>
40 
41     </transactionManager>
42 
43     <sqlMap resource="res/sqlmap/User.xml" />
44 
45     <sqlMap resource="res/sqlmap/Role.xml" />
46 
47     <sqlMap resource="res/sqlmap/Permit.xml" />
48 
49 </sqlMapConfig>

 

说明:如果要优化数据库连接池,可以在sql-map-config.xml修改相关参数:

Pool.MaximumActiveConnections 最大连接数

Pool.MaximumIdleConnections    允许的挂起(idle)连接数

Pool.MaximumWait             最长等待的时间

 

dataSourcetypeDHCP的连接配置

<transactionManager type="JDBC">

       <dataSource type="DBCP">

              <property name="JDBC.Driver" value="$"/>

              <property name="JDBC.ConnectionURL" value="$"/>

              <property name="JDBC.Username" value="$"/>

              <property name="JDBC.Password" value="$"/>

              <!-- Optional properties below -->

              <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 now"/>

              <property name="Pool.LogAbandoned" value="false"/>

              <property name="Pool.RemoveAbandoned" value="false"/>

              <property name="Pool.RemoveAbandonedTimeout" value="50000"/>

       </datasource>

</transactionManager>

 

说明:上面的$等连接信息定义在另外一个properties文件或XML文件中,同dataSourcetypeSIMPLE的连接配置。

 

dataSourcetypeJNDI的连接配置

<transactionManager type="JDBC" >

       <dataSource type="JNDI">

              <property name="DataSource" value="java:comp/env/jdbc/jpetstore"/>

       </dataSource>

</transactionManager>

 

说明:前提是在应用服务器中配置了JNDIjdbc/jpetstore的配置。

 

<bean id="dataSourceA" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="automaticTestTable" value="CONNECTION_TEST" />
        <property name="testConnectionOnCheckout" value="true" />
        <property name="acquireRetryAttempts" value="1" />
        <property name="driverClass" 
            value="org.apache.derby.jdbc.ClientDriver" />
        <property name="jdbcUrl" 
            value="jdbc:derby://localhost:1527/d:/work/lms" />
        <property name="user" value="someUser" />
        <property name="password" value="somePassword" />
</bean>        
     <bean id="dataSourceB" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="automaticTestTable" value="CONNECTION_TEST" />
        <property name="testConnectionOnCheckout" value="true" />
        <property name="acquireRetryAttempts" value="1" />
        <property name="driverClass" value="com.ibm.as400.access.AS400JDBCDriver" />
        <property name="jdbcUrl" 
            value="jdbc:as400://192.168.85.1.1;driver=native;naming=system;errors=full;prompt=false;libraries=*LIBL,SOME_LIB,OTHER_LIB;
                transaction isolation=read committed" />
        <property name="user" value="someUser" />
        <property name="password" value="somePassword" />