JSP中tomcat的SQL Server2000數(shù)據(jù)庫連接池的配置 環(huán)境: 1. 數(shù)據(jù)庫:Microsoft SQL Server 2000 2. 數(shù)據(jù)庫驅動程序:net.sourceforge.jtds.jdbc.Driver JNDI(Java Naming and Directory Interface)概述: Tomcat4(5)提供了一個與Java Enterprise Edition應用服務相兼容的JNDI--InitialContext實現(xiàn)實例。它的初始數(shù)據(jù)設置在$CATALINA_HOME/conf/server.xml文件里,并可能在網(wǎng)頁應用環(huán)境描述(/WEB-INF/web.xml)里被下列元素引用: 1) <env-entry>--環(huán)境入口,設置應用程序如何操作。 2) <resource-ref>--資源參數(shù),一般是數(shù)據(jù)庫驅動程序、JavaMail Session、自定義類工廠等。 3) <resource-env-ref>--在Servlet 2.4里用來簡化設置不需認證信息的資源資源如環(huán)境參數(shù)、resource-ref變量。 InitialContext在網(wǎng)頁應用程序初始化時被設置,用來支持網(wǎng)頁應用程序組件。所有的入口和資源都放在JNDI命名空間里的java:comp/env段里。點擊下列網(wǎng)址以獲取更多信息: 1) Java命名和目錄接口(Java Naming and Directory Interface) 2) J2EE平臺說明(J2EE Platform Specification) 設置JNDI資源 設置JNDI資源要在$CATALINA_HOME/conf/server.xml文件里使用下列標志符: 1) <Environment>--設置域個可變的JNDI InitialContext入口的名字和值(同上面說的<env-entry>等價)。 2) <Resource>--設置應用程序可用的資源的名字和類型(同上面說的<resource-ref>等價)。 3) <ResourceParams>--設置Java資源類工廠的名稱或將用的JavaBean屬性。 4) <ResourceLink>--給全局JNDI環(huán)境(JNDI Context)添加一個鏈接。 上述這些標志符必須放在<Context>和</Context>之間(針對專門的網(wǎng)頁應用程序)或<DefaultContext>和</DefaultContext>之間。 此外,設在網(wǎng)頁應用環(huán)境描述(Web Application Descriptor)(/WEB-INF/web.xml)里的名字和值也在初始環(huán)境(Initial Context)里被設置,當被<Environemt>元素值允許時將被重設初始值。 全局變量能在<Server>子元素的<GlobalNamingResources>里設置。 數(shù)據(jù)庫連接池概述: 數(shù)據(jù)庫連接是一種關鍵的有限的昂貴的資源,這一點在多用戶的網(wǎng)頁應用程序中體現(xiàn)得尤為突出。對數(shù)據(jù)庫連接的管理能顯著影響到整個應用程序的伸縮性和健壯性,影響到程序的性能指標。數(shù)據(jù)庫連接池正是針對這個問題提出來的。 數(shù)據(jù)庫連接池負責分配、管理和釋放數(shù)據(jù)庫連接,它允許應用程序重復使用一個現(xiàn)有的數(shù)據(jù)庫連接,而再不是重新建立一個;釋放空閑時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏。這項技術能明顯提高對數(shù)據(jù)庫操作的性能。 數(shù)據(jù)庫連接池在初始化時將創(chuàng)建一定數(shù)量的數(shù)據(jù)庫連接放到連接池中,這些數(shù)據(jù)庫連接的數(shù)量是由最小數(shù)據(jù)庫連接數(shù)來設定的。無論這些數(shù)據(jù)庫連接是否被使用,連接池都將一直保證至少擁有這么多的連接數(shù)量。連接池的最大數(shù)據(jù)庫連接數(shù)量限定了這個連接池能占有的最大連接數(shù),當應用程序向連接池請求的連接數(shù)超過最大連接數(shù)量時,這些請求將被加入到等待隊列中。數(shù)據(jù)庫連接池的最小連接數(shù)和最大連接數(shù)的設置要考慮到下列幾個因素: 1) 最小連接數(shù)是連接池一直保持的數(shù)據(jù)庫連接,所以如果應用程序對數(shù)據(jù)庫連接的使用量不大,將會有大量的數(shù)據(jù)庫連接資源被浪費; 2) 最大連接數(shù)是連接池能申請的最大連接數(shù),如果數(shù)據(jù)庫連接請求超過此數(shù),后面的數(shù)據(jù)庫連接請求將被加入到等待隊列中,這會影響之后的數(shù)據(jù)庫操作。 3) 如果最小連接數(shù)與最大連接數(shù)相差太大,那么最先的連接請求將會獲利,之后超過最小連接數(shù)量的連接請求等價于建立一個新的數(shù)據(jù)庫連接。不過,這些大于最小連接數(shù)的數(shù)據(jù)庫連接在使用完不會馬上被釋放,它將被放到連接池中等待重復使用或是空閑超時后被釋放。 配置Tomcat數(shù)據(jù)庫連接池的前提: 1. 必須裝有Java運行環(huán)境; 2. 必須有SQL Server2000數(shù)據(jù)庫服務器(可以不在本地); 3. 必須有jtds.jar,并將它放在$CATALINA_HOME/common/lib目錄下(只能是這里)。使用它是因為Microsoft公司的Java SQL Server驅動程序不支持二次查詢,可到網(wǎng)上搜到。目前使用的是jtds-0.6.jar。 在$CATALINA_HOME/conf/server.xml里設置數(shù)據(jù)庫連接池: 下面是配置的代碼,必須放在<Host>和</Host>之間。 <Context path="/quality" docBase="quality" debug="0" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_quality_log." suffix=".txt" timestamp="true"/> <Resource name="jdbc/connectDB" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/connectDB"> <parameter> <name>maxActive</name> <!-- Maximum number of DB connections in pool.Set to 0 for no limit. --> <value>100</value> </parameter> <parameter> <name>maxIdle</name> <!-- Maximum number of idle DB connections to retain in pool.Set to 0 for no limit. --> <value>30</value> </parameter> <parameter> <name>maxWait</name> <!-- Maximum time to wait for a DB connection to become available in ms.An exception is thrown if this timeout is exceeded.Set to -1 to wait indefinitely. --> <value>10000</value> </parameter> <parameter> <name>removeAbandoned</name> <!-- Abandoned DB connections are removed and recycled --> <value>true</value> </parameter> <parameter> <name>removeAbandonedTimeout</name> <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. --> <value>60</value> </parameter> <parameter> <name>logAbandoned</name> <!-- Log a stack trace of the code which abandoned --> <value>false</value> </parameter> <parameter> <name>factory</name> <!-DBCP Basic Datasource Factory --> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <parameter> <name>username</name> <!-- Database User Name --> <value>Iorishinier</value> </parameter> <parameter> <name>password</name> <!-- User Password --> <value>mypasswd</value> </parameter> <parameter> <name>driverClassName</name> <!-- Database Driver Class Name --> <value>net.sourceforge.jtds.jdbc.Driver</value> </parameter> <parameter> <name>url</name> <!-- Database Address --> <value>jdbc:jtds:sqlserver://127.127.127.127:1433/Northwind</value> </parameter> </ResourceParams> </Context> 下面是一些參數(shù)的說明: <Context path="/quality" docBase="quality" debug="0" reloadable="true" crossContext="true"> 其中: 1) path 指定路徑,這里設定的是$CATALINA_HOME/webapps下的quality目錄; 2) docBase 文件根目錄。 3) reloader 當網(wǎng)頁被更新時是否重新編譯。 4) maxActive 連接池的最大數(shù)據(jù)庫連接數(shù)。設為0表示無限制。 5) maxIdle 數(shù)據(jù)庫連接的最大空閑時間。超過此空閑時間,數(shù)據(jù)庫連接將被標記為不可用,然后被釋放。設為0表示無限制。 6) maxWait 最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。 7) removeAbandoned 回收被遺棄的(一般是忘了釋放的)數(shù)據(jù)庫連接到連接池中。 8) removeAbandonedTimeout 數(shù)據(jù)庫連接過多長時間不用將被視為被遺棄而收回連接池中。 9) logAbandoned 將被遺棄的數(shù)據(jù)庫連接的回收記入日志。 10) driverClassName JDBC驅動程序。 11) url 數(shù)據(jù)庫連接字符串 在$CATALINA_HOME/webapps/quality/WEB-INF/web.xml里設置被引用的資源: 下面是配置代碼,必須放在<web-app>和</web-app>里。 <!-- Database Config start --> <resource-ref> <description>connectDB test</description> <res-ref-name>jdbc/connectDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <!-- Database Config end --> 下面是一下參數(shù)的必要說明: 1) description 對被引用的資源的描述。 2) res-ref-name 資源名稱。見上面的<ResourceParams name="jdbc/connectDB"> 3) res-type 資源類型。見上面的<Resource name="jdbc/connectDB" auth="Container" type="javax.sql.DataSource"/> 在JSP中使用資源: 這是在$CATALINA_HOME/webapps/quality下的某級子目錄里的jsp網(wǎng)頁文件部分代碼: <%@ page contentType="text/html;charset=GBK"%> <%@ page errorPage="error.jsp"%> <%@ page import="javax.naming.*"%> <%@ page import="javax.sql.*"%> <%@ page import="java.sql.*"%> <html> <head> </head> <body> <%
……………… ………………
// 數(shù)據(jù)庫操作 Context ctx=null; Connection cnn=null; Statement stmt=null; ResultSet rs=null; try { ctx=new InitialContext(); if(ctx==null) throw new Exception("沒有匹配的環(huán)境"); DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/connectDB"); if(ds==null) throw new Exception("沒有匹配數(shù)據(jù)庫"); cnn=ds.getConnection(); stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=stmt.executeQuery("select * from table1");
……………… ………………
} finally { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(cnn!=null) cnn.close(); if(ctx!=null) ctx.close(); } %> </body> </html> 代碼說明: DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/connectDB"); 上面這句應用了剛才設的資源。 資源使用完要釋放,尤其是Context資源,見try-catch-finally部分的finally代碼段,這是一種好的習慣。資源釋放時如果釋放了上級資源,下級資源將先被釋放。如:釋放了ctx,那么資源釋放順序將是rs,stmt,cnn,ctx。換句話說,如果釋放了ctx,那么rs,stmt和cnn都將不可用了。 這里的釋放資源只是將數(shù)據(jù)庫連接返回連接池中,并不是把資源真正釋放掉,見數(shù)據(jù)庫連接池概述。
|