0

I have a problem where I have tried to login two or more web browser/same browsers with difference tab to the same web page, there will had an error said

Io exception: Socket closed

in either one side when refresh/login in the same time.

Should I use multithread open connection? If yes how it be done? Can anyone help with this problem? The program is written in JSP.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Jimmy428
  • 31
  • 1
  • 1
  • 7

1 Answers1

0

Make sure that you are not declaring and storing the SQL Connection as a static or instance variable anywhere in your code.

E.g, this is bad:

public class SomeClass {

    private Connection connection;
    // Or
    private static Connection connection;

}

It should be declared, created and closed within the very same method block as where you're executing the SQL query.

So, this is good:

public class SomeDAO {

    public SomeEntity find(Long id) throws SQLException {
        Connection connection = null;
        // ...

        try {
            connection = database.getConnection();
            // ...
        }
        finally {
            // ...
            if (connection != null) try { connection.close(); } catch(SQLException ignore) {}
        }

        return someEntity;
    }

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555