jspdatabase-connectiondatabase-server

How to handle multiple users access a web page at the same time without socket close in any users side


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.


Solution

  • 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: