1

I'm trying to create a simple API Backend using Jetty and HttpServlets. I've read here and here about the details of thread safety with HttpServlets The general consensus seems to be that this is the proper way to deal with variable access in HttpServlerts:

public class MyServlet extends HttpServlet {
    private static Object thisIsNotThreadsafe;
    private Object thisIsAlsoNotThreadsafe;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object thisIsThreadsafe;

        thisIsNotThreadsafe = request.getParameter("foo"); // BAD! Shared among all requests.
        thisIsAlsoNotThreadsafe = request.getParameter("foo"); // BAD! Shared among all requests.
        thisIsThreadsafe = request.getParameter("foo"); // Good.
    }
}

What I'm trying to do is create a new MongoClient:

MongoClient mongoClient = new MongoClient("localhost", 27017);

I could make this a "global variable", but that wouldn't be threadsafe. I could make a new instance everytime doGet or doPost is called. But MongoDB's Java Driver Documentation seems to advise against that as well:

A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.

What's the best way to go about this?

Community
  • 1
  • 1
Parth Mehrotra
  • 2,712
  • 1
  • 23
  • 31

1 Answers1

1

If a MongoClient instance is threadsafe and can be shared by multiple threads without being a concurrency bottleneck, then you could implement a Singleton wrapper for the shared MongoClient instance.

Based on what the Mongo documentation seems to be saying, this would be the "most correct" approach, especially if you have / may have multiple servlet classes.

If MongoClient instances were not threadsafe, etcetera, then you would need to implement some kind of (thread-safe) connection pool for the MongoClient instances.


If you are using a framework that supports Dependency Injection (DI) then there are other solutions that avoid some of the issues with explicit singletons and pools.

Graham
  • 7,431
  • 18
  • 59
  • 84
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • So could I just make a static MongoClient object that belongs to the class that extends HttpServlet? That would be safe and good practice? – Parth Mehrotra Oct 09 '16 at 17:53
  • Safe? Yes. Good practice? Hmmm ... did I mention singletons? – Stephen C Oct 09 '16 at 22:40
  • Oh, I didn't know that was bad practice, can you direct me to some reading material about this? – Parth Mehrotra Oct 09 '16 at 23:00
  • 1
    Well, whether it is a good or bad idea depends on the context. In your case, it depends on whether there are, **or might be in the future**, any other servlets that might need to the MongoDB. And whether or not they might need to share the client object. The words "good practice" and "bad practice" are lazy terms. What you should **really** be doing is understanding the issues and making your own judgment in the specific context. – Stephen C Oct 10 '16 at 02:39