• <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 肥仔 閱讀(1378) 評論(0)  編輯 收藏 引用 所屬分類: Web-后臺

            亚洲精品乱码久久久久久蜜桃图片 | 久久毛片一区二区| 欧美粉嫩小泬久久久久久久| 亚州日韩精品专区久久久| 综合久久精品色| 国产精品久久影院| 久久久久亚洲av毛片大| 欧美一区二区三区久久综合| 99久久超碰中文字幕伊人| 久久亚洲精品国产亚洲老地址 | 青草影院天堂男人久久| 国产精品久久久久久久久久影院| 人妻丰满AV无码久久不卡| 国产亚州精品女人久久久久久| 97精品国产97久久久久久免费| 狠狠色婷婷综合天天久久丁香| 亚州日韩精品专区久久久| 久久久久久久亚洲Av无码| 狠狠色狠狠色综合久久 | 97精品依人久久久大香线蕉97| 久久国产精品波多野结衣AV| 亚洲综合久久综合激情久久| 国产亚洲欧美成人久久片| 99re久久精品国产首页2020| 无码久久精品国产亚洲Av影片| 久久久久亚洲av综合波多野结衣| 久久综合九色综合网站| 中文字幕精品无码久久久久久3D日动漫 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 99999久久久久久亚洲| 99久久综合狠狠综合久久止| 久久久综合九色合综国产| 久久狠狠一本精品综合网| 久久久久久久波多野结衣高潮| 99久久精品国产一区二区 | www亚洲欲色成人久久精品| 亚洲精品无码成人片久久| 精品久久久无码21p发布| 欧美黑人激情性久久| 久久久久亚洲AV综合波多野结衣 | 色综合久久久久久久久五月|