tomcat 中設置連接池


[html] view plain copy
 print?
  1. <pre class="html" name="code">1.將數據庫驅動程序的JAR文件放在Tomcat的 common/lib 中;   
  2. </pre><pre class="html" name="code">2.在server.xml中設置數據源,以MySQL數據庫爲例,如下:   
  3. <GlobalNamingResources> </GlobalNamingResources>節點中加入,   
  4. </pre><pre class="html" name="code"><Resource   
  5. name="jdbc/DBPool"   
  6. type="javax.sql.DataSource"   
  7. password="root"   
  8. driverClassName="com.mysql.jdbc.Driver"   
  9. maxIdle="2"   
  10. maxWait="5000"   
  11. username="root"   
  12. url="jdbc:mysql://127.0.0.1:3306/test"   
  13. maxActive="4"/>   
  14. </pre><pre class="html" name="code">屬性說明:name,數據源名稱,通常取”jdbc/XXX”的格式;   
  15.         type,”javax.sql.DataSource”;   
  16.        password,數據庫用戶密碼;   
  17.         driveClassName,數據庫驅動;   
  18.         maxIdle,最大空閒數,數據庫連接的最大空閒時間。超過空閒時間,數據庫連接將被標記爲不可用,然後被釋放。設爲0表示無限制。   
  19.         MaxActive,連接池的最大數據庫連接數。設爲0表示無限制。   
  20.         maxWait ,最大建立連接等待時間。如果超過此時間將接到異常。設爲-1表示無限制。</pre><pre class="html" name="code">   
  21. 3.在你的web應用程序的web.xml中設置數據源參考,如下:   
  22. <web-app></web-app>節點中加入,   
  23. </pre><pre class="html" name="code"><resource-ref>   
  24. <description>MySQL DB Connection Pool</description>   
  25. <res-ref-name>jdbc/DBPool</res-ref-name>   
  26. <res-type>javax.sql.DataSource</res-type>   
  27. <res-auth>Container</res-auth>   
  28. <res-sharing-scope>Shareable</res-sharing-scope>   
  29. </resource-ref>   
  30. </pre><pre class="html" name="code">       子節點說明: </pre><pre class="html" name="code">       description,描述信息;   
  31.         res-ref-name,參考數據源名字,同上一步的屬性name;   
  32.         res-type,資源類型,”javax.sql.DataSource”;   
  33.         res-auth,”Container”;   
  34.         res-sharing-scope,”Shareable”;   
  35. </pre><pre class="html" name="code">4.在web應用程序的context.xml中設置數據源鏈接,如下:   
  36. <Context></Context>節點中加入,   
  37. </pre><pre class="html" name="code"><ResourceLink   
  38. name="jdbc/DBPool"   
  39. type="javax.sql.DataSource"   
  40. global="jdbc/DBPool"/>   
  41. </pre><pre class="html" name="code">屬性說明:</pre><pre class="html" name="code">        name,同第2步和第3步的屬性name值,和子節點res-ref-name值;   
  42.          type,同樣取”javax.sql.DataSource”;   
  43.          global,同name值。   
  44.   
  45. 至此,設置完成,下面是如何使用數據庫連接池。   
  46. </pre><pre class="html" name="code">1.建立一個連接池類,DBPool.java,用來創建連接池,代碼如下:   
  47. <pre class="java" name="code">import javax.naming.Context;   
  48. import javax.naming.InitialContext;   
  49. import javax.naming.NamingException;   
  50. import javax.sql.DataSource;   
  51.   
  52. public class DBPool {   
  53. private static DataSource pool;   
  54.  static {   
  55.     Context env = null;   
  56.   try {   
  57.     env = (Context) new InitialContext().lookup("java:comp/env");   
  58.     pool = (DataSource)env.lookup("jdbc/DBPool");   
  59.     if(pool==null)   
  60.     System.err.println("'DBPool' is an unknown DataSource");   
  61.   } catch(NamingException ne) {   
  62.       ne.printStackTrace();   
  63.   }   
  64.  }   
  65.  public static DataSource getPool() {   
  66.    return pool;   
  67.  }   
  68. </pre><br>  
  69. <pre></pre>  
  70. <pre class="html" name="code">2.在要用到數據庫操作的類或jsp頁面中,用DBPool.getPool().getConnection(),獲得一個Connection對象,就可以進行數據庫操作,最後別忘了對Connection對象調用close()方法,注意:這裏不會關閉這個Connection,而是將這個Connection放回數據庫連接池。  
  71.   
  72. </pre><br>  
  73. <pre></pre>  
  74. <pre></pre>  
  75.      
  76. </pre>  

發佈了23 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章