JDBC Connection Pool

Source: Internet
Author: User
Tags connection pooling set time stmt zip client oracle database
Introduction to Connection Database connection pool
In a traditional two-tier structure, the client program opens the database connection at startup and closes the database connection when exiting the program. In this way, each client always occupies a database connection throughout the program, even when there is a large amount of idle time without a database operation, such as when the user enters data, resulting in inefficient use of the database connection.
In three-tier structure mode, the database connection is managed through the connection pool of the middle tier. Only when the user really needs a database operation does the middle tier request a connection from the connection pool, the database operation completes, and the connection is immediately released into the connection pool for use by other users. In this way, not only greatly improve the efficiency of database connection, so that a large number of users can share fewer database connections, and eliminates the time to establish the connection.

Connection Pool Configuration Usage
Database connection pooling is a basic function of the application server, and we take Apusic application server as an example to illustrate the configuration usage of the JDBC Connection pool.

The Apusic JDBC Connection pool provides support for a variety of databases, such as Oracle, MS SQL Server, Sybase, Informix, DB2, and so on.

The Apusic JDBC Connection pool can be connected to a database through the JDBC driver of the database itself, or through the Jdbc-odbc bridge. Let's take Oracle as an example to illustrate how to configure a connection pool:

The JDBC driver package file for the Oracle database Classes111.zip in/usr/oracle/jdbc/lib (assuming Oracle's installation directory is/usr/oracle) directory, First, the Classes111.zip is added to the classpath of the system. Then set the following in apusic/config/apusic.conf (assuming the installation directory is Apusic):

<service
Class= "Com.apusic.jdbc.PoolManager"
Name= "Jdbcpool:name=jdbc/sample"
>
<attribute name= "expirationtime" value= "/>"
<attribute name= "Mincapacity" value= "5"/>
<attribute name= "URL" value= "Jdbc:oracle:thin:@192.168.19.136:1521:orcl"/>
<attribute name= "connectionproperties" value= "user=gtj,password=abc123"/>
<attribute name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/>
<attribute name= "maxcapacity" value= "/>"
</SERVICE>

Expirationtime: Timeout time, in seconds. When a database connection exceeds the Expirationtime set time is not used
, the system automatically closes the database connection. Default value is 300 seconds
Mincapacity: Minimum number of connections
URL: URL of the database
ConnectionProperties: Connection properties, where: User username, password password
DRIVERCLASSNAME:JDBC Driver class Name
Maxcapacity: Maximum number of connections
The IP address of the computer on which the 192.168.19.136:oracle resides.
 
Calling connection pooling
Let's take a JSP program as an example of how to use connection pooling. First we get datasource through Jndi, then we get the connection connection, as shown in the following example:




<title>jsp sample</title>


<body>

<p>

<%@ page contenttype= "text/html;charset=gb2312"%>

<%@ page import= "

Java.sql.*,

javax.naming.*,

Javax.sql.*

"%>

<%

try{

Context ctx = new InitialContext ();

DataSource ds = (DataSource) ctx.lookup ("Jdbc/sample");

Connection con = ds.getconnection ();

Statement stmt = Con.createstatement ();

ResultSet rs = stmt.executequery ("Select ename from EMP");

while (Rs.next ()) {

Out.println ("<p>" + rs.getstring (1));

}

Rs.close ();

Stmt.close ();

}catch (Exception e) {

System.out.println ("JSP:" + e.getmessage ());

}finally{

try{

Con.close ();

}catch (Exception E1) {}

}

%>

</body>



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.