Tomcat数据库连接池估计现在使用的不多了,现在都是DBCP,C3P0之类的,在很多软件项目中,dbcp,c3p0确实很好,配置一下随便用,使用Tomcat连接池可能不是很方便;但后来发现,在分布式服务器或者分库中使用Tomcat数据库连接池确实不错,可以一次启动打开不同库上的连接池;
以下是配置文件,conf下的server.xml文件(原来内容全部清除)
< Server port ="8005" shutdown ="SHUTDOWN" >
< GlobalNamingResources >
< Resource auth ="Container" driverClassName ="org.gjt.mm.mysql.Driver" maxActive ="200" maxIdle ="30" maxWait ="10000" name ="jdbc/table" removeAbandoned ="true" removeAbandonedTimeout ="60" type ="javax.sql.DataSource" url ="jdbc:mysql://122.207.1.1:3306/table?autoReconnect=true" username ="root" password ="root" />
< Resource auth ="Container" driverClassName ="org.gjt.mm.mysql.Driver" maxActive ="200" maxIdle ="30" maxWait ="10000" name ="jdbc/tablelog" removeAbandoned ="true" removeAbandonedTimeout ="60" type ="javax.sql.DataSource" url ="jdbc:mysql://122.207.1.1:3306/table_log?autoReconnect=true" username ="root" password ="root" />
< Resource auth ="Container" driverClassName ="org.gjt.mm.mysql.Driver" maxActive ="200" maxIdle ="30" maxWait ="10000" name ="jdbc/tableR1" removeAbandoned ="true" removeAbandonedTimeout ="60" type ="javax.sql.DataSource" url ="jdbc:mysql://122.207.1.1:3306/table_main?autoReconnect=true" username ="root" password ="root" />
< Resource auth ="Container" driverClassName ="org.gjt.mm.mysql.Driver" maxActive ="200" maxIdle ="30" maxWait ="10000" name ="jdbc/tableR2" removeAbandoned ="true" removeAbandonedTimeout ="60" type ="javax.sql.DataSource" url ="jdbc:mysql://122.207.1.1:3306/table_main?autoReconnect=true" username ="root" password ="root" />
</ GlobalNamingResources >
< Service name ="Catalina" >
< Connector port ="80" maxHttpHeaderSize ="8192" maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75" enableLookups ="false" acceptCount ="100" connectionTimeout ="20000" disableUploadTimeout ="true" />
< Engine name ="Catalina" defaultHost ="localhost" >
< Host name ="localhost" appBase ="webapps" unpackWARs ="true" autoDeploy ="true" xmlValidation ="false" xmlNamespaceAware ="false" >
< Context docBase ="E:\Workspaces\MyEclipse8.6\myproject\WebRoot" path ="" reloadable ="false" >
< ResourceLink global ="jdbc/table" name ="jdbc/table" type ="javax.sql.DataSource" />
< ResourceLink global ="jdbc/tablelog" name ="jdbc/tablelog" type ="javax.sql.DataSource" />
< ResourceLink global ="jdbc/tableR1" name ="jdbc/tableR1" type ="javax.sql.DataSource" />
< ResourceLink global ="jdbc/tableR2" name ="jdbc/tableR2" type ="javax.sql.DataSource" />
</ Context >
</ Host >
</ Engine >
</ Service >
</ Server >
这里总共有4个库,当然这些库不一定只在一台机器上,可以通过Resource节点下的url来指定,这里全是是本地的库,还要把mysql的连接包放入Tomcat下的lib目录中;
这里的Context节点配置了这个Tomcat容器只能为myproject这个工程使用,可以不用部署,直接启动Tomcat即可,这样可以加快启动速度,在Tomcat启动同时,数据库
连接也被打开,我们只需要在jav工程中读取连接即可
连接形式如下:
DataSource datesource1="java:comp/env/jdbc/table"
DataSource datesource2="java:comp/env/jdbc/tablelog"
DataSource datesource3="java:comp/env/jdbc/tableR1"
DataSource datesource4="java:comp/env/jdbc/tableR2"
这样就可以在一个工程中获取多个数据库的连接;确实很强大;
关于Tomcat的server.xml的各个节点于属性的意思这里就不再多说了,大家从字面意思上就可以了解