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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:43:30+00:00 2026-06-04T15:43:30+00:00

I have own connection pool: public final class ConnectionPool { private static final Logger

  • 0

I have own connection pool:

public final class ConnectionPool {


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

private static final int DEFAULT_POOL_SIZE = 10;


//single instance
private static ConnectionPool instance;
//queue of free connections
private BlockingQueue<Connection> connectionQueue;

public ConnectionPool(String driver, String url, String user,
        String password, int poolSize)
        throws ClassNotFoundException, DAOException{
    try{
        Class.forName(driver);
        connectionQueue = new ArrayBlockingQueue<Connection>(poolSize);
        for(int i = 0; i < poolSize ;i++){
            Connection connection = DriverManager.getConnection(url, user, password);
            connectionQueue.offer(connection);
        }
    }
    catch (SQLException e) {
        log.error(e);
        throw new DAOException(e.getMessage());
    }
}

public static void init() throws DAOException{
    try {
    if(instance == null){

        String driver  =  ConfigurationManager.
        getInstance().getProperty(ConfigurationManager.DATABASE_DRIVER_NAME);
        String url = ConfigurationManager.
        getInstance().getProperty(ConfigurationManager.DATABASE_URL);
        String user = ConfigurationManager.
        getInstance().getProperty(ConfigurationManager.DATABASE_USER);
        String password = ConfigurationManager.
        getInstance().getProperty(ConfigurationManager.DATABASE_PASSWORD);
        String poolSizeStr = ConfigurationManager.
        getInstance().getProperty(ConfigurationManager.DATABASE_POOLSIZE);
        int poolSize = (poolSizeStr != null) ?
                Integer.parseInt(poolSizeStr) : DEFAULT_POOL_SIZE;

        log.info("Trying to create pool of connections...");

        instance = new ConnectionPool(driver,url,user,password,poolSize);

        log.info("Connection pool initialized");
    }
    }catch (ClassNotFoundException e) {
        log.error(e);
    } catch (SQLException e) {
        log.error(e);
        throw new DAOException(e.getMessage());
    }
}

public static void dispose() throws DAOException {
    try {
        if(instance != null){
            instance.clearConnectionQueue();
            instance = null;
            log.info("Connection queue is disposed");
        }
    } catch (DAOException e) {
        log.info(e.getMessage());
        throw new DAOException(e.getMessage());
    }
}

public static ConnectionPool getInstance(){
    return instance;
}

public Connection takeConnection() {
    Connection connection = null;
    try{
        connection = connectionQueue.take();
    }catch (InterruptedException e) {
        log.info("Free connection waiting interrupted.Returned null connection");
        log.error(e);
    }
    return connection;
}

public static void releaseConnection(Connection connection) throws DAOException {
    try {

        if(!connection.isClosed()){
            if(!getInstance().connectionQueue.offer(connection)){
                log.info("Connections is not added.");
            }
        }
        else{
            log.info("Trying to release closed connection.");
        }
    } catch (SQLException e) {
        log.info("SQLException at connection isClosed(). Connection is not               added");
        throw new DAOException(e.getMessage());
    }
}

private void clearConnectionQueue() throws DAOException{
    try {
        Connection connection;
        while((connection = connectionQueue.poll()) != null){

            if(!connection.getAutoCommit()){
                connection.commit();
                connection.close();
            }
    }
    } catch (SQLException e) {
        log.info(e.getMessage());
        throw new DAOException(e.getMessage());
    }
}



}

And I now I’m initialise and destroy it with listener and load properties using my own class ConfigurationManager,connected with ResourceBundle:

public final class ConfigurationManager {

private static ConfigurationManager instance;
private ResourceBundle resourceBundle;
//getting info from config.properties
private static final String BUNDLE_NAME = "config";
public static final String DATABASE_DRIVER_NAME =
    "DATABASE_DRIVER_NAME";
public static final String DATABASE_URL =
    "DATABASE_URL";
public static final String DATABASE_USER =
    "DATABASE_USER";
public static final String DATABASE_PASSWORD =
    "DATABASE_PASSWORD";
public static final String ERROR_PAGE_PATH =
    "ERROR_PAGE_PATH";

public static final String BEAN_PATH =
    "BEAN_PATH";
public static final String DATABASE_POOLSIZE =
    "DATABASE_POOLSIZE";


public synchronized static ConfigurationManager getInstance() {
    if (instance == null) {
        instance = new ConfigurationManager();
        instance.resourceBundle =
            ResourceBundle.getBundle(BUNDLE_NAME);
    }
    return instance;
}
public String getProperty(String key) {
    return (String)resourceBundle.getObject(key);
}
 }

But I want to do it(init,destroy,properties), using Spring. So how could I do it?

  • 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-04T15:43:31+00:00Added an answer on June 4, 2026 at 3:43 pm

    Use Spring’s init and destroy methods, of course:

    http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/

    Personally, I think this is ill-advised. You aren’t likely to improve on the pools already available to you (e.g. C3P0). You may do worse. But that’s your choice.

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

Sidebar

Related Questions

I have my own connection pool class public class ConnectionPool { private static final
I'm trying to hide window after its startup. I have own window-class which is
I have my own type: CREATE TYPE MyType AS TABLE ( foo INT )
Should I have to read these vars and make my own abstract connection layer?
With most of the application servers nowadays have their own connection pools built-in, i.e.
I have own component which works in my testing winform app good but when
I would like to make every EditText object have own title like in Pure
I have my own simple equal heights code in jQuery to make two columns
I have an own annotation processor (let's call it MyProcessor) and a project (let's
I have my own asp.net cookie created like this: var authTicket = new FormsAuthenticationTicket(

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.