Tomcat 5.5 database Connection pool configuration _tomcat

Source: Internet
Author: User
Tags auth
Server.xml's modifications I recommend using Tomcat's admin component to complete, fully graphical configuration, fairly easy. or according to the following writing in < globalnamingresources></globalnamingresources> between the following configuration (for reference only, when you use, please change to their own database configuration):
Copy Code code as follows:

<resource
Name= "Jdbc/mysql"
Type= "Javax.sql.DataSource"
password= "Hu Jintao"
Driverclassname= "Org.gjt.mm.mysql.Driver"
Maxidle= "2"
maxwait= "5000"
Username= "Root"
Url= "Jdbc:mysql://127.0.0.1/test"
Maxactive= "4"/>

Then, create an XML file with the same name as your Web site folder in the/conf/catalina/localhost folder under the Tomcat installation directory. For example: Testsourse.xml. The contents are as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<Context>
  
<resource
Name= "Jdbc/mysql"
Type= "Javax.sql.DataSource"
password= "Hu Jintao"
Driverclassname= "Org.gjt.mm.mysql.Driver"
Maxidle= "2"
maxwait= "5000"
Username= "Root"
Url= "Jdbc:mysql://127.0.0.1/test"
Maxactive= "4"/>
  
</Context>

The fact is to repeat the contents of the Server.xml. This step is very important, if you do not have this step will be wrong, will appear org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot create JDBC driver of class ' for connect URL ' null ' ERROR.
Finally, in the/web-inf/web.xml file of your own website, add the Code (Reference):
Copy Code code as follows:

<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>

Ok. The configuration is all complete. Now you can write code to test this database connection pool. Such as:
Copy Code code as follows:

DataSource ds = null;
InitialContext CTX = new InitialContext ();
ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/mysql");
Connection conn = Ds.getconnection ();

The database connection object was obtained.

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.