• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            TOMCAT數據庫連接池的配置方法總結(待續)

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

            Xml代碼 clip_image001

            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代碼 clip_image001

            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代碼 clip_image001

            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.     }  

            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設置到我們的Tomcat中,下面詳細的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):
            這里我查到的設置方法就有了一點區別了。有的人把DataSource設置在Tomcatserver.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就寫在context.xml中了
            先說下在server.xml添加DataSource
            第一步:在Tomcatconf中的server.xml文件中找到

            Xml代碼 clip_image001

            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代碼 clip_image001

            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>


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

            Xml代碼 clip_image001

            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代碼 clip_image001

            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文件了
            Tomcatconf文件夾下的context.xml中加入

            Xml代碼 clip_image001

            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代碼 clip_image001

            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文件中各個標簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)

            posted on 2009-08-03 18:02 肥仔 閱讀(1373) 評論(0)  編輯 收藏 引用 所屬分類: Web-后臺

            亚洲午夜久久久影院| 伊人精品久久久久7777| 久久综合综合久久狠狠狠97色88| 久久九九青青国产精品| 99久久精品免费观看国产| 伊人久久一区二区三区无码| 久久无码人妻一区二区三区午夜| 91精品婷婷国产综合久久| 日产精品久久久久久久| 国产精品嫩草影院久久| 无码人妻精品一区二区三区久久久| 久久高清一级毛片| 国内精品久久久久影院日本| 久久久这里有精品| 岛国搬运www久久| av无码久久久久不卡免费网站| 国内精品久久久久影院老司| 麻豆精品久久久一区二区| 日韩精品无码久久久久久| 亚洲国产精品成人久久蜜臀| 热re99久久精品国产99热| 99久久婷婷国产综合亚洲| 久久人妻无码中文字幕| 久久伊人影视| 久久久久亚洲AV成人网人人网站| 国产V综合V亚洲欧美久久| 久久国产精品成人影院| 亚洲精品无码久久久久| 久久伊人精品一区二区三区| 合区精品久久久中文字幕一区| 久久精品国产清自在天天线| 四虎国产永久免费久久| 国产精品久久久久久影院| 国产三级久久久精品麻豆三级| 国内精品人妻无码久久久影院导航| 亚洲人成无码网站久久99热国产 | 国产精品久久久久久久久久影院 | 无码任你躁久久久久久久| 久久精品亚洲精品国产欧美| 精品乱码久久久久久夜夜嗨| 国产真实乱对白精彩久久|