Configure the oracle database connection pool in tomcat 6.0

Source: Internet
Author: User

 

The configuration of tomcat 5.x and tomcat 6.x connection pool is different. Here, the configuration process of tomcat 6.0 is recorded.

 

Container: tomcat 6.0

 

Database: oracle 10g

 

Procedure:

 

1,

 

Put the driver package ojdbc6.jar of the oracle database to tomcat 6.0/lib. Otherwise, java will be reported during running. lang. classNotFoundException is unusual. if you Add the driver package to java Build Path> Libraries> Add JARs, an error is reported. You must Add the driver package to tomcat/lib;

 

2,

 

Add database connection information to tomcat configuration file (context. xml)

 

<Resource name = "jdbc/zhx" auth = "Container" type = "javax. SQL. DataSource"

 

DriverClassName = "oracle. jdbc. driver. OracleDriver"

 

Url = "jdbc: oracle: thin: @ 192.168.20.90: 1521: carddb"

 

Username = "card_settle"

 

Password = "card_settle"

 

MaxActive = "20"

 

MaxIdle = "10"

 

MaxWait = "10000" type = "codeph" text = "/codeph"/>

 

 

 

Attribute description:

 

Name, data source name, usually in the format of "jdbc/xxx"

 

Auth, which does not need to be modified. The default value is used.

 

Type, value: "javax. SQL. DataSource", using the data source

 

DriverClassName, database driver

 

Url, the path to access the database. carddb is the SID of the oracle database.

 

MaxActive: the maximum number of database connections in the connection pool. If it is set to 0, there is no limit.

 

MaxIdle indicates the maximum idle time of the database connection. If the idle time is exceeded, the database connection is marked as unavailable and then released. If it is set to 0, there is no limit.

 

MaxWait: The maximum waiting time for establishing a connection. If this time is exceeded, an exception is thrown. If it is set to-1, it indicates no limit.

 

 

 

3. Write the following code for the host that requires database connection in the program:

 

 

 

Java code

Import javax. naming. Context;

Import javax. naming. InitialContext;

Import javax. naming. NamingException;

Import javax. SQL. DataSource;

 

Public class DataSourceTest {

Public static Connection getCon (){

Connection conn = null;

Try {

Context ctx = new InitialContext ();

DataSource ds = (DataSource) ctx. lookup ("java:/comp/env/jdbc/zhx ");

Conn = ds. getConnection ();

} Catch (NamingException e ){

E. printStackTrace ();

} Catch (SQLException e ){

E. printStackTrace ();

}

 

Return conn;

}

}

 

Import javax. naming. Context;

Import javax. naming. InitialContext;

Import javax. naming. NamingException;

Import javax. SQL. DataSource;

 

Public class DataSourceTest {

Public static Connection getCon (){

Connection conn = null;

Try {

Context ctx = new InitialContext ();

DataSource ds = (DataSource) ctx. lookup ("java:/comp/env/jdbc/zhx ");

Conn = ds. getConnection ();

} Catch (NamingException e ){

E. printStackTrace ();

} Catch (SQLException e ){

E. printStackTrace ();

}

 

Return conn;

}

}

 

After use, remember to call the close () method. instead of actually closing the connection, it is released to let it return to the connection pool.

 

 

 

Appendix:

 

Connection Pool operation principle:

 

In actual application development, especially in WEB application systems, If JSP, Servlet, or EJB uses JDBC to directly access data in the database, every data access request must go through the steps of establishing a database connection, opening a database, accessing data, and closing a database connection. Connecting to and opening a database is a resource-consuming and time-consuming task, if such database operations occur frequently, the system performance will inevitably drop sharply, and even cause the system to crash. Database connection pool technology is the most commonly used method to solve this problem. In many application servers (such as Weblogic, WebSphere, and JBoss), this technology is basically provided, you do not need to program it yourself, but it is necessary to gain an in-depth understanding of this technology.

 

The idea of database connection pool technology is very simple. database connections are stored as objects in a Vector object. Once a database connection is established, different database access requests can share these connections, by reusing these established database connections, you can overcome these shortcomings and greatly save system resources and time.

 

The main operations of the database connection pool are as follows:

 

(1) Create a database connection pool object (server startup ).

 

(2) create an initial number of database connections (idle connections) based on the specified parameters ).

 

(3) For a database access request, a connection is directly obtained from the connection pool. If there is no idle connection in the database connection pool object and the number of connections does not reach the maximum (that is, the maximum number of active connections), create a new database connection. (4) access the database.

 

(5) Close the database and release all database connections (close the database connection at this time, rather than actually close it, but put it into the idle queue. If the actual number of idle connections is greater than the initial number of idle connections, the connection is released ).

 

(6) release the database connection pool object (during server stop and maintenance, release the database connection pool object and release all connections ).

Related Article

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.