tomcat5.5.12 连接池设置
2005-10-27 15:31
375 查看
关于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>
...
(2)conf/context.xml含以下标记
<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>
(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(); //释放连接
}
}
%>
(5)重启动tomcat,MSSQLSERVER ,测试通过。
(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>
...
(2)conf/context.xml含以下标记
<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>
(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(); //释放连接
}
}
%>
(5)重启动tomcat,MSSQLSERVER ,测试通过。
相关文章推荐
- Tomcat 的数据库连接池设置与应用
- tomcat 中设置连接池
- Tomcat5.0版本的设置连接池
- Tomcat4/5连接池的设置及简单应用示例
- Tomcat4/5连接池的设置及简单应用示例
- Tomcat 的数据库连接池设置与应用
- Tomcat Oracle数据库连接池设置与使用 (转)
- Tomcat设置Sqlserver连接池
- Tomcat6.0的Mysql数据库连接池设置与举例(总结)
- Tomcat 的数据库连接池设置与应用(转载)
- Tomcat5.0以上版本设置连接池
- Tomcat 的数据库连接池设置与应用
- Tomcat4/5连接池的设置及简单应用示例
- Tomcat4/5连接池的设置及简单应用示例
- AccessDB Tomcat 连接池设置
- Tomcat6的连接池设置
- Tomcat 的数据库连接池设置与应用
- tomcat5.5.12 配置连接池(oracle)
- Tomcat 的数据库连接池设置与应用
- 基于tomcat5.5的数据库连接池环境设置