Answer the question
In order to leave comments, you need to log in
Do I need to take care of connection pool thread safety?
I am writing a study project. Inside one of the servlets, the ServletContext is called to get an attribute that stores the connection pool. Given that the ServletContext is accessed not only by multiple threads of the same servlet, but also by other servlets that require a connection to the database, is it necessary to think about thread pool thread safety? Those. theoretically, a situation may arise when the same Connection object from the connection pool will be accessed from different threads and the question arises of how to avoid this.
Code example:
ServletContext ctx = getServletContext();
ConnectionPool pool = (ConnectionPool) ctx.getAttribute("ConnectionPool");
Connection connection = pool.getConnection();
ctx.setAttribute("ConnectionPool", pool);
UserDAO uDao = new UserDAO(connection);
request.setCharacterEncoding("utf-8");
String login = request.getParameter("Login");
String password = request.getParameter("Password");
User user = uDao.getDataByPrimaryKey(login);
if (user != null & user.getPassword() == password) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
}
pool.putConnection(connection);
ctx.setAttribute("ConnectionPool", pool);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question