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 6645853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:20:26+00:00 2026-05-26T00:20:26+00:00

I have a problem with a ConnectionPool that gets exausted very quickly. Basically i

  • 0

I have a problem with a ConnectionPool that gets exausted very quickly.
Basically i have loaded a listener within the application, whenever the context is initialized, a Timer will start, and a timertask within it, will perform an SQL query, which uses a connection pool “the connection pool jar i copied it in Tomcat 6”
The listener “only the relevant method”:

private MyTask task=new MyTask();

public void contextInitialized(ServletContextEvent event) {


this.ctx=event.getServletContext();


Timer timer= new Timer();


Calendar calendar=Calendar.getInstance();


Date firstTime=calendar.getTime();


timer.scheduleAtFixedRate(task, firstTime, 1000*60);


ctx.setAttribute("timer",timer);


}

The connection pool:



public class ConnectionPool {


private static ConnectionPool pool=null;


private static DataSource source=null;


/**

* Private constructor

* Private omdat dit klasse een singleton-patroon volgd, dus nooit meer dan een instantie is toegestaan. 

*/

private ConnectionPool(){


try{


InitialContext ini=new InitialContext();


source=(DataSource)ini.lookup("java:/comp/env/jdbc/verkocht");


}catch(Exception e){


System.out.println("Verkocht db not found");//bedoelt voor test

e.printStackTrace();

}

}


/**

* Singleton getInstance() methode

* @return the connection pool

*/

public static ConnectionPool getInstance(){


if(pool==null){


pool=new ConnectionPool();

}


return pool;

}


/**

* Deze methode returneert een Connection instantie uit het pool

* @return the Connection

*/

public Connection getConnection(){


try{


return source.getConnection();


}catch(SQLException e){


System.out.println("No connection estabilished");

e.printStackTrace();

return null;

}


}

/**

* Sluid deze Connection object

* @param Een connection instantie

*/

public void freeConnection(Connection c){


try{


c.close();

} catch(SQLException e){


e.printStackTrace();

}

}


public static void main(String[] args){


ConnectionPool con=ConnectionPool.getInstance();


con.getConnection();

}


}

Basically I get the null pointer at this line:

return source.getConnection();

Telling me that the database name is not found.

The problems is that it works for the first times, for 5 or 6 minutes (when I set the timer to 1000*60) the queries are sent successfully, so the database is found, meaning that the configuration in context.xml is correct.

The method that is using the connection pool from inside the overridden run method of MyTask, which extends TimerTask is:

    public void assign(int toegewezenAan, double verkoopPrijs, double totaalAfgetrokken, int artikelId){

String sqlUpdate="UPDATE artikelen SET toegewezenAan=?, verkoopPrijs=?, totaalAfgetrokken=?, status=? WHERE artikelId=?";

ConnectionPool pool=ConnectionPool.getInstance();

Connection con=pool.getConnection();

PreparedStatement prep=null;

try{


prep=con.prepareStatement(sqlUpdate);

prep.setInt(1,toegewezenAan);

prep.setDouble(2, verkoopPrijs);

prep.setDouble(3,totaalAfgetrokken);

prep.setString(4, "VERKOCHT");

prep.setInt(5, artikelId);

prep.executeUpdate();


} catch(SQLException e){

e.printStackTrace();

pool.freeConnection(con);

}

} 

Any advice?

  • 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-05-26T00:20:26+00:00Added an answer on May 26, 2026 at 12:20 am

    You only free the connection in the catch-Block, but you actually need to free it everytime.
    You should put it in a finally block, so that the connection is freed (and returned to the pool) after each use.

    ConnectionPool pool=ConnectionPool.getInstance();
    
    Connection con=pool.getConnection();
    
    PreparedStatement prep=null;
    try{
    
      prep=con.prepareStatement(sqlUpdate);
    
      prep.setInt(1,toegewezenAan);
    
      prep.setDouble(2, verkoopPrijs);
    
      prep.setDouble(3,totaalAfgetrokken);
    
      prep.setString(4, "VERKOCHT");
    
      prep.setInt(5, artikelId);
    
      prep.executeUpdate();
    
    } catch(SQLException e){
    
      e.printStackTrace();
    } finally
    {
    
      pool.freeConnection(con);
    }
    

    And you should call prep.close() in the finally to close the PreparedStatement (if pool.freeConnection(con) does not take of this itself)).

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

Sidebar

Related Questions

I have problem in some JavaScript that I am writing where the Switch statement
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have a problem with a little .Net web application which uses the Amazon
I have an application that is already using the Spring Framework and Spring JDBC
I have a client/server application that depends on MS SQL database for backend storage,
I'm having a bit of a problem with my Android application. I have 2
We have an application that needs to access a database that is owned by
I have a problem with Hibernate (3.6.0.-Final) in my Java EE application using MySQL
We have a query that's taking 3 hours to finish. This wasn't a problem
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:

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.