关于tomcat 5.5.12 如何配置连接池。
(1)admin登陆后,选 Resources / Data Source,Create New Data Source,创建JNDI后conf/server.xml 里含有以下内容:
(url与driverClassName相对应)
...
< GlobalNamingResources >
< Environment
name ="simpleValue"
type ="java.lang.Integer"
value ="30" />
< Resource
auth ="Container"
description ="User database that can be updated and saved"
name ="UserDatabase"
type ="org.apache.catalina.UserDatabase"
pathname ="conf/tomcat-users.xml"
factory ="org.apache.catalina.users.MemoryUserDatabaseFactory" />
< Resource
name ="jdbc/sqlserver"
type ="javax.sql.DataSource"
password ="111111"
driverClassName ="net.sourceforge.jtds.jdbc.Driver"
maxIdle ="2"
maxWait ="5000"
username ="sa"
url ="jdbc:jtds:sqlserver://localhost:1433/demo"
maxActive ="4" />
</ GlobalNamingResources >
...
< GlobalNamingResources >
< Environment
name ="simpleValue"
type ="java.lang.Integer"
value ="30" />
< Resource
auth ="Container"
description ="User database that can be updated and saved"
name ="UserDatabase"
type ="org.apache.catalina.UserDatabase"
pathname ="conf/tomcat-users.xml"
factory ="org.apache.catalina.users.MemoryUserDatabaseFactory" />
< Resource
name ="jdbc/sqlserver"
type ="javax.sql.DataSource"
password ="111111"
driverClassName ="net.sourceforge.jtds.jdbc.Driver"
maxIdle ="2"
maxWait ="5000"
username ="sa"
url ="jdbc:jtds:sqlserver://localhost:1433/demo"
maxActive ="4" />
</ GlobalNamingResources >
...
(2)conf/context.xml含以下标记
<
Context
>
...
< ResourceLink name ="jdbc/sqlserver" global ="jdbc/sqlserver" type ="javax.sql.DataSource" />
</ Context >
...
< ResourceLink name ="jdbc/sqlserver" global ="jdbc/sqlserver" type ="javax.sql.DataSource" />
</ Context >
(3)在webapps下的项目所在的目录的 WEB-INF/web.xml 里含以下标记
<
web-app
version
="2.4"
>
...
< resource-ref >
< description > MSSQLSERVER Datasource </ description >
< res-ref-name > jdbc/sqlserver </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
</ web-app >
...
< resource-ref >
< description > MSSQLSERVER Datasource </ description >
< res-ref-name > jdbc/sqlserver </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
</ web-app >
(4)编写测试 test.jsp
<%
@ page
import
=
"
javax.naming.*
"
%>
<% @ page import = " javax.sql.* " %>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup( " java:/comp/env " );
DataSource ds = (DataSource)envContext.lookup( " jdbc/sqlserver " );
Connection conn = ds.getConnection();
if (conn != null ){
out.print( " Connection pool success<br> " );
try {
PreparedStatement ps = conn.prepareStatement( " select * from table1 " );
ResultSet rs = ps.executeQuery();
while (rs.next()){
out.print(rs.getString( 2 ) + " <br> " );
}
}
catch (Exception e){
out.print( " -------- Error: " + e + " -------------<br> " );
}
finally {
conn.close(); // 释放连接
}
}
%>
<% @ page import = " javax.sql.* " %>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup( " java:/comp/env " );
DataSource ds = (DataSource)envContext.lookup( " jdbc/sqlserver " );
Connection conn = ds.getConnection();
if (conn != null ){
out.print( " Connection pool success<br> " );
try {
PreparedStatement ps = conn.prepareStatement( " select * from table1 " );
ResultSet rs = ps.executeQuery();
while (rs.next()){
out.print(rs.getString( 2 ) + " <br> " );
}
}
catch (Exception e){
out.print( " -------- Error: " + e + " -------------<br> " );
}
finally {
conn.close(); // 释放连接
}
}
%>
(5)重启动tomcat,MSSQLSERVER ,测试通过。