-2

I read lot of information about this in the internet and implemented the solutions but I'm still seeing this, can someone please give me some help?

Here is what I'm doing:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Connection connection = DbUtil.getConnection(); try { UserBean user = new UserBean(); user.setUserName(request.getParameter("username")); user.setPassword(request.getParameter("password")); user = UserDAO.login(user); Statement statement = connection.createStatement(); ResultSet rs = statement .executeQuery("SELECT * FROM projects WHERE status IN ('Waiting docs','Testing','On hold')"); List<Project> projects = new ArrayList<Project>(); while (rs.next()) { Project project = new Project(); project.setProject(rs.getString("project")); project.setStatus(rs.getString("status")); project.setPoc(rs.getString("poc")); project.setDescription(rs.getString("description")); project.setDeploy_date(rs.getString("deploy_date")); project.setCurrent_status(rs.getString("current_status")); project.setNotes(rs.getString("notes")); project.setId(rs.getString("id")); projects.add(project); } if (user.isValid()) { HttpSession session = request.getSession(true); session.setAttribute("currentSessionUser", user); request.setAttribute("project", projects); RequestDispatcher disp = request.getRequestDispatcher("ListProjects.jsp"); disp.forward(request, response); } else { RequestDispatcher disp = request.getRequestDispatcher("invalidLogin.jsp"); disp.forward(request, response); // error page } } catch (Throwable theException) { System.out.println(theException); } } 

My DbUtil:

package util; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DbUtil { private static Connection connection = null; public static Connection getConnection() { if (connection != null) return connection; else { try { Properties prop = new Properties(); InputStream inputStream = DbUtil.class.getClassLoader() .getResourceAsStream("/db.properties"); prop.load(inputStream); String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String password = prop.getProperty("password"); Class.forName(driver); connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return connection; } } } 

My db.properties have the autoconnect:

url=jdbc:mysql://localhost:3306/wsidb?autoReconnect=true

3
  • “but I'm still seeing this” -> seeing what? Commented Mar 30, 2015 at 15:49
  • 1
    Your DbUtil class is a really bad idea, you are sharing a single connection for all requests, leading to all kinds of nice concurrency problems. Use a connection pool instead. Commented Mar 31, 2015 at 18:24
  • Can you give me an example? Commented Apr 1, 2015 at 21:37

1 Answer 1

1

You should close the connection,statement,rs indoPost after you have extracted the data. Put some logs in your DbUtil in both if and else conditions then it will be clear if new connection is being created or not and existing one is used. I would prefer if you use some kind of connection pool dpcp or bonecp.

Sign up to request clarification or add additional context in comments.

1 Comment

I think it could be the rs so... I did a change here and will test. Thanks for the answer!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.