浏览 17325 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-22
这几天在弄个小东西,要用到数据库,以前就听说过数据库连接池这个概念,所以就打算在这个小东西中加入数据库连接池。呵呵。从网上搜了一些资料。今天就整理一下。我搜到的设置基本上主要有两种方法我们以MySQL+TOMCAT为例
1.把DataSource设置到我们的WEB项目中,下面详细的介绍下: 第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml <?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哈哈~省事了) <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对象了 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设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了 先说下在server.xml添加DataSource 第一步:在Tomcat的conf中的server.xml文件中找到 <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>修改为 <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中加入 <ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/> 第三步:就是在WEB项目的WEB-INF中的web.xml添加 <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中加入 <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添加 <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文件中各个标签的属性及其代表的意思。有兴趣的也可以自己先查下。:-) <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 会话连接工厂</td> <td>java:comp/env/url</td> <td>URL 连接工厂</td> <td>java:comp/env/jms</td> <td>JMS 连接工厂和目标</td> <td>java:comp/ORB</td> <td>应用程序组件之间共享的 ORB 实例</td> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-09-24
早期习惯用JNDI,现在习惯用c3p0的连接池和配置文件。。或者spring里配置数据库那也集成c3p0
|
|
返回顶楼 | |
发表时间:2008-09-25
使用c3p0应该很方便.
|
|
返回顶楼 | |
发表时间:2008-09-25
不错,虽然c3p0很方便,但用这个可以很清楚底层的东西
|
|
返回顶楼 | |
发表时间:2008-09-27
新手的话还是学习下,比较好。毕竟框架的那些东西封装了好多操作。呵呵,我是这么看的(因为我是个新手)
|
|
返回顶楼 | |
发表时间:2008-10-09
tsliyangyang 写道 新手的话还是学习下,比较好。毕竟框架的那些东西封装了好多操作。呵呵,我是这么看的(因为我是个新手)
尽量做到知其所以然啊,该了解的,还是可以了解一下啊 感觉这样比较好啊 |
|
返回顶楼 | |
发表时间:2009-03-17
SPRING作者不是也推荐在容器里配置的么.
|
|
返回顶楼 | |