【问题标题】:JSP - Can we open two DB connectios on same JSP by closing each [duplicate]JSP - 我们可以通过关闭每个 [重复] 在同一个 JSP 上打开两个数据库连接吗?
【发布时间】:2016-10-16 06:26:02
【问题描述】:

我尝试编写下面的代码,其中两个数据库连接正在打开和关闭,那么在 TOO MANY CONNECTION 问题方面会有任何问题吗?我没有收到任何错误,只是想确认以下代码中是否有任何问题。

    <%
                        Class.forName("com.mysql.jdbc.Driver");
                        connection = DriverManager.getConnection(connectionURL, username, password);
                        statement = connection.createStatement();
                        try {
                            rs1 = statement.executeQuery("SELECT QUERY");
              if (rs1.next()) {
                                try {

                                    PreparedStatement ps1 = DriverManager.getConnection(connectionURL, username, password).prepareStatement("UPDATE QUERY);
                                    ps1.execute();
                                    ps1.close();

                                } catch (Exception e) {
                                    out.println(e);
                                }
                            } else {
                                rs1 = statement.executeQuery("SELECT QUERY");

                if (rs1.next()) {

                          CONDITION.....

             }
              }
          rs1.close();
                        }
                    }


                        finally {
              if (!connection.isClosed() && connection != null) {
                            connection.close();
                        }
                    }

                %> 

                Some HTML CODE.....HERE


               <%
                        Class.forName("com.mysql.jdbc.Driver");
                        connection = DriverManager.getConnection(connectionURL, username, password);
                        statement = connection.createStatement();
                        try {
                            rs1 = statement.executeQuery("SELECT QUERY");
              if (rs1.next()) {
                                try {

                                    PreparedStatement ps1 = DriverManager.getConnection(connectionURL, username, password).prepareStatement("UPDATE QUERY);
                                    ps1.execute();
                                    ps1.close();

                                } catch (Exception e) {
                                    out.println(e);
                                }
                            } else {
                                rs1 = statement.executeQuery("SELECT QUERY");

                if (rs1.next()) {

                          CONDITION.....

             }
              }
          rs1.close();
                        }
                    }


                        finally {
              if (!connection.isClosed() && connection != null) {
                            connection.close();
                        }
                    }

                %> 

【问题讨论】:

  • 永远,永远不要在 JSP 中管理您的数据库连接 :-(((
  • 那我应该怎么做呢,通过连接池?..
  • 查看@Sagar Kadu 的回答...您必须学习一些有关 MVC 模式、JSP、数据库处理的基础知识...

标签: mysql sql database jsp servlets


【解决方案1】:

首先,在 JSP 中处理数据库逻辑是一种不好的做法...尽可能避免使用 scriptlet..此外,您可能会遇到并发问题,因为 JSP 默认情况下不是线程安全的..我会编写 DAO 实用程序类处理数据库逻辑,该逻辑将为操作控制器返回适当的数据。控制器将以请求/会话属性的形式设置该数据,您可以在问题中将其转发给 JSP。

【讨论】:

    猜你喜欢
    • 2014-06-24
    • 2020-08-22
    • 1970-01-01
    • 2013-06-19
    • 2012-07-10
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    相关资源
    最近更新 更多