tomcat 中设置连接池

本文详细介绍如何在Tomcat服务器中配置MySQL数据库连接池,包括放置JAR文件、设置server.xml和web.xml中的数据源参数,以及在应用程序中获取和使用连接。


[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>  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值