-1

I tried to write a below code where in two DB connections are getting open and close so will there be any issue in terms of TOO MANY CONNECTION issue? I am not getting any error but just wanted to confirm is there any issue in below code.

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

                %> 

1 Answers1

1

First of all handling Database logic in JSP is a bad practice...Avoid scriptlets as much as possible..Also you might run into concurrency issues as JSP's by default are not thread safe..I would write DAO utility class to handle database logic which would return appropriate data for action controller..Controller would set that data in the form of request/session attributes which you can forward to JSP in the question..

Sagar Kadu
  • 251
  • 2
  • 18