Servlet implements singlethreadmodel
Take the state out of your servlet instead, so that the same servlet can be used by multiple threads concurrently.
Chapter 9, "Database Connectivity".Keeping state in a "pool" of servlet instances, each of which can have state left over from the previous request etc is pretty horrible.public class SingleThreadConnection extends HttpServlet implements single bw SingleThreadModel Connection con null; / database connection, one per pooled servlet instance public void init(ServletConfig config) throws ServletException it(config / Establish the connection for this instance con establishConnection tAutoCommit(false public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException tContentType.Public void doGet(HttpServletRequest req, HttpServletResponse res) throws.For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used.However, SingleThreadModel does guarantee that more resources will be used than are needed, as maintaining the multiple instances has some cost to the servlet engine.For example, the following rather extreme implementation synchronizes the entire servlet activity: public class MyVeryThreadSafeServlet extends HttpServlet.The skeleton code is shown.The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
Writing your servlet with big synchronized blocks may be highly thread-safe but won't scale.
Note that SingleThreadModel does not solve all thread safety issues.
It's basically a poor way of handling concurrency.You could have plenty of spare CPU power; indeed, if you had eight CPUs, this implementation would leave seven of them mostly idle.If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method.If your servlet received an average of one request per second and the processing took an average of half a second, then this implementation is adequate.Make the servlet thread-safe.With this servlet implementation, every http request processed by this servlet would have to go through the same synchronized monitor from start to finish, so only one request could be processed at a time, regardless of how many threads you spawned for this servlet.
It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources.