本文使用的軟件版本: tomcat 5.5.15 MySql 5 Eclipse 3.1
正常情況下,配置一個(gè)數(shù)據(jù)庫連接池比較簡單,主要分3步: 1、在tomcat中配置一個(gè)數(shù)據(jù)源,可以通過tomcat自帶的工具進(jìn)行配置,配置完成后,tomcat的server.xml中會(huì)多出如下內(nèi)容: <Resource name="jdbc/mysql" type="javax.sql.DataSource" password="123456" driverClassName="com.mysql.jdbc.Driver" maxIdle="20" maxWait="5000" username="root" url="jdbc:mysql://localhost/test" maxActive="10"/>
2、在某個(gè)Web應(yīng)用的web.xml文件中加入如下代碼: <resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
3、在該Web應(yīng)用的context片段中加入如下代碼: <ResourceLink name="jdbc/mysql" type="javax.sql.DataSource" global="jdbc/mysql"/>
進(jìn)行以上3處配置后,就可以使用數(shù)據(jù)庫連接池了,具體試驗(yàn)代碼如下: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="javax.naming.*"%> <%@ page import="javax.sql.*"%> <%@ page import="java.sql.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>aaaa</h1> <%
Context ctx=null; Connection cnn=null; Statement stmt=null; ResultSet rs=null; try { ctx=new InitialContext(); if(ctx==null){ out.println("1 wrong"); throw new Exception("11"); } DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); if(ds==null){ out.println("2 wrong"); throw new Exception("22"); } cnn=ds.getConnection(); stmt=cnn.createStatement(); rs=stmt.executeQuery("select * from student"); rs.next(); out.println(rs.getString(2));
}catch(SQLException e) { out.println(e.getSQLState()); } finally { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(cnn!=null) cnn.close(); if(ctx!=null) ctx.close(); } %> </body> </html>
下面探討一下在Lomboz中使用數(shù)據(jù)庫連接池會(huì)出現(xiàn)哪些問題: 最重要的一點(diǎn),他無法完成第三步配置,因?yàn)樵贚omboz中啟動(dòng)tomcat時(shí),他不會(huì)讀取該web應(yīng)用的context片段,因此,你在context片段中進(jìn)行的任何配置,對(duì)于Lomboz來講都是無效的,自然也就無法使用數(shù)據(jù)庫連接池了。那么有沒有解決辦法呢?當(dāng)然有,讓我們來分析一下Lomboz啟動(dòng)tomcat的過程,在Lomboz中配置好一個(gè)tomcat服務(wù)器后,他會(huì)生成一個(gè)名叫Servers的項(xiàng)目,該項(xiàng)目和你的其他Eclipse項(xiàng)目位于同一個(gè)目錄,很好找。進(jìn)入Servers文件夾,你會(huì)看見一個(gè)名叫Tomcat v5.5 Server @ localhost-config的文件夾,進(jìn)入該文件夾你會(huì)找到一個(gè)server.xml文件,這個(gè)文件就是在Lomboz中啟動(dòng)tomcat所用到的配置文件,他不會(huì)使用位于tomcat目錄下的配置文件。因此要想使你的配置對(duì)于Lomboz有效,你就應(yīng)該修改這個(gè)文件。最后再說一點(diǎn)重要的,對(duì)這個(gè)文件的修改不可以通過一般的編輯軟件進(jìn)行,必須使用Eclipse自帶的XML編輯器進(jìn)行,否則在Lomboz中將無法啟動(dòng)tomcat。 最后給出一個(gè)修改好的例子,紅色部分為被修改處,僅供參考: <?xml version="1.0" encoding="UTF-8"?> <Server> <Listener className="org.apache.catalina.core.AprLifecycleListener"/> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/> <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/> <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/mysql" type="javax.sql.DataSource" password="123456" driverClassName="com.mysql.jdbc.Driver" maxIdle="20" maxWait="5000" username="root" url="jdbc:mysql://localhost/test" maxActive="10"/> </GlobalNamingResources> <Service name="Catalina"> <Connector connectionLinger="-1" connectionTimeout="60000" maxHttpHeaderSize="8192" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="80" redirectPort="8443" serverSoTimeout="0" tcpNoDelay="true"> </Connector> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"> </Connector> <Engine defaultHost="localhost" name="Catalina"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/> <Host appBase="webapps" name="localhost"> <Context docBase="E:\java程序\addbook\.deployables\addbook" path="/addbook" privileged="true" reloadable="true"> <ResourceLink name="jdbc/mysql" type="javax.sql.DataSource" global="jdbc/mysql"/> </Context><Context docBase="E:\java程序\myweb\.deployables\myweb" path="/myweb" reloadable="true" source="com.ibm.wtp.web.server:myweb"/> <Context docBase="E:\java程序\StrutsTest\.deployables\StrutsTest" path="/StrutsTest" reloadable="true" source="com.ibm.wtp.web.server:StrutsTest"/><Context docBase="E:\MyWeb" path="/aa" reloadable="true"/></Host> </Engine> </Service> </Server>
本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/andycpp/archive/2006/04/14/663642.aspx
|