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 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:
The driver leaks the server cursor when all the following conditions are true:
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.
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, Microsoft recommends 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 phone numbers and information about support costs, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;%5BLN%5D;CNTACTMS
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 fix has the file attributes (or later) 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.jarThe 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 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.
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.
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.
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 ); } }}