Tomcat7配置数据库连接池
在glassfish配置了数据库连接池,之后又想在tomcat7下配置mysql的,琢磨了2个小时遇到点挫折总算弄好了经验分享如下:
一、首先配置连接池
1、tomcat下配置连接池在conf/server.xml中的GlobalNamingResources加入一下代码
<Resource name="jdbc/webtest"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="webtest"
password="webtest"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.4.244:3307/webtest"/>
2、在conf/context.xml中的context标签中加入如下
1
<ResourceLink global="jdbc/webtest" name="jdbc/webtest" type="javax.sql.DataSource"/>
3、在项目中的web.xml加入以下配置,否则会找不到配置的连接池
<resource-ref>
<description>DB</description>
<res-ref-name>jdbc/webtest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
到此,连接池配置完成,接着是测试
二、测试配置的数据库连接池
新建一个测试页面index.jsp,写入如下测试代码
1
<%@ page language="java" pageEncoding="gbk"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%
//连接池的获取
Connection conn = null;
DataSource ds = null;
ResultSet rs=null;
Statement stmt = null;
Context initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/webtest");
if(ds!=null){
out.println("DataSource!");
out.println("<br>");
}
%>
如页面没有抛出异常则配置成功
下雨了,别忘了打伞,湿身是小,淋病就麻烦啦*^_^* 不在放荡中变坏,就在沉默中变态! 如果恐龙是人,那人是什么? 此地禁止大小便,违者没收工具。 所有的男人生来平等,结婚的除外。 走过了年少,脚起了水泡
页:
[1]