mysql| Data | database | Database connection with the latest couple of banging, it's been a whole day. Tomcat 5.5 configuration MySQL database connection pool, online experience can not be fully used in the new environment, I write the whole process to facilitate the configuration.
1 Environment Description
JDK 1.5
Tomcat 5.5.4
MySQL 4.0.20
MySQL JDBC 3.0.15
Commons DBCP 1.2.1
2 Preparation work
JDK, Tomcat, MySQL installation process is not my focus, skip.
From http://dev.mysql.com/downloads/ Download the Mysql-connector-java-3.0.15-ga.zip and place the Mysql-connector-java-3.0.15-ga-bin.jar in Jre/lib/ext and Tomcat 5.5/common/ Lib inside.
Download Commons-dbcp-1.2.1.zip from http://jakarta.apache.org/commons/dbcp/and place Commons-dbcp-1.2.1.jar in jre/lib/ Ext and Tomcat 5.5/common/lib inside.
3 Configuring Tomcat
Add in Tomcat 5.5/conf/server.xml <GlobalNamingResources>:
<resource name= "JDBC for MySQL" type= "Javax.sql.DataSource" driverclassname= "Com.mysql.jdbc.Driver" "password=" " Maxidle= "2" maxwait= "5000" username= "root" url= "Jdbc:mysql://localhost/test" maxactive= "4"/>.
Add in Tomcat 5.5/webapps/test/web-inf/web.xml <web-app>:
<resource-ref>
<description>mysql Connection pool</description>
<res-ref-name>jdbc for Mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Add in Tomcat 5.5/webapps/test/meta-inf/context.xml <Context>:
<resourcelink name= "jdbc for MySQL" global= "jdbc for MySQL" type= "Javax.sql.DataSourcer"/>
4 test
Restart the Tomcat server and write a test.jsp:
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/REC-html40/strict.dtd" >
<%@ page import= "java.sql.*"%>
<%@ page import= "javax.sql.*"%>
<%@ page import= "javax.naming.*"%>
<%@ page session= "false"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>test of MySQL Connection pool</title>
<body>
<%
Out.print ("start<br/>");
try{
Context Initctx = new InitialContext ();
Context CTX = (context) initctx.lookup ("java:comp/env");
Object obj = (object) ctx.lookup ("JDBC for MySQL");
Javax.sql.DataSource ds = (javax.sql.DataSource) obj;
Connection conn = Ds.getconnection ();
Out.print ("MySQL Connection pool runs perfectly!");
Conn.close ();
}
catch (Exception ex) {
Out.print (Ex.getmessage ());
Ex.printstacktrace ();
}
%>
</body>
Browsing the JSP file in the browser, it worked. Remember to use close well, or you'll run out of it. This can be done by logging in to MySQL as root, running show processlist command to view all current connections.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.