第一步:在C:"Tomcat 5.0"conf"server.xml中<Host>….</Host>中增加如下内容,注意:<DefaultContext>属于<Host>下的一级元素。
<DefaultContext>
<Resource name = "jdbc/testdb0_cs" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/testdb0_cs">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.informix.jdbc.IfxDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:informix-sqli://192.177.191.130:6666/testdb0_cs:INFORMIXSERVER=ccbhr_test</value>
</parameter>
<parameter>
<name>username</name>
<value>hrmis</value>
</parameter>
<parameter>
<name>password</name>
<value>hrmis</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>poolPrepareStatements</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<!-- Abandoned DB connections are removed and recycled -->
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<!-- Log a stack trace of the code which abandoned -->
<value>false</value>
</parameter>
</ResourceParams>
</DefaultContext>
第二步:修改C:"eclipse-SDK-3.0.1-win32"eclipse"workspace"zlp"WebContent"WEB-INF下web.xml,在<web-app>…..</web-app>中增加一级元素如下:
<!-- Database Config start -->
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- Database Config end -->
说明:蓝色的路径是你的应用所在路径下的web.xml
第三步:将Informix JDBC Driver拷贝到C:"Tomcat 5.0"common"lib目录下
第四步:重新启动tomcat
二、Tomcat中配置多个数据源的方法
第一步:server.xml 蓝色和绿色部分是两个数据源,分别连接两个不同的数据库。依次类推,配置多个。
<DefaultContext>
<Resource name="jdbc/testdb0_cs1" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/testdb0_cs1">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.informix.jdbc.IfxDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:informix-sqli://192.177.179.208:6666/testdb0_cs1:INFORMIXSERVER=ccbhr_test</value>
</parameter>
<parameter>
<name>username</name>
<value>informix</value>
</parameter>
<parameter>
<name>password</name>
<value>google</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>poolPrepareStatements</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<!-- Abandoned DB connections are removed and recycled -->
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<!-- Log a stack trace of the code which abandoned -->
<value>false</value>
</parameter>
</ResourceParams>
<Resource name="jdbc/testdb0_cs" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/testdb0_cs">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.informix.jdbc.IfxDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:informix-sqli://192.177.191.130:6666/testdb0_cs:INFORMIXSERVER=ccbhr_test</value>
</parameter>
<parameter>
<name>username</name>
<value>informix</value>
</parameter>
<parameter>
<name>password</name>
<value>informix</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>poolPrepareStatements</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<!-- Abandoned DB connections are removed and recycled -->
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<!-- Log a stack trace of the code which abandoned -->
<value>false</value>
</parameter>
</ResourceParams>
</DefaultContext>
第二步:在应用的web.xml中增加新增的数据源,如下所示:
<!-- Database Config start -->
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/testdb0_cs1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- Database Config end -->
第三步:重启tomcat
三、多个数据源的访问方法参考
public Connection getConn(String dsnName)
{
Connection conn = null;
Context ctx = null;
DataSource ds = null;
String jndiStr = null;
try{
jndiStr = "java:comp/env/jdbc/" + dsnName.trim();
System.out.println("jndiStr=[" + jndiStr + "]");
ctx = new InitialContext();
ds = (DataSource) ctx.lookup(jndiStr);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.println("Connect database [" + dsnName.trim() + "] Error:" + e);
}
return conn;
}
说明:其中dsnName是我们要具体访问的数据源JNDI,例如:testdb0_cs1