• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

connection pooling

Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how will we create connection pooling manually?
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the name indicates, Connection pooling is just a way to efficiently handle your database connection(JDBC connection) by sharing it for different clients. So when your server starts or when you make the first call to a db, you can make a pool of prefixed number of database connections and whenever you need a connection instead of creating a connection it picks the connection from the collection where you have stored your connection. Once you are done with your connection, you return it back to the pool.

What I said is the minimum requirement. May be the best starting point is understand how the various existing connection pools behave. Then it's just matter of programming.
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some steps/points to get a web application up and running in tomcat
5.0 that uses DBCP Connection pooling and JNDI lookup for locating
the data source that is configured in the pool.

Tomcat 5.0 (DBCP JAR included)
MySQL Database (I have used for this example)
MySQL Driver (e.g. mysql-connector-java-3.2.0-bin.jar)

Source Code (I used servlet in this example)

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
<display-name>DBCP Test</display-name>
<description>Testing Connection Pooling and JNDI</description>
<!-- I dont find any relevance for this code...
thats why i commented out this.
<description>DB Connection</description>

package jean;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

public class TestServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response) {
Connection con = null;

try {
PrintWriter out = response.getWriter();
out.print("<BR><BR><b>DBCP with JNDI based
Connection</b><BR>Sample Rows...") ;
Context init = new InitialContext();
Context c = (Context) init.lookup("java:comp/env");
DataSource ds = (DataSource) c.lookup
synchronized (ds) {
conn = ds.getConnection();
ps = conn.prepareStatement("Select * from a");
ResultSet rs=null;
rs = ps.executeQuery();
while (rs.next()) {
out.println("<BR>" + rs.getString(2));
} catch (Exception e) {

There are more that one way we can configure the web application
context in the tomcat. In this example I just make a jeantest.xml
and copied to the '<TOMCAT_HOME>/conf/Catalina/localhost' folder.

In this file you can see that I am using empty string in the parth
attribute of the 'Context' tag. This will make this web application
which resides in the folder 'D:/jeandbcptest' to be mounted as the
root context with the tomcat. If we do like this we can access the
application using the url 'http://localhost:8080/' (Default ROOT
will not be seen in the root context). If you want to change it to
any sub context you can give any name in that path attribute (say
<Context path="TestDBCP" ...). Then you can use access your
application using the URL 'http://localhost:8080/TestDBCP/'.

In this file I have created a connection pool with the JNDI
name 'jdbc/MySQLDBPool' using 'Resource' tag and set the parameters
with the 'ResourceParams' tag.

<Context path="" docBase="D:/jeandbcptest" debug="0"
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
<Resource name="jdbc/MySQLDBPool"
<ResourceParams name="jdbc/MySQLDBPool">

That's all... Start the tomcat and Give the URL

If you want to add this context definition in the server.xml place
it between <HOST> and </HOST> tag.

Note that JNDI pool is binded to the context name that we are giving
in the 'path' attribute. For example we have put this application in
the '<TOMCAT_HOME>/webapp/testinwebapp' folder, and we just update
the 'docBase' attribute of 'Context' with this path... We can access
this same application with these two URL's. But we cannot use the
JNDI name with the 2nd URL.
1. http://localhost:8080/TestDBCPServlet
2. http://localhost:8080/testinwebapp/TestDBCPServlet

Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice Explanation
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to obtain and return connections from/to a pool, a connexion pool should have capabilities like 'Monitoring Timed out connections'.
This is an 'Event driven' paradim, where a pool thread will notify the timedout connection and there by returning that connection.
Other features are monitoring stale connections, where a pool thread takes the unused conenctions in the pool and fire some sample SQLs in regular intervals, there by making sure that the connection is a live connection before giving it to a request.
I'm doing laundry! Look how clean this tiny ad is:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic