Close ResultSet Object Explicitly Before Closing Connection when Using Servlets

Close ResultSet Object Explicitly Before Closing Connection when Using Servlets

Most Java programmers close a connection with database directly without closing the ResultSet. This is okay with standalone Java programs, where the Java compiler automatically closes the Resultset for you, but when we deploy this code in a servlet, then we are not guaranteed this behavior.

For example, take a look at the following code snippet:

 public void doGet ( HttpServletRequest req, HttpServletResponse res )                         throws ServletException, IOException  {        Connection con = null;        try        {            Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );            con = DriverManager.getConnection("jdbc:odbc:issue","super","super");            ResultSet  rs   = stmt.executeQuery( "SELECT * FROM person_id_table" );            /* More Code Here */        }        catch( SQLException e )        {        }         finally        {           try            {                  if( con != null )                  {                     con.close();  // Resultset not closed prior to this statement                 }           }           catch( SQLException ignored )             {           }         }   } 

This code looks perfectly fine. The problem arises when we deploy the code using JSDK1.1 Servlet Engine. It leads to a Windows NT Access Violation Error. The same runs perfectly all right with ServletExec Servlet Engine. So to be safe, remember to close the ResultSet explicitly before closing the Connection like in the following code:

 public void doGet ( HttpServletRequest req, HttpServletResponse res )                         throws ServletException, IOException  {        Connection con = null;        ResultSet rs = null;        try        {            Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );            con = DriverManager.getConnection("jdbc:odbc:issue","super","super");            rs   = stmt.executeQuery( "SELECT * FROM person_id_table" );            /* More Code Here */        }        catch( SQLException e )        {        }         finally        {           try            {                  if( con != null )                  {                     rs.close();                    con.close(); // Resultset closed prior to this statement.                 }           }           catch( SQLException ignored )             {           }         }   } 
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several