SoFunction
Updated on 2025-04-05

Tomcat 5.5 Database Connection Pool Configuration

I recommend using Tomcat's Admin component to complete the modification, which is completely graphical and quite easy. Or write the following configuration between <GlobalNamingResources></GlobalNamingResources> according to the following writing method (for reference only. When you use it, please change it to your own database configuration):
Copy the codeThe code is as follows:

 <Resource
name="jdbc/mysql"
type=""
password="******"
driverClassName=""
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 website folder in the /conf/Catalina/localhost folder in the Tomcat installation directory. For example:. The content is as follows:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Context>

<Resource
name="jdbc/mysql"
type=""
password="******"
driverClassName=""
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1/test"
maxActive="4" />

</Context>

In fact, it’s just repeating the content. This step is very important. If this step is not available, an error will occur. The following error will appear: Cannot create JDBC driver of class '' for connect URL 'null' error.
Finally, add the code (reference) to the /WEB-INF/ file of your own website:
Copy the codeThe code is as follows:

 <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type></res-type>
<res-auth>Container</res-auth>
</resource-ref>

OK. All configurations are complete. Now you can write code to test this database connection pool. like:
Copy the codeThe code is as follows:

DataSource ds = null;
InitialContext ctx = new InitialContext();
ds = (DataSource) ("java:comp/env/jdbc/mysql");
Connection conn = ();

The database connection object is obtained.