Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:22:06+00:00 2026-06-12T11:22:06+00:00

I am facing a problem in closing database conenctions. I am getting the below

  • 0

I am facing a problem in closing database conenctions. I am getting the below error

==================================================================================

267137 [http-8080-Processor23] FATAL com.abc.r2.dwrclasses.DBUtilities  - Failed to get a connection from DataSource JNDI/TPXSRUDB
org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
    at com.abc.r2.dwrclasses.DBUtilities.getNativeConnection(DBUtilities.java:110)
    at com.abc.r2.dwrclasses.ProcedureCaller.callProcedure(ProcedureCaller.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.directwebremoting.impl.DefaultRemoter$1.doFilter(DefaultRemoter.java:740)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:744)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:593)
    at org.directwebremoting.dwrp.BaseCallHandler.handle(BaseCallHandler.java:90)
    at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:120)
    at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:141)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at com.oracle.determinations.web.platform.util.CharsetFilter.doFilter(CharsetFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
    ... 42 more

===================================================================================

I have written a DBUtilities.java file which gets the connection using JNDI and returns the connection object. This connection object is then used in other java classes for quering database (calling procedures) and then closed when done. However, after some amount of successful connection, its giving the above error. The code written in my class is given below. Can anyone please let me know whats wrong in the code or is there a problem with the database. I am using oracle 10g.

=========================================================================================

package com.abc.r2.dwrclasses;


/*
 * Utilites.java
 *
 * Created on 08 July 2004, 11:55
 */



import java.sql.*;

import javax.sql.*;
import javax.naming.*;

import java.util.*;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.commons.dbutils.DbUtils;

import com.ibm.ws.rsadapter.jdbc.WSJdbcConnection;
import com.ibm.ws.rsadapter.jdbc.WSJdbcUtil;


/**
 * Abstract class containing useful database methods.
 * @author Rajan Panchal
 */
public abstract class DBUtilities {

    private static final Logger log = Logger.getLogger(DBUtilities.class);

    private static Context initCtx; // Initial context
    private static Context envCtx;  // Environment context
    static ResourceBundle rb;
    static String appServer;
    DataSource ds;
    // Only get the JNDI context once, it's expensive.
    /* Get the JNDI context depending upon the configuration stored in appconfiguration.properties file in configuration folder */
    static {
        try {
                rb = ResourceBundle.getBundle("configuration.queries");
                appServer = rb.getString("APP_SERVER");
                log.debug("Application Server: "+appServer);
            if(appServer.equalsIgnoreCase("TOMCAT")){        
                initCtx = new InitialContext();        
                envCtx  = (Context)initCtx.lookup("java:/comp/env");
            }else if(appServer.equalsIgnoreCase("WEBSPHERE")){
                Hashtable env = new Hashtable();
                env.put("java.naming.factory.initial","com.ibm.websphere.naming.WsnInitialContextFactory");
                System.out.println("INITIAL_CONTEXT_FACTORY: com.ibm.websphere.naming.WsnInitialContextFactory");
                initCtx = new InitialContext(env);
            }
        } catch (javax.naming.NamingException e) {
                log.fatal("Failed to get JNDI context on "+appServer, e);
                throw new RuntimeException(e);
        }
    }

    /**
     *  Get a JDBC connection
     * @param jndiName the JNDI name of the datasource
     * @return a Connection to the datasource
     */
    public static Connection getConnection(String jndiName) {
        int maxtry = 10;
        int DBConnectTry = 0;
        while( DBConnectTry++ < maxtry){
            log.info("trying to connect:"+DBConnectTry);
        try {
            if(appServer.equalsIgnoreCase("TOMCAT")){               
                DataSource ds = (DataSource)envCtx.lookup(jndiName);
                log.debug("Returning connection object for "+appServer);
            return ds.getConnection();
            }else if(appServer.equalsIgnoreCase("WEBSPHERE")){
                DataSource ds = (DataSource)initCtx.lookup(jndiName);
                log.debug("Returning connection object for "+appServer);
                return ds.getConnection();
            }
        } catch (javax.naming.NamingException e) {
            log.fatal("JNDI lookup  failed for DataSource "+jndiName, e);
            throw new RuntimeException(e);
        } catch (SQLException e) {
            log.fatal("Failed to get a connection from DataSource "+jndiName, e);
            log.info("Wait for 1 sec...");
             Long WaitTime = System.currentTimeMillis()+1000L;
             while(System.currentTimeMillis()<WaitTime){

           }
            log.info("Wait Over retrying...");            
        }
        }
        return null;

    }
/* This method returns the native connection for executing the pl/sql  procedure with array descriptors.*/    
    public static Connection getNativeConnection(String jndiName) {
        int maxtry = 10;
        int DBConnectTry = 0;
        Connection conn;
        while( DBConnectTry++ < maxtry){
            log.info("trying to connect:"+DBConnectTry);
        try {
                if(appServer.equalsIgnoreCase("TOMCAT")){
                    DataSource ds = (DataSource)envCtx.lookup(jndiName);
                    conn = ds.getConnection();
                    if (conn instanceof org.apache.commons.dbcp.DelegatingConnection)      { 
                        log.debug("detected apache commons dbcp datasource");          
                        conn = ((org.apache.commons.dbcp.DelegatingConnection) conn).getInnermostDelegate();      
                    }
                    return conn;
                }else if(appServer.equalsIgnoreCase("WEBSPHERE")){
                    DataSource ds = (DataSource)initCtx.lookup(jndiName);
                    conn = (Connection) WSJdbcUtil.getNativeConnection((WSJdbcConnection)ds.getConnection());                   
                    return conn;
                }
        } catch (javax.naming.NamingException e) {
            log.fatal("JNDI lookup  failed for DataSource "+jndiName, e);
            throw new RuntimeException(e);
        } catch (SQLException e) {
            log.fatal("Failed to get a connection from DataSource "+jndiName, e);
            log.info("Wait for 1 sec...");
             Long WaitTime = System.currentTimeMillis()+1000L;
             while(System.currentTimeMillis()<WaitTime){

           }
            log.info("Wait Over retrying...");            
        }
        }
        return null;

    }
    /**
     * Utility method to close a connection, and/or a statement, and/or a resultset
     * @param con Connection, or null
     * @param stmt Statement, or null
     * @param rs ResultSet, or null
     */
    public static void cleanUp(Connection con, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                while (rs.next()) // Drain the resultset.
                    ;
                rs.close();
            }

            if (stmt != null)
                stmt.close();

            if (con != null)
                con.close();
        } catch (SQLException e) {
            log.error("Failed to close connection", e);
        }
    }

    /**
     * Convert a ResultSet to a List of Maps
     * This enables the results to be processed
     * using the JSTL forEach tag.
     * @param rs the ResultSet to convert
     * @throws SQLException may be thrown by ResultSet operations
     * @return the converted ResultSet
     */
    @SuppressWarnings("unchecked")
    public static List convertResultSet(ResultSet rs) throws SQLException {
        try {
            NDC.push("convertResultSet");
            log.debug("start");

            // Get column names
            ResultSetMetaData rsmd = rs.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();
            log.debug("Column Count is "+numberOfColumns);
            String[] names = new String[numberOfColumns];
            int[] types = new int[numberOfColumns];
            for (int i = 0; i < names.length; ++i) {
                names[i] = rsmd.getColumnName(i+1);
                types[i] = rsmd.getColumnType(i+1);
                log.debug("  Name="+names[i]);
            }

            List l = new LinkedList();

            // Loop through the rows
            while (rs.next()) {
                log.debug("Processing row "+l.size());
                Map m = new HashMap();

                // Loop through the columns
                for (int i = 0; i < names.length; ++i) {
                    Object o;
                    switch(types[i]) {
                        case Types.DATE:
                            o = rs.getTimestamp(i+1);
                            break;
                        default:
                            o = rs.getString(i+1);
                    }
                    log.debug("  "+names[i]+"='"+o+"'");
                    /*
                    if (o == null)
                        o = "&nbsp;";
                    */
                    m.put(names[i], o);
                }

                // Add the map to the list
                l.add(m);
                log.debug("done");
            }

            return l;
        } finally {
            NDC.pop();
        }
    }

    @SuppressWarnings("unchecked")
    public static void formatDates(List rows, java.text.SimpleDateFormat fmt) {
        Iterator it1 = rows.iterator();
        while (it1.hasNext()) {
            Map m = (Map)it1.next();
            List names = new LinkedList();
            Iterator it2 = m.entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry e = (Map.Entry)it2.next();
                if (e.getValue() instanceof Timestamp) {
                    names.add(e.getKey());
                }
            }

            it2 = names.iterator();
            while(it2.hasNext()) {
                String name = (String)it2.next();
                Timestamp t = (Timestamp) m.get(name);
                String s = fmt.format(t);
                m.put(name,s);
            }

        }

    }

    /**
     * Generate dummy results as a List of Maps
     *
     * @param columns A string array containing the required columns names
     * @param rows    The number of rows to generate
     * @return The data as a List of Maps
     */
    @SuppressWarnings("unchecked")
    public static List generateTestData(String[] columns, int rows) {

        try {
            NDC.push("generateTestData");

            List l = new LinkedList();

            for (int row = 1; row <= rows; ++row) {
                Map m = new HashMap();

                // Loop through the columns
                for (int i = 0; i < columns.length; ++i) {
                    String s = columns[i]+" "+row;
                    m.put(columns[i], s);
                }

                // Add the map to the list
                l.add(m);
            }

            return l;
        } finally {
            NDC.pop();
        }
    }


    /**
     * retrieves just the error code from an oracle message
     */
    public static String parseOracleError(String errCode, String errMsg) {

        int start = 0;
        int end = 0;
        start = errMsg.indexOf(errCode);
        end = errMsg.indexOf(":", start);
        return errMsg.substring(start, end);
    }

     /**Close a <code>Connection</code>, <code>Statement</code> and
     * <code>ResultSet</code> cleanly.
     */
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        try {
            DbUtils.close(rs);
            DbUtils.close(stmt);
            DbUtils.close(conn);
        } catch (SQLException sqlEx) {
            log.error("Failed to close database objects cleanly.", sqlEx);
        }
    }

    /**Close a <code>Connection</code> and <code>Statement</code> cleanly.
     */
    public static void close(Connection conn, Statement stmt) {
        try {
            DbUtils.close(stmt);
            DbUtils.close(conn);
        } catch (SQLException sqlEx) {
            log.error("Failed to close database objects cleanly.", sqlEx);
        }
    }

    /**Close a <code>Connection</code> and <code>Statement</code> quietly.
     */
    public static void closeQuietly(Connection conn, Statement stmt) {
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(conn);
    }

    /**Close a <code>Connection</code>, <code>Statement</code> and 
     * a <code>ResultSet</code> quietly.
     */
    public static void closeQuietly(Connection conn, Statement stmt, ResultSet rst) {
        DbUtils.closeQuietly(rst);
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(conn);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:22:07+00:00Added an answer on June 12, 2026 at 11:22 am

    Your cleanUp method could fail to close a given SQLConnection if it tries to also close a ResultSet and/or Statement beforehand. e.g. if closing the ResultSet fails and throws a SQLException, it would jump directly to the catch block, without even trying to close the connection. Are there errors on closing a connection in your log?

    To avoid that problem, either provide dedicated cleanUp methods that only attempt to close a single thing or wrap each attempt to close sth. inside its own try/catch block. The same advice applies to the various close and closeQuietly methods.

    // edit

    This code section is most likely harmful. In getNativeConnection(String jndiName) :

    DataSource ds = (DataSource)envCtx.lookup(jndiName);
    conn = ds.getConnection();
    if (conn instanceof org.apache.commons.dbcp.DelegatingConnection) { 
       log.debug("detected apache commons dbcp datasource");          
       conn = ((org.apache.commons.dbcp.DelegatingConnection)conn).getInnermostDelegate();      
    }
    

    The DelegatingConnection seems to be a proxy that handles the pooling of the connection. As you are unwrapping the “native” connection the DelegatingConnection-Proxy is not notified when the connection is closed and cannot return it to the pool. That way the pool fills up with closed connections until the limit is reached.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing problem while implementing https with webHttpBinding. I am getting below error
I am facing problem while setting focus in EdiText. Below is my EditText property.
I am facing some problem with MySQL database. actually It was fast when I
I am facing problem in getting Facebook data I am using Facebook SDK for
I have facing problem in listview i am getting data from server and after
I am facing problem with hibernate annotations.For the code shown below I have a
I am facing problem in creating more than one tables in sqllite database in
I am facing problem in accessing database file when I am running it on
I am facing problem when getting LIKES of Photos. In result i m getting
I am facing problem on a centOS server while sending java mails. Getting the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.