MySQL:连接关闭后不允许任何操作

时间:2015-03-30 14:09:57

标签: java mysql jdbc

我在互联网上阅读了很多有关此内容的信息并实施了解决方案,但我仍然看到了这一点,有人可以给我一些帮助吗?

以下是我正在做的事情:

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);
        }

    }

我的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;
        }

    }
}

我的db.properties具有autoconnect:

URL = JDBC:MySQL的://本地主机:3306 / wsidb autoReconnect的=真

1 个答案:

答案 0 :(得分:1)

提取数据后,应关闭连接语句rs indoPost。在if和else条件下将一些日志放入DbUtil中,然后将清楚是否正在创建新连接并且使用现有连接。如果您使用某种连接池dpcp或bonecp,我更愿意。