Tomcat连接池配置方法V2.1

1,063 views

Published on

Tomcat连接池配置方法

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,063
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Tomcat连接池配置方法V2.1

  1. 1. Tomcat 连接池配置方法 Zianed Hou zianed@live.cn 0、准备工作 1、配置Tomcat的server.xml文件 2、工程中加入context.xml文件 3、Catalina/localhost/加入${appName}.xml 4、注意事项 5、测试jsp 6、Tomcat6 对连接池的支持 7、javax.sql.RowSet对连接池的支持 Zianed Version 2.1 1
  2. 2. 0、准备工作 在$CATALINA_HOME/common/lib/中加入 class12.jar 包 1、配置Tomcat的server.xml文件 在$CATALINA_HOME/conf/server.xml 的<Host>中 : <Context path="/test" docBase="E:/test"> <Resource name="jdbc/linary" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:linary"/> </Context> 2、工程中加入context.xml文件 在 test 工程的 META-INF 中加入 context.xml 文件: 注:此时工程必须是在 webapp 中 <Context path="/test" docBase="/test"> <Resource name="jdbc/linary" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:linary"/> </Context> Zianed Version 2.1 2
  3. 3. 3、Catalina/localhost/加入${appName}.xml 在 $CATALINA_HOME/conf/Catalina/localhost/ 下 加 入 一 个 和 工 程 名 一 致 的 test.xml 文件: <Context path="/test" docBase="E:/test"> <Resource name="jdbc/linary" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:linary"/> </Context> 4、注意事项 有时需要在 WEB-INF/web.xml 中加入,增加工程依赖的资源 注:在大部分时间没有加,并不报错 <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/linary</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 5、测试 jsp 测试代码: //getPool.jsp <%@page contentType="text/html; charset=GBK"%> <%@ page import="java.sql.*"%> <%@ page import="javax.naming.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> Zianed Version 2.1 3
  4. 4. <title>Test of connection pool</title> </head> <body> <% out.print("Start<br/>"); try{ InitialContext ctx = new InitialContext(); javax.sql.DataSource connectionPool = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/linary"); Connection conn = connectionPool.getConnection(); out.print("DB connection pool run OK!"); conn.close(); } catch(Exception ex){ out.print(ex.getMessage()); ex.printStackTrace(); } %> </body> </html> 6、Tomcat6 对连接池的支持 Tomcat 引入了 tomcat-dbcp 包,自动支持 dbcp 连接池的构建。 测试代码: public class DataSourcePool { public static DataSource initDataSource(String url, String username, String password) { BasicDataSource bds = new BasicDataSource(); bds.setUrl(url); bds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); bds.setUsername(username); bds.setPassword(password); bds.setMaxActive(5); return bds; } public static void closeDataSource(DataSource ds) throws SQLException { BasicDataSource bds = (BasicDataSource) ds; Zianed Version 2.1 4
  5. 5. bds.close(); } /** * @param args * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { String url = "jdbc:oracle:thin:@192.168.1.125:1521:testjoe"; String username = "linary"; String password = "linary"; // 创建BasicDataSource DataSource dataSource = initDataSource(url, username, password); Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); // 创建JDBC对象 Connection conn = null; Statement st = null; ResultSet rs = null; try { conn = dataSource.getConnection(); st = conn.createStatement(); String sql = "select * from emp"; rs = st.executeQuery(sql); System.out.println("ResultSet Results:"); int numcols = rs.getMetaData().getColumnCount(); while (rs.next()) { for (int i = 1; i <= numcols; i++) { System.out.print(rs.getString(i) + "t"); } System.out.println(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { Zianed Version 2.1 5
  6. 6. rs.close(); } if (st != null) { st.close(); } if (conn != null) { conn.close(); } if (dataSource != null) { closeDataSource(dataSource); } } catch (SQLException e) { e.printStackTrace(); } } } 7、javax.sql.RowSet对连接池的支持 可以直接将连接池作为参数传递给RowSet 测试代码: <%@page contentType="text/html; charset=GBK"%> <%@ page import="java.sql.*"%> <%@ page import="javax.sql.*"%> <%@ page import="com.sun.rowset.*"%> <%@ page import="javax.sql.rowset.*"%> <%@ page import="javax.naming.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Test of connection pool</title> </head> <body> <% out.print("Start<br/>"); try{ InitialContext ctx = new InitialContext(); javax.sql.DataSource connectionPool = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/linary"); Zianed Version 2.1 6
  7. 7. Connection conn = connectionPool.getConnection(); out.print("DB connection pool run OK!<br/>"); conn.close(); } catch(Exception ex){ out.print(ex.getMessage()); ex.printStackTrace(); } %> <% try{ //InitialContext ctx = new InitialContext(); //javax.sql.DataSource connectionPool = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/linary"); JdbcRowSet jrs=new JdbcRowSetImpl(); jrs.setDataSourceName("java:comp/env/jdbc/linary"); jrs.setCommand("select * from emp"); jrs.execute(); int numcols = jrs.getMetaData().getColumnCount(); System.out.println("JdbcRowSet Result:"); out.print("JdbcRowSet Result<br/>"); while (jrs.next()) { for (int i = 1; i <= numcols; i++) { out.print(jrs.getString(i) + "t"); } out.print("<br/>"); } jrs.close(); } catch(Exception ex){ out.print(ex.getMessage()); ex.printStackTrace(); } %> </body> </html> Zianed Version 2.1 7
  8. 8. Zianed Homepage:http://my.unix-center.net/~Zianed/ Mail: hxuanzhe86@sina.com MSN:zianed@live.cn QQ:1196123432 QQGroup: 50457022 Date:2009-10-24 Zianed Version 2.1 8

×