亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)(待續(xù))

系統(tǒng) 1955 0

這幾天在弄個小東西,要用到數(shù)據(jù)庫,以前就聽說過數(shù)據(jù)庫連接池這個概念,所以就打算在這個小東西中加入數(shù)據(jù)庫連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例
1.把DataSource設(shè)置到我們的WEB項目中,下面詳細(xì)的介紹下:
第一步:在我們的WEB項目中的 META-INF 文件夾下建立一個 context.xml

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. <? xml ? version = '1.0' ? encoding = 'utf-8' ?> ??
  2. ??
  3. < Context > ??
  4. ??
  5. ???? < Resource ? name = "jdbc/mysql" ??? ??
  6. ??????? auth = "Container" ??? ??
  7. ??????? type = "javax.sql.DataSource" ??? ??
  8. ??????? driverClassName = "com.mysql.jdbc.Driver" ??? ??
  9. ??????? url = "jdbc:mysql://localhost/bbs" ??? ??
  10. ??????? username = "root" ??? ??
  11. ??????? password = "root" ??? ??
  12. ??????? maxActive = "50" ??? ??
  13. ??????? maxIdle = "20" ??? ??
  14. ??????? maxWait = "10000" ? /> ??? ??
  15. ??
  16. </ Context > ??
    <?xml version='1.0' encoding='utf-8'?>



<Context>



    <Resource name="jdbc/mysql"   

       auth="Container"   

       type="javax.sql.DataSource"   

       driverClassName="com.mysql.jdbc.Driver"   

       url="jdbc:mysql://localhost/bbs"   

       username="root"   

       password="root"   

       maxActive="50"   

       maxIdle="20"   

       maxWait="10000" />   



</Context>


  


第二步:在我們的WEB項目下的 WEB-INF 文件夾下建立一個 web.xml (如果存在了就不用了,直接修改就行了)
(這幾天測試了一下,不做這步也可以,O(∩_∩)O哈哈~省事了)

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < resource-ref > ??
  2. ???? < description > DB?Connection </ description > ??
  3. ???? < res-ref-name > jdbc/mysql </ res-ref-name > ??
  4. ???? < res-type > javax.sql.DataSource </ res-type > ??
  5. ???? < res-auth > Container </ res-auth > ??
  6. </ resource-ref > ??
      <resource-ref>

      <description>DB Connection</description>

      <res-ref-name>jdbc/mysql</res-ref-name>

      <res-type>javax.sql.DataSource</res-type>

      <res-auth>Container</res-auth>

  </resource-ref>


  


第三步:我們就可以用代碼來獲取 Connection 對象了

Java代碼 復(fù)制代碼 ? 收藏代碼
  1. package ?xushun.util; ??
  2. ??
  3. import ?java.sql.*; ??
  4. import ?javax.sql.*; ??
  5. import ?javax.naming.*; ??
  6. ??
  7. public ? class ?DBHelper?{ ??
  8. ???? ??
  9. ???? public ? static ?Connection?getConnection()? throws ?SQLException,NamingException ??
  10. ????{ ??
  11. ???????? //?初始化查找命名空間 ??
  12. ????????Context?initContext?=? new ?InitialContext(); ??
  13. ????????Context?envContext?=?(Context)initContext.lookup( "java:/comp/env" ); ??
  14. ???????? //?找到DataSource ??
  15. ????????DataSource?ds?=?(DataSource)envContext.lookup( "jdbc/mysql" ); ??
  16. ???????? return ?ds.getConnection(); ??
  17. ????} ??
  18. }??
    package xushun.util;



import java.sql.*;

import javax.sql.*;

import javax.naming.*;



public class DBHelper {

    

    public static Connection getConnection() throws SQLException,NamingException

    {

        // 初始化查找命名空間

        Context initContext = new InitialContext();

        Context envContext = (Context)initContext.lookup("java:/comp/env");

        // 找到DataSource

        DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");

        return ds.getConnection();

    }

}


  


