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

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

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

            這幾天在弄個小東西,要用到數(shù)據(jù)庫,以前就聽說過數(shù)據(jù)庫連接池這個概念,所以就打算在這個小東西中加入數(shù)據(jù)庫連接池。呵呵。從網(wǎng)上搜了一些資料。今天就整理一下。我搜到的設(shè)置基本上主要有兩種方法我們以MySQL+TOMCAT為例
            1.
            DataSource設(shè)置到我們的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設(shè)置到我們的Tomcat中,下面詳細的介紹下(測試用的JAVA代碼和上面的一樣就不帖出了):
            這里我查到的設(shè)置方法就有了一點區(qū)別了。有的人把DataSource設(shè)置在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文件中各個標(biāo)簽的屬性及其代表的意思。有興趣的也可以自己先查下。:-)

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

            人人狠狠综合久久亚洲| 国产亚洲美女精品久久久久狼| 久久精品国产黑森林| 久久天天躁狠狠躁夜夜不卡| 国产精品99久久久精品无码| 亚洲精品乱码久久久久久蜜桃| 久久91精品国产91| 久久精品国产精品青草| 久久无码国产| 久久久婷婷五月亚洲97号色| 久久九九精品99国产精品| 精品久久久久久无码免费| 97精品伊人久久大香线蕉| 97热久久免费频精品99| 久久九九久精品国产免费直播| 久久久久人妻一区二区三区 | 久久青青草原精品国产| 久久精品国产亚洲沈樵| 2021国产精品久久精品| 国产精自产拍久久久久久蜜| 久久久一本精品99久久精品88| 亚洲国产精品婷婷久久| 久久综合九色综合网站| 欧美伊人久久大香线蕉综合| 2021国产成人精品久久| 69国产成人综合久久精品| 久久精品国产亚洲av麻豆蜜芽| 久久精品中文字幕有码| 激情综合色综合久久综合| 婷婷综合久久中文字幕蜜桃三电影| 91精品国产高清91久久久久久| 午夜精品久久久久久99热| 久久久久99精品成人片| 精品久久久无码中文字幕| 久久最新精品国产| 国产精品美女久久久久| 久久综合伊人77777麻豆| 久久99国产一区二区三区| 国产精品狼人久久久久影院| 国产精品va久久久久久久| 精品久久久久久中文字幕人妻最新|