Tomcat 5.5 database connection pool Configuration

Source: Internet
Author: User

Server. xml modification I recommend that you use the admin component of Tomcat to complete the configuration in full graphical mode, which is quite easy. Alternatively, write the following configuration between <globalnamingresources> </globalnamingresources> (for your reference only, change it to your own database configuration ): CopyCode The Code is as follows: <Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "******"
Driverclassname = "org. gjt. Mm. MySQL. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // 127.0.0.1/test"
Maxactive = "4"/>

Create an XML file with the same name as your website folder in the/CONF/Catalina/localhost folder under the tomcat installation directory. For example, testsourse. xml. The content is as follows: copy the Code the code is as follows:

name = "JDBC/MySQL"
type = "javax. SQL. datasource "
Password =" ******* "
driverclassname =" org. gjt. mm. mySQL. driver "
maxidle =" 2 "
maxwait =" 5000 "
username =" root "
url =" JDBC: mysql: // 127.0.0.1/test "
maxactive =" 4 "/>

In fact, it is to repeat the content of server. xml. This step is very important. If this step does not exist, an error will occur, and org. apache. tomcat. DBCP. DBCP. sqlnestedexception: cannot create JDBC driver of class ''for connect URL 'null' error.
Finally, in your own website's/WEB-INF/Web. xml file, add the code (refer ):Copy codeThe Code is 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. All configurations are complete. Now you can write code to test the database connection pool. For example:Copy codeThe Code is 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 is 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.