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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:54:43+00:00 2026-05-19T00:54:43+00:00

I am refactoring others code. The one thing I notice is that of the

  • 0

I am refactoring others code. The one thing I notice is that of the manner on how the system is getting a connection from the connection pool.

Sample is like this. On every call of the service method, the system is making a context lookup on the JNDI for the datasource.

public class CheckinServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            //Obtain Connection
            InitialContext initialContext = new InitialContext();
            javax.sql.DataSource ds = (javax.sql.DataSource) initialContext
                    .lookup("jdbc/mysqldb");
            java.sql.Connection conn = ds.getConnection();
            //business logic
            //redirect
        } finally {
            conn.close();
        }
    }
}

I do think that there is a performance hit on doing this every time. I am thinking of another way around these on how to retrieve a connection from a connection pool.

I am thinking about using the servlet’s init() method but I think that is not optimal.

  • 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-19T00:54:43+00:00Added an answer on May 19, 2026 at 12:54 am

    Do it once in a ServletContextListener instead of everytime in init() of many servlets. The contextInitialized() method is executed only once during webapp’s startup.

    public class Config implements ServletContextListener {
        private static final String ATTRIBUTE_NAME = "config";
        private DataSource dataSource;
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            ServletContext servletContext = event.getServletContext();
            String databaseName = servletContext.getInitParameter("database.name");
            try {
                dataSource = (DataSource) new InitialContext().lookup(databaseName);
            } catch (NamingException e) {
                throw new RuntimeException("Config failed: datasource not found", e);
            }
            servletContext.setAttribute(ATTRIBUTE_NAME, this);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // NOOP.
        }
    
        public DataSource getDataSource() {
            return dataSource;
        }
    
        public static Config getInstance(ServletContext servletContext) {
            return (Config) servletContext.getAttribute(ATTRIBUTE_NAME);
        }
    }
    

    Configure it as follows in web.xml:

    <context-param>
        <param-name>database.name</param-name>
        <param-value>jdbc/mysqldb</param-value>
    </context-param>
    <listener>
        <listener-class>com.example.Config</listener-class>
    </listener>
    

    You can obtain it in your servlet as follows (init() or doXXX() method, you choose):

    DataSource dataSource = Config.getInstance(getServletContext()).getDataSource();
    

    I’d however refactor it a step further, JDBC code should preferably be placed in its own classes, not in servlets. Lookup the DAO pattern.

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

Sidebar

Related Questions

While refactoring my code base I found a piece of code which I'd like
I'm refactoring some code and would like to junit test some methods but they
We are refactoring a weird system where within its backend, values that should be
I was refactoring some code and found there are two places that can be
I came across a few articles like this one , which suggest that some
I want to do some refactoring of code, especially the include-like relationships between files.
This days I'm refactoring code and one of the things I want to improve
I'm currently refactoring some Javascript code we have and amongst other things I've changed
I am refactoring some code where I grab the values inputted into textboxes and
Basically, I do lots of one-off code generation, large-scale refactorings, etc. etc. in Java.

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.