tomcat6.x+struts1.x+mysql5.x connection Pooling problem

Source: Internet
Author: User
Tags connection pooling tomcat

1. Build context.xml files under the Webroot/meta-inf of the project, note that it must be in this directory, Tomcat will automatically find this file, Tomcat6.0 will not be configured in Web.xml later:

<?xml version= "1.0" encoding= "UTF-8"?> <context reloadable=
"true" crosscontext= "true" >

< Resource 
name= "jdbc/test"----------Note: test for the project name
auth= "Container"----------the item is immutable
type= " Javax.sql.DataSource "----------This item is invariant
driverclassname=" com.mysql.jdbc.Driver "----------database driver
url=" Jdbc:mysql://localhost:3306/haotian?autoreconnect=true "-------url
username=" root-----username
password= " Root "------Password
maxactive=" "------Maximum number of connections 
maxidle=" 5 "--------Maximum number of idle connections
maxwait="-1 "/>------max Wait Number of seconds,-1 for infinite wait

</Context>

2. Connection class:

public class DbConnection { 
private Connection conn=null;
Public Connection getconnection () {
//Generates a context object through which it can send aliases to the container.
Context context;
try {Context
= new InitialContext ();
Find Object
DataSource ds= (DataSource) context.lookup ("Java:comp/env/jdbc/test");//jdbc/test to name
//Get in config file     Connect
try {
conn=ds.getconnection ();
catch (SQLException e) {
e.printstacktrace ();
The
catch (Namingexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
return
conn;
}        

3. Place the MySQL5.0 drive pack under Tomcat's Lib directory, note: Tomcat Lib, not project Lib. (Do not know why this, before the use of this connection pool in the project's Lib can be connected to success, but now can not, personally think it may be context.xml make Tomcat find the driver package in their lib, not the driver package in the project)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.