Use Tomcat's own connection pool

Source: Internet
Author: User
Tags auth connection pooling

The database is used for Web site development. A simple database connection will reconnect to the database each time, resulting in slower access, which the connection pool is designed to address, and Tomcat himself provides. OK, let's see how we can use the connection pooling mechanism provided by Tomcat to make a database connection. (add: The tomcat version you use is 5.0.x or higher, and you still need to configure the use of connection pooling in Server.xml) here we take oracle9i as an example:

1. Just like any other database connection, you need to copy the Oracle driver to Tomcat's Common/lib directory.

2. To modify your application profile under Tomcat, this file is placed under Tomcat's Conf/catalina/localhost directory, and the file name of our example is Bulletinsystem.xml, which reads as follows:

-----------------------------------------------
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<context docbase= "E:/jbuilderproject/eshop/bulletinsystem" path= "/bulletinsystem" reloadable= "true" workDir= "E" :/jbuilderproject/eshop/tomcat/work/bulletinsystem ">

<resource name= "Jdbc/eshop/bulletinsystem" auth= "Container" type= "Javax.sql.DataSource"/>
<resourceparams name= "Jdbc/eshop/bulletinsystem" >
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@192.168.13.4:1521:hong</value>
</parameter>
<parameter>
<name>username</name>
<value>eshop</value>
</parameter>
<parameter>
<name>password</name>
<value>eshop</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>select * from dual</value>
</parameter>
</ResourceParams>

</Context>
---------------------------------------------
The Resource Name property defines the data source name (the green word) and is used when configuring its parameters and getting the data connection object, requiring unique. This name usually starts with jdbc/, because the prefix used to find the connection pool data source is JAVA:COMP/ENV/JDBC, see 4.
Parameters inside the resource element:
#factory: Defines a data source factory for generating and managing connection pool objects, which are provided by Tomcat.
#driverclassname: Defines the name of the driver. For different types of databases and different types of drivers, this value is different. The Oracle database is used in this example.
#url: The URL string to use when establishing a data connection. Different databases have different connection methods, and the value is different. In this parameter, set the IP and database service name of the database you want to access.
#username: The user name to access the database.
#password: The password to access the data.
#maxwait: The time to set up a database connection is waiting. Throws an exception when the timeout is exceeded.
#maxactive: Maximum number of connections at the same time. This takes into account the load capacity of the machine.
#maxidle: Maximum number of idle connections. The maximum number of connections that need to be maintained for idle time.
#validationquery: This defines a query statement that can be executed. The primary effect is that the connection pool is used to detect whether a connection is valid, because TCP/IP connections are not always maintained. The specified validationquery must be very simple and efficient to perform; In this case, a virtual object dual of Orale is used, which does not exist in other databases.

3. Modifying the Web.xml of your application is the application of the Web-inf/web.xml, add the code below, add in </web-app> before, (^_^, practice shows that the front is configured, do not add this configuration also connect pool can be used)

--------------------------------------------
<resource-ref>
<description>oracle Datasource for Edushop</description>
<res-ref-name>jdbc/eshop/bulletinSystem</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
--------------------------------------------

4. In your application, use the following code to get the database connection:

--------------------------------------------
Import java.sql.*;
Import javax.naming.*;
Import javax.sql.*;

Context Initctx = new InitialContext ();
Context envctx= (Initctx.lookup) ("java:comp/env");
DataSource ds = (DataSource) envctx.lookup ("Jdbc/eshop/bulletinsystem");
Connection conn=ds.getconnection ();
--------------------------------------------
You can also use it directly:
DataSource ds = (DataSource) initctx.lookup ("Java:comp/env/jdbc/eshop/bulletinsystem");


4. So much. Remember to release after using a database connection. Write more simple, have time to supplement, ^_^

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.