這幾天在弄個小東西,要用到數據庫,以前就聽說過數據庫連接池這個概念,所以就打算在這個小東西中加入數據庫連接池。呵呵。從網上搜了一些資料。今天就整理一下。我搜到的設置基本上主要有兩種方法我們以MySQL+TOMCAT為例, MySQL驅動文件不僅要在工程lib目錄下,也要在Tomcat的lib目錄下。1.把DataSource設置到我們的WEB項目中,下面詳細的介紹下:第一步:在我們的WEB項目中的
META-INF文件夾下建立一個
context.xml- <?xml version='1.0' encoding='utf-8'?>
-
- <Context>
-
- <Resource name="jdbc/mysql"
- auth="Container"
- type="javax.sql.DataSource"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost/bbs"
- username="root"
- password="root"
- maxActive="50"
- maxIdle="20"
- maxWait="10000" />
-
- </Context>
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/bbs"
username="root"
password="root"
maxActive="50"
maxIdle="20"
maxWait="10000" />
</Context>
第二步:在我們的WEB項目下的
WEB-INF文件夾下建立一個
web.xml(如果存在了就不用了,直接修改就行了)
(這幾天測試了一下,不做這步也可以,O(∩_∩)O哈哈~省事了)- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/mysql</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
第三步:我們就可以用代碼來獲取
Connection對象了
- package xushun.util;
-
- import java.sql.*;
- import javax.sql.*;
- import javax.naming.*;
-
- public class DBHelper {
-
- public static Connection getConnection() throws SQLException,NamingException
- {
-
- Context initContext = new InitialContext();
- Context envContext = (Context)initContext.lookup("java:/comp/env");
-
- DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
- return ds.getConnection();
- }
- }
package xushun.util;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBHelper {
public static Connection getConnection() throws SQLException,NamingException
{
// 初始化查找命名空間
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
// 找到DataSource
DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
return ds.getConnection();
}
}
2.把DataSource設置到我們的Tomcat中,下面詳細的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):這里我查到的設置方法就有了一點區別了。有的人把DataSource設置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了
先說下在server.xml添加DataSource第一步:在Tomcat的conf中的server.xml文件中找到
- <GlobalNamingResources>
- <!-- Editable user database that can also be used by
- UserDatabaseRealm to authenticate users
- -->
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
修改為
- <GlobalNamingResources>
- <!-- Editable user database that can also be used by
- UserDatabaseRealm to authenticate users
- -->
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- <Resource name="jdbc/bbs"
- auth="Container" type="javax.sql.DataSource"
- driverClassName="com.mysql.jdbc.Driver"
- maxIdle="20"
- maxWait="5000"
- username="root"
- password="admin"
- url="jdbc:mysql://localhost:3306/bbs"
- maxActive="100"
- removeAbandoned="true"
- removeAbandonedTimeout="60"
- logAbandoned="true"/>
- </GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
第二步:在Tomcat的conf文件夾下的context.xml中加入
- <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
第三步:就是在WEB項目的WEB-INF中的web.xml添加
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/mysql</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
還有就是在Tomcat文檔中提到的方法,直接修改context.xml文件了在Tomcat的conf文件夾下的context.xml中加入
- <Resource name="jdbc/bbs"
- auth="Container" type="javax.sql.DataSource"
- driverClassName="com.mysql.jdbc.Driver"
- maxIdle="20"
- maxWait="5000"
- username="root"
- password="admin"
- url="jdbc:mysql://localhost:3306/bbs"
- maxActive="100"
- removeAbandoned="true"
- removeAbandonedTimeout="60"
- logAbandoned="true"/>
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
然后就是在WEB項目的WEB-INF中的web.xml添加
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/mysql</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
就是這些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下資料整理出上面用到的XML文件中各個標簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)<td>JNDI 查找名稱</td> <td>關聯的引用</td>
<td>java:comp/env</td> <td>應用程序環境條目</td>
<td>java:comp/env/jdbc</td> <td>JDBC 數據源資源管理器連接工廠</td>
<td>java:comp/env/ejb</td> <td>EJB 引用</td>
<td>java:comp/UserTransaction</td><td>UserTransaction 引用</td>
<td>java:comp/env/mail</td> <td>JavaMail 會話連接工廠</td>
<td>java:comp/env/url</td> <td>URL 連接工廠</td>
<td>java:comp/env/jms</td> <td>JMS 連接工廠和目標</td>
<td>java:comp/ORB</td> <td>應用程序組件之間共享的 ORB 實例</td>
參考如下
posted on 2012-07-27 19:29
Terry Zou 閱讀(3069)
評論(0) 編輯 收藏 所屬分類:
Tomcat+Eclipse