FIX: SQL Server 2000 Driver for JDBC leaks server cursors (820773)



The information in this article applies to:

  • Microsoft SQL Server 2000 Driver for JDBC

SYMPTOMS

The Microsoft SQL Server 2000 Driver for JDBC does not close server cursors that it opens under some conditions. This can cause cursor build up and memory pressure on Microsoft SQL Server. This can also cause server and application performance degradation, so that eventually clients can no longer connect to the SQL Server.

The following errors may be reported in the SQL Server error log:

2003-05-06 11:24:10.82 server Error: 17803, Severity: 20, State: 17
2003-05-06 11:24:10.82 server Insufficient memory available.
2003-05-06 11:25:26.94 spid395 BPool::Map: no remappable address found.
Microsoft SQL Server may return the following error message to the client, but this message is generally not reported in the SQL Server error log:
Error 701: There is insufficient system memory to run this query.
The driver leaks the server cursor when all the following conditions are true:
  • The connection is opened with SelectMethod = Cursor property set.
  • The statement is executed with the execute method.
  • No resultset is retrieved.
  • The statement is closed.

CAUSE

According to the JDBC specifications, when a statement is closed, the driver immediately releases the database of the object of that statement, and also releases the JDBC resources. However, because of a bug in the Microsoft SQL Server 2000 Driver for JDBC, when a statement is executed with the execute method, the driver only closes retrieved resultsets. This behavior implicitly closes the associated server cursor. If no resultset is retrieved, the server cursor is left open.

RESOLUTION

Method 1

To resolve this issue, download and install SQL Server 2000 Driver for JDBC Service Pack 3 from the following Microsoft Web site:

Method 2

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.
The Windows version of this fix has the following properties:

   Date         Time   Version            Size    File name
   --------------------------------------------------------------
   05-May-2003  17:27  2.2.0034          286,788  Msbase.jar
   05-May-2003  17:27  2.2.0034           67,159  Mssqlserver.jar
   05-May-2003  17:27  2.2.0034           58,903  Msutil.jar

The Unix-based version of this fix has the following properties:

   Date         Time   Version            Size    File name
   --------------------------------------------------------------
   05-May-2003  14:27  2.2.0034          286,788  Msbase.jar
   05-May-2003  14:27  2.2.0034           67,159  Mssqlserver.jar
   05-May-2003  14:27  2.2.0034           58,903  Msutil.jar
Note Although the file timestamp may be different for each operating system that is listed, internally the files are exactly the same. The best way to determine the version of the driver that you are using is to use the getDriverVersion method of the DatabaseMetaData interface.

To install this hotfix, follow these steps:
  1. Unzip the hotfix package to get three related files (msbase.jar, mssqlserver.jar and msutil.jar)
  2. Click Start, click Run type %programfiles%\Microsoft SQL Server 2000 Driver for JDBC\lib, and then click OK.
  3. Replace the following existing files in this folder with the files in the hotfix:
    • msbase.jar
    • mssqlserver.jar
    • msutil.jar

WORKAROUND

To work around this problem use one of the following methods.

Note If your application uses any third-party components that may execute queries under the covers with the conditions that are mentioned in the "Symptoms" section of this article, use only the first method. Only the first method might work around the problem when you use these third-party components.

Method 1

Use the default SelectMethod. When the SelectMethod = Cursor Connection property is not set, the driver always uses the SelectMethod = Direct option. The SelectMethod = Direct does not open any cursors on the server.

Method 2

Use the executeQuery method instead of the execute method on the statement.

Method 3

Return the resultset by using the getResultSet method on the statement after you execute the query. In this case, the Close method on the statement implicitly closes the resultset and the associated server cursor.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

You can use Performance Monitor to monitor the number of cursors that are opened in Microsoft SQL Server. To do this, use the SQL Server:Cache Manager performance object, and then select the Cache Object Counts counter and the Cursors instance. If this counter shows a steady increase in value up to the point of the out of memory condition, you might be experiencing the problem that is mentioned in the "Symptoms" section of this article.

Steps to reproduce the problem

  1. Start SQL Profiler to trace the calls to the SQL Server that the application connects to. Make sure that the trace properties are set to include the Stored Procedures event. Start the trace.
  2. Compile, and then run the following Java code.

    Note You must replace the server name, the user id, and the password in the connection string as appropriate for your environment.
    import java.sql.*;
    import java.io.*;
    import java.lang.Object.*;
    
    public class CursorLeak
    { 
    
    	
    	public static void main( String args[] )
    	{ 
    	
    		Connection conn = null; 
    		Statement stmt = null; 
    		ResultSet result = null; 
    
    		try
    		{
     
    			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    			conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://myServer:1433;DatabaseName=myDatabase;SelectMethod=Cursor;User=UserID;Password=password" );
    			
    		      //Workaround #1: Use SelectMethod = Direct
    		      //conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://myServer:1433;DatabaseName=myDatabase;SelectMethod=Direct;User=UserID;Password=password" );
    		}
    		catch( Exception e )
    		{ 
    			System.out.println( e ); 
    		} 
    
    		try
    		{ 
    			stmt = conn.createStatement(); 
    			String query = "Select CompanyName, City, Phone from Customers"; 
    			stmt.execute( query ); 
    		      
    		      //Workaround #2: If you call executeQuery() instead of execute(), stmt.close() closes the cursor.
    		      //ResultSet rs = stmt.executeQuery( query );	
    		
    		      //Workaround #3: If you call getResultset() after you call execute(), stmt.close() closes the cursor.
    		      //ResultSet rs = stmt.getResultSet(); 		
    			
      		      //Closing explicitly the ResultSet closes the cursor
    		      //if(stmt != null)
    		      	//rs.close();
    
    			System.out.println( "Close" ); 
    			
    		      	stmt.close();
    
    			try
      			{
    				System.out.println("Wait");
    				Thread.sleep(10000);
    			}
    			catch(Exception e)
    			{
    				System.out.println(e.getMessage());
    			}  
    		}
    		catch( SQLException e )
    		{ 
    				System.out.println( e ); 
    		}
    	}
    }
  3. Check the Profiler output. You see sp_cursoropen and sp_cursorfetch. However, but the sp_cursorclose call is not there.

Modification Type:MinorLast Reviewed:10/25/2005
Keywords:kbHotfixServer kbQFE kbQFE KB820773 kbAudDeveloper