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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:47:51+00:00 2026-06-03T05:47:51+00:00

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

  • 0

I have my own connection pool class

public class ConnectionPool {


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

public final static String PROPERTIES_FILENAME = "config";
public static final int DEFAULT_POOL_SIZE = 10;


//single instance
private static ConnectionPool instatance;
//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(instatance == null){


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

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

        instatance = 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(instatance != null){
            instatance.clearConnectionQueue();
            instatance = null;
            log.info("Connection queue is disposed");
        }
    } catch (DAOException e) {
        log.info(e.getMessage());
        throw new DAOException(e.getMessage());
    }
}

public static ConnectionPool getInstance(){
    return instatance;
}

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 void releaseConnection(Connection connection) throws DAOException {
    try {

        if(!connection.isClosed()){
            if(!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 need it to be initialized in the begining of struts servlet’s life cycle. If it were regular servlet I’d used init() method not thinking, and I’d done it:

public class LinkAction extends DispatchAction {
private static final String PARAM_NAME_LANGUAGE = "language";

/**
 * This is the Struts action method called on
 * http://.../actionPath?method=myAction1,
 * where "method" is the value specified in <action> element : 
 * ( <action parameter="method" .../> )
 */
private static ConnectionPool connectionPool =
    ConnectionPool.getInstance();

public void init(){
    try {
        if(connectionPool == null){

            ConnectionPool.init();
            connectionPool = ConnectionPool.getInstance();
        }

    } catch (DAOException e) {
        log.error(e);
    }
}


public ActionForward newsList(ActionMapping mapping, ActionForm  form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    return mapping.findForward("newsList");
}

public ActionForward addNews(ActionMapping mapping, ActionForm  form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    return mapping.findForward("addNews");
}

public ActionForward changeLocale(ActionMapping mapping, ActionForm  form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    String localeValue = request.getParameter("localeValue");
    request.getSession().setAttribute(PARAM_NAME_LANGUAGE, localeValue);
    return mapping.findForward("newsList");
}


}

But this doesn’t work with Struts Action, so I decided that it could be done in the struts-config.xml or web.xml. But how?

  • 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-03T05:47:52+00:00Added an answer on June 3, 2026 at 5:47 am

    I think that you should use a ServletContextListener:

    http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

    Even when using another framework it’s a great place to put data source initialization code.
    This listener is called every time your applications comes up and running so that you can put your datasource into a context attribute and recover it whenever necessary.

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

Sidebar

Related Questions

I'm trying to hide window after its startup. I have own window-class which is
c#.Net 3.5 with a SQL Server 2000 backend, I have a connection string in
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(
I have an own Tree object implemented in PHP. Say we have a tree

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.