几天在弄个小东西,要用到数据库,以前就听说过数据库连接池这个概念,所以就打算在这个小东西中加入数据库连接池。呵呵。从网上搜了一些资料。今天就整理一下。我搜到的设置基本上主要有两种方法我们以MySQL+TOMCAT为例
1.把DataSource设置到我们的WEB项目中,下面详细的介绍下:
第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><?xml version='1.0' encoding='utf-8'?>
<!--[if !supportLists]-->2.
<!--[endif]-->
<!--[if !supportLists]-->3.
<!--[endif]--><Context>
<!--[if !supportLists]-->4.
<!--[endif]-->
<!--[if !supportLists]-->5.
<!--[endif]--> <Resource name="jdbc/mysql"
<!--[if !supportLists]-->6.
<!--[endif]--> auth="Container"
<!--[if !supportLists]-->7.
<!--[endif]--> type="javax.sql.DataSource"
<!--[if !supportLists]-->8.
<!--[endif]--> driverClassName="com.mysql.jdbc.Driver"
<!--[if !supportLists]-->9.
<!--[endif]--> url="jdbc:mysql://localhost/bbs"
<!--[if !supportLists]-->10.
<!--[endif]--> username="root"
<!--[if !supportLists]-->11.
<!--[endif]--> password="root"
<!--[if !supportLists]-->12.
<!--[endif]--> maxActive="50"
<!--[if !supportLists]-->13.
<!--[endif]--> maxIdle="20"
<!--[if !supportLists]-->14.
<!--[endif]--> maxWait="10000" />
<!--[if !supportLists]-->15.
<!--[endif]-->
<!--[if !supportLists]-->16.
<!--[endif]--></Context>
第二步:在我们的WEB项目下的WEB-INF文件夹下建立一个web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O哈哈~省事了)
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><resource-ref>
<!--[if !supportLists]-->2.
<!--[endif]--> <description>DB Connection</description>
<!--[if !supportLists]-->3.
<!--[endif]--> <res-ref-name>jdbc/mysql</res-ref-name>
<!--[if !supportLists]-->4.
<!--[endif]--> <res-type>javax.sql.DataSource</res-type>
<!--[if !supportLists]-->5.
<!--[endif]--> <res-auth>Container</res-auth>
<!--[if !supportLists]-->6.
<!--[endif]--></resource-ref>
第三步:我们就可以用代码来获取Connection对象了
Java代码
<!--[if !supportLists]-->1.
<!--[endif]-->package xushun.util;
<!--[if !supportLists]-->2.
<!--[endif]-->
<!--[if !supportLists]-->3.
<!--[endif]-->import java.sql.*;
<!--[if !supportLists]-->4.
<!--[endif]-->import javax.sql.*;
<!--[if !supportLists]-->5.
<!--[endif]-->import javax.naming.*;
<!--[if !supportLists]-->6.
<!--[endif]-->
<!--[if !supportLists]-->7.
<!--[endif]-->public class DBHelper {
<!--[if !supportLists]-->8.
<!--[endif]-->
<!--[if !supportLists]-->9.
<!--[endif]--> public static Connection getConnection() throws SQLException,NamingException
<!--[if !supportLists]-->10.
<!--[endif]--> {
<!--[if !supportLists]-->11.
<!--[endif]--> // 初始化查找命名空间
<!--[if !supportLists]-->12.
<!--[endif]--> Context initContext = new InitialContext();
<!--[if !supportLists]-->13.
<!--[endif]--> Context envContext = (Context)initContext.lookup("java:/comp/env");
<!--[if !supportLists]-->14.
<!--[endif]--> // 找到DataSource
<!--[if !supportLists]-->15.
<!--[endif]--> DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
<!--[if !supportLists]-->16.
<!--[endif]--> return ds.getConnection();
<!--[if !supportLists]-->17.
<!--[endif]--> }
<!--[if !supportLists]-->18.
<!--[endif]-->}
2.把DataSource设置到我们的Tomcat中,下面详细的介绍下(测试用的JAVA代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了
先说下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><GlobalNamingResources>
<!--[if !supportLists]-->2.
<!--[endif]--> <!-- Editable user database that can also be used by
<!--[if !supportLists]-->3.
<!--[endif]--> UserDatabaseRealm to authenticate users
<!--[if !supportLists]-->4.
<!--[endif]--> -->
<!--[if !supportLists]-->5.
<!--[endif]--> <Resource name="UserDatabase" auth="Container"
<!--[if !supportLists]-->6.
<!--[endif]--> type="org.apache.catalina.UserDatabase"
<!--[if !supportLists]-->7.
<!--[endif]--> description="User database that can be updated and saved"
<!--[if !supportLists]-->8.
<!--[endif]--> factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
<!--[if !supportLists]-->9.
<!--[endif]--> pathname="conf/tomcat-users.xml" />
<!--[if !supportLists]-->10.
<!--[endif]--></GlobalNamingResources>
修改为
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><GlobalNamingResources>
<!--[if !supportLists]-->2.
<!--[endif]--> <!-- Editable user database that can also be used by
<!--[if !supportLists]-->3.
<!--[endif]--> UserDatabaseRealm to authenticate users
<!--[if !supportLists]-->4.
<!--[endif]--> -->
<!--[if !supportLists]-->5.
<!--[endif]--> <Resource name="UserDatabase" auth="Container"
<!--[if !supportLists]-->6.
<!--[endif]--> type="org.apache.catalina.UserDatabase"
<!--[if !supportLists]-->7.
<!--[endif]--> description="User database that can be updated and saved"
<!--[if !supportLists]-->8.
<!--[endif]--> factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
<!--[if !supportLists]-->9.
<!--[endif]--> pathname="conf/tomcat-users.xml" />
<!--[if !supportLists]-->10.
<!--[endif]--> <Resource name="jdbc/bbs"
<!--[if !supportLists]-->11.
<!--[endif]--> auth="Container" type="javax.sql.DataSource"
<!--[if !supportLists]-->12.
<!--[endif]--> driverClassName="com.mysql.jdbc.Driver"
<!--[if !supportLists]-->13.
<!--[endif]--> maxIdle="20"
<!--[if !supportLists]-->14.
<!--[endif]--> maxWait="5000"
<!--[if !supportLists]-->15.
<!--[endif]--> username="root"
<!--[if !supportLists]-->16.
<!--[endif]--> password="admin"
<!--[if !supportLists]-->17.
<!--[endif]--> url="jdbc:mysql://localhost:3306/bbs"
<!--[if !supportLists]-->18.
<!--[endif]--> maxActive="100"
<!--[if !supportLists]-->19.
<!--[endif]--> removeAbandoned="true"
<!--[if !supportLists]-->20.
<!--[endif]--> removeAbandonedTimeout="60"
<!--[if !supportLists]-->21.
<!--[endif]--> logAbandoned="true"/>
<!--[if !supportLists]-->22.
<!--[endif]--></GlobalNamingResources>
第二步:在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
第三步:就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><resource-ref>
<!--[if !supportLists]-->2.
<!--[endif]--> <description>DB Connection</description>
<!--[if !supportLists]-->3.
<!--[endif]--> <res-ref-name>jdbc/mysql</res-ref-name>
<!--[if !supportLists]-->4.
<!--[endif]--> <res-type>javax.sql.DataSource</res-type>
<!--[if !supportLists]-->5.
<!--[endif]--> <res-auth>Container</res-auth>
<!--[if !supportLists]-->6.
<!--[endif]--></resource-ref>
还有就是在Tomcat文档中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><Resource name="jdbc/bbs"
<!--[if !supportLists]-->2.
<!--[endif]--> auth="Container" type="javax.sql.DataSource"
<!--[if !supportLists]-->3.
<!--[endif]--> driverClassName="com.mysql.jdbc.Driver"
<!--[if !supportLists]-->4.
<!--[endif]--> maxIdle="20"
<!--[if !supportLists]-->5.
<!--[endif]--> maxWait="5000"
<!--[if !supportLists]-->6.
<!--[endif]--> username="root"
<!--[if !supportLists]-->7.
<!--[endif]--> password="admin"
<!--[if !supportLists]-->8.
<!--[endif]--> url="jdbc:mysql://localhost:3306/bbs"
<!--[if !supportLists]-->9.
<!--[endif]--> maxActive="100"
<!--[if !supportLists]-->10.
<!--[endif]--> removeAbandoned="true"
<!--[if !supportLists]-->11.
<!--[endif]--> removeAbandonedTimeout="60"
<!--[if !supportLists]-->12.
<!--[endif]--> logAbandoned="true"/>
然后就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<!--[if !supportLists]-->1.
<!--[endif]--><resource-ref>
<!--[if !supportLists]-->2.
<!--[endif]--> <description>DB Connection</description>
<!--[if !supportLists]-->3.
<!--[endif]--> <res-ref-name>jdbc/mysql</res-ref-name>
<!--[if !supportLists]-->4.
<!--[endif]--> <res-type>javax.sql.DataSource</res-type>
<!--[if !supportLists]-->5.
<!--[endif]--> <res-auth>Container</res-auth>
<!--[if !supportLists]-->6.
<!--[endif]--></resource-ref>
就是这些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下资料整理出上面用到的XML文件中各个标签的属性及其代表的意思。有兴趣的也可以自己先查下。:-)
<td>JNDI 查找名称</td>
<td>关联的引用</td>
<td>java:comp/env</td>
<td>应用程序环境条目</td>
<td>java:comp/env/jdbc</td> <td>JDBC 数据源资源管理器连接工厂</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 <
分享到:
相关推荐
context.xml, 数据库连接池配置文
Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置...
这个文档详细讲述了mysql数据库连接池的配置以及数据库连接池的工作原理。
Tomcat服务器配置及数据库连接池配置(个人整理版)。纯粹个人整理意见。
详细介绍常用Java各数据库连接池配置,包括C3P0,DBCP,Proxool等。
对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据库资源。 在较为完备的数据库连接池实现中,可根据...
tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上文章&csdn上的代码tomcat数据库连接池配置网上...
比较实用的几种数据库连接池详细配置,值得收藏
达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,
数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中配置方式数据库连接池的集中...
这里是对数据库连接池配置好了的,有是Oracle9i和MySQL的配置,直接可以用不必配置
hibernate配置数据库连接池的三种方法(数据库连接池的概念)
Tomcat6.0+MyEclipse6.0+mysql5.0数据库连接池配置!.pdf
Tomcate服务器下连接数据库连接池的详细配置及全部过程
tomcat配置数据库连接池,本次以MYSQL为例
tomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXTtomcat数据库连接池配置.TXT
Druid为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目。Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能.它可以替换DBCP和C3P0连接池。Druid提供了一个高效、功能强大、可...
数据库连接池配置终极指南,详细介绍了tomcat的部署流程
proxool数据库连接池配置.doc