Use with Connection Pooling

From Documentation

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.

Use with Connection Pooling

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them. Instead of closing a connection immediately, it keeps them in a pool such that the next connect request could be served very efficiently. Connection pooling, in addition, has a lot of benefits, such as control resource usage.

There is no reason not to use connection pooling when developing Web-based applications, including ZK applications.

The concept of using connection pooling is simple: configure, connect and close. The way to connect and close a connection is very similar the ad-hoc approach, while configuration depends on what Web server and database server are in use.

Connect and Close a Connection

After configuring connection pooling (which will be discussed in the following section), you could use JNDI to retrieve an connection as follows.

 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;

 import javax.naming.InitialContext;
 import javax.sql.DataSource;

 import org.zkoss.zul.Window;

 public class MyWindows extends Window {
     private Textbox name, email;
     public void onCreate() {
         //initial name and email
         name = getFellow("name");
         email = getFellow("email");
     public void onOK() throws Exception {
             DataSource ds = (DataSource)new InitialContext()
             //Assumes your database is configured and
             //named as "java:comp/env/jdbc/MyDB"

         Connection conn = null;
         Statement stmt = null;
         try {
             conn = ds.getConnection();
             stmt = conn.prepareStatement("INSERT INTO user values(?, ?)");

             //insert what end user entered into database table
             stmt.setString(1, name.value);
             stmt.setString(2, email.value);

             //execute the statement
             stmt.close(); stmt = null;
                 //optional because the finally clause will close it
                 //However, it is a good habit to close it as soon as done, especially 
                 //you might have to create a lot of statement to complete a job
         } finally { //cleanup
             if (stmt != null) {
                 try {
                 } catch (SQLException ex) {
                     //(optional log and) ignore
             if (conn != null) {
                 try {
                 } catch (SQLException ex) {
                     //(optional log and) ignore


  • It is important to close the statement and connection after use.
  • You could access multiple database at the same time by use of multiple connections. Depending on the configuration and J2EE/Web servers, these connections could even form a distributed transaction.

Configure Connection Pooling

The configuration of connection pooling varies from one J2EE/Web/Database server to another. Here we illustrated some of them. You have to consult the document of the server you are using.

Tomcat 5.5 + MySQL

To configure connection pooling for Tomcat 5.5, you have to edit $TOMCAT_DIR/conf/context.xml[1], and add the following content under the <Context> element. The information that depends on your installation and usually need to be changed is marked in the blue color.

 <!-- The name you used above, must match _exactly_ here!
     The connection pool will be bound into JNDI with the name
 <Resource name="jdbc/MyDB" username="someuser" password="somepass" 
     auth="Container" defaultAutoCommit="false" 
     driverClassName="com.mysql.jdbc.Driver" maxActive="20" 
     type="javax.sql.DataSource" />

Then, in web.xml, you have to add the following content under the <web-app> element as follows.



JBoss + MySQL

The following instructions is based on section of the reference manual of MySQL 5.0.

To configure connection pooling for JBoss, you have to add a new file to the directory called deploy ($JBOSS_DIR/server/default/deploy). The file name must end with "-ds.xml", which tells JBoss to deploy this file as JDBC Datasource. The file must have the following contents. The information that depends on your installation and usually need to be changed is marked in the blue color.

         <!-- This connection pool will be bound into JNDI with the name
              "java:/MyDB" -->


         <!-- Don't set this any higher than max_connections on your
          MySQL server, usually this should be a 10 or a few 10's
          of connections, not hundreds or thousands -->


         <!-- Don't allow connections to hang out idle too long,
          never longer than what wait_timeout is set to on the
          server...A few minutes is usually okay here,
          it depends on your application
          and how much spikey load it will see -->


         <!-- If you're using Connector/J 3.1.8 or newer, you can use
              our implementation of these to increase the robustness
              of the connection pool. -->



JBoss + PostgreSQL

     <!-- This connection pool will be bound into JNDI with the name
          "java:/MyDB" -->

     <!-- jdbc:postgresql://[servername]:[port]/[database name] -->


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.