You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Arjen van der Weijden <Ar...@rivm.nl> on 2005/04/08 16:08:07 UTC

help on DBCP

Hi folks,

Just started out examining DBCP, so I'm completely new to the subject.
I adapted the example given by Dirk V. (given below). I put some stuff in a
for loop.

The PROBLEM is that after a few loops the program seems to hang (less than
10 connections).

Can anybody help me on this, it must be someting trivial I guess?

Configuration standard mysql installation on a redhat
_____________________________________________________________________________________________
public class DataSourceExample {

    public static void main(String[] args) {
      try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
      catch (Exception ex) {
            ex.printStackTrace();
        }

        DataSource dataSource =
setupDataSource("jdbc:mysql://localhost/mysql?user=mysql&password=pizza");

        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;

        for (int ii = 0; ii < 3; ii++) {
        try {
            for (int j = 0; j < 3; j++) {
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            String $query = "SELECT * FROM user";
            rset = stmt.executeQuery($query);
            System.out.println("Results:");
            int numcols = rset.getMetaData().getColumnCount();
            while(rset.next()) {
                for(int i=1;i<=numcols;i++) {
                    System.out.print("\t" + rset.getString(i));
                }
                System.out.println("");
            }
            }
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            try { rset.close(); } catch(Exception e) { }
            try { stmt.close(); } catch(Exception e) { }
            try { conn.close(); } catch(Exception e) { }
        }
        }
    }

    public static DataSource setupDataSource(String connectURI) {
            ConnectionFactory connectionFactory = new
DriverManagerConnectionFactory(connectURI,null);
            PoolableConnectionFactory poolableConnectionFactory = new \

PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
            PoolingDataSource dataSource = new
PoolingDataSource(connectionPool);
            return dataSource;
    }
}
____________________________________________________________________________

 DISCLAIMER:  http://www.rivm.nl/disclaimer


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: help on DBCP

Posted by anshul khare <an...@tavant.com>.
Hey,

I guess the problem lies with the order in which you close the ResultSet,
Statement and Connection.

The "Statement"should be closed before the "ResultSet" followed  by
"Connection". I had a similar problem and this fix worked for me.

Anshul

----- Original Message ----- 
From: "Arjen van der Weijden" <Ar...@rivm.nl>
To: <co...@jakarta.apache.org>
Sent: Friday, April 08, 2005 7:08 AM
Subject: help on DBCP


> Hi folks,
>
> Just started out examining DBCP, so I'm completely new to the subject.
> I adapted the example given by Dirk V. (given below). I put some stuff in
a
> for loop.
>
> The PROBLEM is that after a few loops the program seems to hang (less than
> 10 connections).
>
> Can anybody help me on this, it must be someting trivial I guess?
>
> Configuration standard mysql installation on a redhat
>
____________________________________________________________________________
_________________
> public class DataSourceExample {
>
>     public static void main(String[] args) {
>       try {
>             Class.forName("com.mysql.jdbc.Driver").newInstance();
>         }
>       catch (Exception ex) {
>             ex.printStackTrace();
>         }
>
>         DataSource dataSource =
> setupDataSource("jdbc:mysql://localhost/mysql?user=mysql&password=pizza");
>
>         Connection conn = null;
>         Statement stmt = null;
>         ResultSet rset = null;
>
>         for (int ii = 0; ii < 3; ii++) {
>         try {
>             for (int j = 0; j < 3; j++) {
>             conn = dataSource.getConnection();
>             stmt = conn.createStatement();
>             String $query = "SELECT * FROM user";
>             rset = stmt.executeQuery($query);
>             System.out.println("Results:");
>             int numcols = rset.getMetaData().getColumnCount();
>             while(rset.next()) {
>                 for(int i=1;i<=numcols;i++) {
>                     System.out.print("\t" + rset.getString(i));
>                 }
>                 System.out.println("");
>             }
>             }
>         } catch(SQLException e) {
>             e.printStackTrace();
>         } finally {
>             try { rset.close(); } catch(Exception e) { }
>             try { stmt.close(); } catch(Exception e) { }
>             try { conn.close(); } catch(Exception e) { }
>         }
>         }
>     }
>
>     public static DataSource setupDataSource(String connectURI) {
>             ConnectionFactory connectionFactory = new
> DriverManagerConnectionFactory(connectURI,null);
>             PoolableConnectionFactory poolableConnectionFactory = new \
>
>
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,t
rue);
>             PoolingDataSource dataSource = new
> PoolingDataSource(connectionPool);
>             return dataSource;
>     }
> }
>
____________________________________________________________________________
>
>  DISCLAIMER:  http://www.rivm.nl/disclaimer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: help on DBCP

Posted by Dirk Verbeeck <di...@pandora.be>.
It is indeed a trivial problem.
The close() methods aren't called inside your inner for loop.
So for each 3 connections created in the "for j" loop only 1 is closed.

The order of closing rset,stmt,conn is correct.

-- Dirk

Arjen van der Weijden wrote:

> Hi folks,
> 
> Just started out examining DBCP, so I'm completely new to the subject.
> I adapted the example given by Dirk V. (given below). I put some stuff in a
> for loop.
> 
> The PROBLEM is that after a few loops the program seems to hang (less than
> 10 connections).
> 
> Can anybody help me on this, it must be someting trivial I guess?
> 
> Configuration standard mysql installation on a redhat
> _____________________________________________________________________________________________
> public class DataSourceExample {
> 
>     public static void main(String[] args) {
>       try {
>             Class.forName("com.mysql.jdbc.Driver").newInstance();
>         }
>       catch (Exception ex) {
>             ex.printStackTrace();
>         }
> 
>         DataSource dataSource =
> setupDataSource("jdbc:mysql://localhost/mysql?user=mysql&password=pizza");
> 
>         Connection conn = null;
>         Statement stmt = null;
>         ResultSet rset = null;
> 
>         for (int ii = 0; ii < 3; ii++) {
>         try {
>             for (int j = 0; j < 3; j++) {
>             conn = dataSource.getConnection();
>             stmt = conn.createStatement();
>             String $query = "SELECT * FROM user";
>             rset = stmt.executeQuery($query);
>             System.out.println("Results:");
>             int numcols = rset.getMetaData().getColumnCount();
>             while(rset.next()) {
>                 for(int i=1;i<=numcols;i++) {
>                     System.out.print("\t" + rset.getString(i));
>                 }
>                 System.out.println("");
>             }
>             }
>         } catch(SQLException e) {
>             e.printStackTrace();
>         } finally {
>             try { rset.close(); } catch(Exception e) { }
>             try { stmt.close(); } catch(Exception e) { }
>             try { conn.close(); } catch(Exception e) { }
>         }
>         }
>     }
> 
>     public static DataSource setupDataSource(String connectURI) {
>             ConnectionFactory connectionFactory = new
> DriverManagerConnectionFactory(connectURI,null);
>             PoolableConnectionFactory poolableConnectionFactory = new \
> 
> PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
>             PoolingDataSource dataSource = new
> PoolingDataSource(connectionPool);
>             return dataSource;
>     }
> }




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org