2.把DataSource設(shè)置到我們的Tomcat中,下面詳細(xì)的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):
這里我查到的設(shè)置方法就有了一點區(qū)別了。有的人把DataSource設(shè)置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了
先說下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < GlobalNamingResources > ??
  2. ??<!--?Editable?user?database?that?can?also?be?used?by ??
  3. ???????UserDatabaseRealm?to?authenticate?users ??
  4. ??-- > ??
  5. ?? < Resource ? name = "UserDatabase" ? auth = "Container" ??
  6. ???????????? type = "org.apache.catalina.UserDatabase" ??
  7. ???????????? description = "User?database?that?can?be?updated?and?saved" ??
  8. ???????????? factory = "org.apache.catalina.users.MemoryUserDatabaseFactory" ??
  9. ???????????? pathname = "conf/tomcat-users.xml" ? /> ??
  10. </ GlobalNamingResources > ??
      <GlobalNamingResources>

    <!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    -->

    <Resource name="UserDatabase" auth="Container"

              type="org.apache.catalina.UserDatabase"

              description="User database that can be updated and saved"

              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

              pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>
  

修改為

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < GlobalNamingResources > ??
  2. ??<!--?Editable?user?database?that?can?also?be?used?by ??
  3. ???????UserDatabaseRealm?to?authenticate?users ??
  4. ??-- > ??
  5. ?? < Resource ? name = "UserDatabase" ? auth = "Container" ??
  6. ???????????? type = "org.apache.catalina.UserDatabase" ??
  7. ???????????? description = "User?database?that?can?be?updated?and?saved" ??
  8. ???????????? factory = "org.apache.catalina.users.MemoryUserDatabaseFactory" ??
  9. ???????????? pathname = "conf/tomcat-users.xml" ? /> ??
  10. ?? < Resource ? name = "jdbc/bbs" ????? ??
  11. ????????? auth = "Container" ? type = "javax.sql.DataSource" ??
  12. ????????? driverClassName = "com.mysql.jdbc.Driver" ??
  13. ????????? maxIdle = "20" ??
  14. ????????? maxWait = "5000" ??
  15. ????????? username = "root" ??
  16. ????????? password = "admin" ??
  17. ????????? url = "jdbc:mysql://localhost:3306/bbs" ????? ??
  18. ????????? maxActive = "100" ? ??
  19. ????????? removeAbandoned = "true" ??
  20. ????????? removeAbandonedTimeout = "60" ??
  21. ????????? logAbandoned = "true" /> ??
  22. </ GlobalNamingResources > ??
      <GlobalNamingResources>

    <!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    -->

    <Resource name="UserDatabase" auth="Container"

              type="org.apache.catalina.UserDatabase"

              description="User database that can be updated and saved"

              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

              pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/bbs"     

	          auth="Container" type="javax.sql.DataSource"

	          driverClassName="com.mysql.jdbc.Driver"

	          maxIdle="20"

	          maxWait="5000"

	          username="root"

	          password="admin"

	          url="jdbc:mysql://localhost:3306/bbs"     

	          maxActive="100" 

	          removeAbandoned="true"

	          removeAbandonedTimeout="60"

	          logAbandoned="true"/>

  </GlobalNamingResources>
  


第二步:在Tomcat的conf文件夾下的context.xml中加入

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < ResourceLink ? name = "jdbc/bbs" ? global = "jdbc/bbs" ? type = "javax.sql.DataSource" /> ??
    <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
  


第三步:就是在WEB項目的WEB-INF中的web.xml添加

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < resource-ref > ??
  2. ???? < description > DB?Connection </ description > ??
  3. ???? < res-ref-name > jdbc/mysql </ res-ref-name > ??
  4. ???? < res-type > javax.sql.DataSource </ res-type > ??
  5. ???? < res-auth > Container </ res-auth > ??
  6. </ resource-ref > ??
      <resource-ref>

      <description>DB Connection</description>

      <res-ref-name>jdbc/mysql</res-ref-name>

      <res-type>javax.sql.DataSource</res-type>

      <res-auth>Container</res-auth>

  </resource-ref>


  


還有就是在Tomcat文檔中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夾下的context.xml中加入

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < Resource ? name = "jdbc/bbs" ????? ??
  2. ?????????????? auth = "Container" ? type = "javax.sql.DataSource" ??
  3. ?????????????? driverClassName = "com.mysql.jdbc.Driver" ??
  4. ?????????????? maxIdle = "20" ??
  5. ?????????????? maxWait = "5000" ??
  6. ?????????????? username = "root" ??
  7. ?????????????? password = "admin" ??
  8. ?????????????? url = "jdbc:mysql://localhost:3306/bbs" ????? ??
  9. ?????????????? maxActive = "100" ? ??
  10. ?????????????? removeAbandoned = "true" ??
  11. ?????????????? removeAbandonedTimeout = "60" ??
  12. ?????????????? logAbandoned = "true" /> ??
    <Resource name="jdbc/bbs"     

	          auth="Container" type="javax.sql.DataSource"

	          driverClassName="com.mysql.jdbc.Driver"

	          maxIdle="20"

	          maxWait="5000"

	          username="root"

	          password="admin"

	          url="jdbc:mysql://localhost:3306/bbs"     

	          maxActive="100" 

	          removeAbandoned="true"

	          removeAbandonedTimeout="60"

	          logAbandoned="true"/>
  

然后就是在WEB項目的WEB-INF中的web.xml添加

Xml代碼 復(fù)制代碼 ? 收藏代碼
  1. < resource-ref > ??
  2. ???? < description > DB?Connection </ description > ??
  3. ???? < res-ref-name > jdbc/mysql </ res-ref-name > ??
  4. ???? < res-type > javax.sql.DataSource </ res-type > ??
  5. ???? < res-auth > Container </ res-auth > ??
  6. </ resource-ref > ??
      <resource-ref>

      <description>DB Connection</description>

      <res-ref-name>jdbc/mysql</res-ref-name>

      <res-type>javax.sql.DataSource</res-type>

      <res-auth>Container</res-auth>

  </resource-ref>


  


就是這些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下資料整理出上面用到的XML文件中各個標(biāo)簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)

<td>JNDI 查找名稱</td>?????? <td>關(guān)聯(lián)的引用</td>

<td>java:comp/env</td>????? <td>應(yīng)用程序環(huán)境條目</td>

<td>java:comp/env/jdbc</td> <td>JDBC 數(shù)據(jù)源資源管理器連接工廠</td>

<td>java:comp/env/ejb</td>? <td>EJB 引用</td>

<td>java:comp/UserTransaction</td><td>UserTransaction 引用</td>

<td>java:comp/env/mail</td> <td>JavaMail 會話連接工廠</td>

<td>java:comp/env/url</td>? <td>URL 連接工廠</td>

<td>java:comp/env/jms</td>? <td>JMS 連接工廠和目標(biāo)</td>

<td>java:comp/ORB</td>????? <td>應(yīng)用程序組件之間共享的 ORB 實例</td>

TOMCAT數(shù)據(jù)庫連接池的配置方法總結(jié)(待續(xù))


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 热99精品只有里视频最新 | 久久综合网久久综合 | 精品毛片 | 色婷婷综合久久久 | 男女车车好快的车车免费网站 | 98色花堂永久地址国产精品 | 高清不卡在线 | 国产精品久久久久激情影院 | 五月亭亭激情五月 | 国产亚洲美女 | 99影视在线视频免费观看 | 人人爱人人草 | 久草丁香 | 香蕉视频18| 国产成人高清亚洲一区久久 | 久久99精品国产自在现线小黄鸭 | 在线日韩欧美 | 一级毛片免费网站 | 欧美一区二区在线观看免费网站 | 99久久免费费视频在线观看 | 成人国产片免费 | 日本高清免费毛片久久看 | 亚洲精品国产一区二区三区四区 | 够爱久久 | 国产一国产一级毛片视频在线 | 色婷婷免费视频 | 日韩欧美一级毛片视频免费 | ww亚洲ww亚在线观看 | 最新九九精品 | 色偷偷亚洲女人天堂观看欧 | 亚洲欧美在线免费 | 天天综合天天操 | 亚洲精品第四页中文字幕 | 男女免费在线视频 | 亚洲黄色免费在线观看 | 久久精品国产999久久久 | 四虎国产精品免费久久麻豆 | 欧美性一区二区三区五区 | 久久99热这里只有精品 | 亚洲国产精品视频在线观看 | 国内精品视频免费观看 |