Tomcat配置數據庫連接池
本例為myeclipse+tomcat7.0+sqlserver2008配置數據庫連接池 具體步驟: 在<GlobalNamingResources> </GlobalNamingResources>節點中加入,<Resourcename="jdbc/DBPool"type="javax.sql.DataSource"password="aaaaaa"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"maxIdle="2"maxWait="5000"username="sa"url="jdbc:sqlserver://localhost:1433;DataBaseName=COFFEE"maxActive="4"/>
屬性說明:name,數據源名稱,通常取”jdbc/XXX”的格式; type,”javax.sql.DataSource”; password,數據庫用戶密碼; driveClassName,數據庫驅動; maxIdle,最大空閑數,數據庫連接的最大空閑時間。超過空閑時間,數據庫連接將被標記為不可用,然后被釋放。設為0表示無限制。 MaxActive,連接池的最大數據庫連接數。設為0表示無限制。 maxWait ,最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。 2、在你的web應用程序的web.xml中設置數據源參考,如下: 在<web-app></web-app>節點中加入,<resource-ref><description>DB Connection Pool</description><res-ref-name>jdbc/DBPool</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth><res-sharing-scope>Shareable</res-sharing-scope></resource-ref>
子節點說明: description,描述信息; res-ref-name,參考數據源名字,同上一步的屬性name; res-type,資源類型,”javax.sql.DataSource”; res-auth,”Container”; res-sharing-scope,”Shareable”;3、在tomcat目錄下的context.xml中設置數據源鏈接,如下: 在<Context></Context>中加入:<ResourceLinkname="jdbc/DBPool"type="javax.sql.DataSource"global="jdbc/DBPool"/>
屬性說明:name,同第2步和第3步的屬性name值,和子節點res-ref-name值; type,同樣取”javax.sql.DataSource”; global,同name值。 4、測試:import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;public class Test {private static DataSource pool;public static void main(String[] args) {Context env = null;try {env = (Context) new InitialContext().lookup("java:comp/env");pool = (DataSource)env.lookup("jdbc/DBPool");if(pool==null)System.err.println("'DBPool' is an unknown DataSource");} catch(NamingException ne) {ne.printStackTrace();}Connection conn;try {conn = pool.getConnection();String sql = "select * from allbook";PreparedStatement ps;ps = conn.prepareStatement(sql);ResultSet rs=ps.executeQuery();while(rs.next()){System.out.println(rs.getString("BOOKNAME"));}} catch (SQLException e) {e.printStackTrace();}}}
<Resource name="jdbc/DBPool" type="javax.sql.DataSource" password="aaaaaa" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxIdle="2" maxWait="5000" username="sa" url="jdbc:sqlserver://localhost:1433;DataBaseName=COFFEE" maxActive="4"/> |
<resource-ref> <description>DB Connection Pool</description> <res-ref-name>jdbc/DBPool</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> |
<ResourceLink name="jdbc/DBPool" type="javax.sql.DataSource" global="jdbc/DBPool"/> |
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class Test { private static DataSource pool; public static void main(String[] args) { Context env = null; try { env = (Context) new InitialContext().lookup("java:comp/env"); pool = (DataSource)env.lookup("jdbc/DBPool"); if(pool==null) System.err.println("'DBPool' is an unknown DataSource"); } catch(NamingException ne) { ne.printStackTrace(); } Connection conn; try { conn = pool.getConnection(); String sql = "select * from allbook"; PreparedStatement ps; ps = conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); while(rs.next()){ System.out.println(rs.getString("BOOKNAME")); } } catch (SQLException e) { e.printStackTrace(); } } } |
posted on 2014-05-19 10:16 順其自然EVO 閱讀(286) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