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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:27+00:00 2026-06-16T06:34:27+00:00

This code below is the base servlet we use for our project: public class

  • 0

This code below is the “base” servlet we use for our project:

public class MyServlet extends Servlet {
    private static final long serialVersionUID = 1L;

    protected Connection conn;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
    }

    protected void connectDB() throws NamingException, SQLException {
        // get database settings

        InitialContext cxt = new InitialContext();

        DataSource ds =(DataSource)cxt.lookup( "java:/comp/env/jdbc/MyDB" );
        conn = ds.getConnection();      
    }

    protected void closeDB() throws SQLException {
        try {
            if (conn!=null) conn.close();
        }
        finally {
            conn = null;
        }
    }

    protected void addCacheControl(HttpServletResponse response) {
        response.setHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, pre-check=0, post-check=0");
        response.setHeader("Pragma", "no-cache");
    }

}

For example SomeServlet extends MyServlet …

Now SomeServlet is access in the browser it do a forward() to a JSP page then it refresh for a certain period (say 10 sec.)

After a while that the page is open, this code throws NullPointerException:

stmt = conn.prepareCall("{call sp_someSP(?, ?, ?)}");
  • 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-16T06:34:28+00:00Added an answer on June 16, 2026 at 6:34 am

    You cannot use instance fields of a Servlet like this, because it will be shared by multiple threads.

    You need to explicitly pass the connection around as parameters and local variables through your program (so that they live on the stack, and each thread gets their own version), or maybe use a ThreadLocal if you want a more “global” approach. A ThreadLocal Connection can also be reliably closed by a ServletFilter when your request is done.

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

Sidebar

Related Questions

I have this code below. I need to use a class id instead of
I have this code below: <li> <a href=<?php echo base_url()?>home/promos/call class=promo-call active-call></a> </li> <li>
As you can see in the code below, I have an Abstract Base Class
The code below uses Javascript to create a base class, eventRaiser , that has
Consider the code below: public class Analyzer { protected Func f,fd; public delegate double
What I mean is, in the code below: class base { } class derived
This code below allows me to find the word error in all my files
This code below is for executing ls -l | wc -l. In the code,
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
This code below doesn't work correctly since my MFC program is in unicode circumstance.

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.