tomcat 几种连接池配置代码(包括tomcat5.0,tomcat5.5x,tomcat6.0)
服务器 发布日期:2024/12/29 浏览次数:1
正在浏览:tomcat 几种连接池配置代码(包括tomcat5.0,tomcat5.5x,tomcat6.0)
Tomcat6.0连接池配置
1.配置tomcat下的conf下的context.xml文件,在之间添加连接池配置:
复制代码 代码如下:
<Resource name="jdbc/oracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url=" jdbc:oracle:thin:@host:port:databse"
username=" user "
password="password"
maxActive="100"
maxIdle="30"
maxWait="10000" />
2.配置你的应用下的web.xml中的之间加入:
复制代码 代码如下:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.把连接数据库的第三方驱动放到common/lib下面就ok了
4.测试程序我就不写了
Tomcat5.5x连接池配置
方式一、全局数据库连接池
1、通过管理界面配置连接池,或者直接在tomcat\conf\server.xml的GlobalNamingResources中增加
复制代码 代码如下:
<Resource name="jdbc/mydb"
type="javax.sql.DataSource"
password="mypwd"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
validationQuery="select 1"
username="sa"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"
maxActive="4"/>
2、在tomcat\webapps\myapp\META-INF\context.xml的Context中增加:
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
这样就可以了。
方式二、全局数据库连接池
1、同上
2、在tomcat\conf\context.xml的Context中增加:
复制代码 代码如下:
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
方式三、局部数据库连接池
只需在tomcat\webapps\myapps\META-INF\context.xml的Context中增加:
复制代码 代码如下:
<Resource name="jdbc/mydb" type="javax.sql.DataSource" password="mypwd" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" validationQuery="select 1" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb" maxActive="4"/>
参数说明:
driveClassName:JDBC驱动类的完整的名称;
maxActive:同时能够从连接池中被分配的可用实例的最大数;
maxIdle:可以同时闲置在连接池中的连接的最大数;
maxWait:最大超时时间,以毫秒计;
password:用户密码;
url:到JDBC的URL连接;
user:用户名称;
validationQuery:用来查询池中空闲的连接。
以上三种方式在tomcat 5.5.4下都可以。另外,sql server的jdbc driver是从微软网站上下载的sql server jdbc (sp3)。
tomcat5.0连接池配置
在tomcat 的下面路径(Tomcat \conf\Catalina\localhost)下建一个xml文件,内容如下
复制代码 代码如下:
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<!-- Class name for JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<!-- Autocommit setting. This setting is required to make
Hibernate work. Or you can remove calls to commit(). -->
<parameter>
<name>defaultAutoCommit</name>
<value>true</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://url/filedb;charset=gb2312;autoReconnect=true</value>
</parameter>
<!-- Recover abandoned connections -->
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<!-- Set the number of seconds a dB connection has been idle
before it is considered abandoned.
-->
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<!-- Log a stack trace of the code which abandoned the dB
connection resources.
-->
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>