歡迎您光臨本站 註冊首頁

設置tomcat資料庫連接池

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

設置tomcat資料庫連接池

首先需要將資料庫的jdbc驅動程序jar包拷貝到tomcat的lib目錄下


[代碼] server.xml
01 <!-- Oracle 資料庫 -->

02 <Context path="/app1" docBase="app1" debug="1" reloadable="true" crossContext="true">   

03        

04      <Resource name="jdbc/orcldb" auth="Container" type="javax.sql.DataSource"/>   

05      <ResourceParams name="jdbc/orcldb">   

06       <parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>   

07       <parameter><name>driverClassName</name><value>oracle.jdbc.driver.OracleDriver</value></parameter>   

08       <parameter><name>url</name><value>jdbc:oracle:thin:@localhost:1521:orcl</value></parameter>   

09       <parameter><name>username</name><value>user</value></parameter>   

10       <parameter><name>password</name><value>pwd</value></parameter>   

11       <parameter><name>maxActive</name><value>20</value></parameter>   

12       <parameter><name>maxIdle</name><value>10</value></parameter>   

13       <parameter><name>maxWait</name><value>10000</value></parameter>   

14      </ResourceParams>   

15 </Context>


[代碼] 接下來要在WEB-INF里的web.xml里加上以下信息

01 <?xml version="1.0" encoding="ISO-8859-1"?>     

02 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     

03     "http://java.sun.com/dtd/web-app_2_3.dtd">     

04 <web-app>     

05     <description>oralceDB</description>     

06     <resource-ref>     

07         <description>DB Connection</description>     

08         <res-ref-name>jdbc/orcldb</res-ref-name>     

09         <res-type>javax.sql.DataSource</res-type>     

10         <res-auth>Container</res-auth>     

11     </resource-ref>     

12 </web-app>


[代碼] 調用方法

01 /**   

02      * 返回Oracle資料庫連接 注意:紅色的字體表示他要調用的連接池名,同配置   

03      * @return    Connection   

04      * @throws SQLException   

05      */   

06 public static synchronized Connection getOracleConn() throws SQLException {   

07   try {   

08     Context jndiCntx = new InitialContext();   

09     DataSource ds = (DataSource)jndiCntx.lookup( "java:comp/env/jdbc/orcldb");   

10     return ds.getConnection();   

11   }catch (NamingException ne) {   

12     throw new EJBException(ne);   

13   }   

14  }
《解決方案》

oracle和tomcat的資料庫版本應該說明吧

[火星人 ] 設置tomcat資料庫連接池已經有789次圍觀

http://coctec.com/docs/service/show-post-15523.html