Microsoft KB Archive/894556

= FIX: A JDBC connection becomes invalid when you try to cancel a query from a second thread through the SQL Server 2000 Driver for JDBC =

Article ID: 894556

Article Last Modified on 4/12/2005

-

APPLIES TO


 * Microsoft SQL Server 2000 Driver for JDBC

-





SYMPTOMS
If you execute a query through the Microsoft SQL Server 2000 Driver for JDBC on one thread and then you try to cancel the query from a secondary thread, the JDBC connection on which the query was canceled may become invalid. This issue may cause a NullPointerException exception when you try to execute a subsequent SQL command on the same connection.



RESOLUTION
To resolve this issue, obtain SQL Server 2000 Driver for JDBC Service Pack 3 (SP3). For more information, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyID=07287b11-0502-461a-b138-2aa54bfdc03a&DisplayLang=en



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the behavior
 On the first thread, follow these steps:  Create a connection and a statement. Start a transaction on the connection, and then lock a row. To do this, update the row by using the statement. Create a second connection and a second statement. Start a worker thread, and then pass the second statement to the worker thread as a parameter.</ol> </li> On the second thread, execute a stored procedure that tries to use the previously locked row. This stored procedure will be blocked forever because you do not try to unlock that row.</li> On the first thread, follow these steps: <ol style="list-style-type: lower-alpha;"> Cancel the second statement.</li> Create a new statement on the second connection, and then execute an arbitrary query. For example, execute SELECT @@VERSION.</li></ol> </li></ol>

To do this, compile and then run the following Java code: import java.util.*; import java.sql.*;

public class Class1 {   static String strURL = &quot;jdbc:microsoft:sqlserver://<Server>:1433;SelectMethod=direct&quot;; static String strUid = &quot;<UserID>&quot;; static String strPass = &quot;<Password>&quot;; public static void main(String args[]) throws Exception {

Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;); createBlock; //Make con 2. //This is the connection where the cancellation will occur. System.out.println(&quot;making second con&quot;); Connection con2 = DriverManager.getConnection(strURL, strUid, strPass); con2.setAutoCommit(false); //Display some random data at this point to prove that the connection is good. //Notice that this data is not at all related to the data that is actually used //to create the blocking scenario. displayAnyData(con2); // Spawn another thread to call proc and then stop responding. System.out.println(&quot;spawning second thread&quot;); Statement s2 = con2.createStatement; iThread thread = new iThread( s2 ); thread.start;

System.out.println(&quot;main thread sleep.&quot;); // Sleep long enough for other thread to stop responding. Thread.sleep(10000); System.out.println(&quot;main thread back from sleep. Going to cancel&quot;); s2.cancel; Thread.sleep(10000); //start Rollback option //If you uncomment this code, you will see the &quot;operation has been canceled&quot; //message on the rollback call instead of the following displayAnyData call. //     System.out.println(&quot;main thread Going to rollback&quot;); //     try //     { //          con2.rollback; //     } //      catch (Exception ee) //     { //          System.out.println(&quot;after st.cancel, con2.rollback gives &quot; + ee ); //     }        //end rollback option s2.close; try {           System.out.println(&quot;main thread trying query with new stmt from con with canceled statement.&quot;);

//The first time that you do anything on this connection, you receive a message //about the query being canceled. displayAnyData(con2); //The second time that you try to do something on this connection, you receive //a NullPointerException exception. In this case, you just try to display the data again. displayAnyData(con2);

System.out.println(&quot;done with after-cancel query&quot;); }       catch (Exception ee) {           System.out.println(&quot;unexpected exception occurred &quot; + ee ); ee.printStackTrace; }   }    //The purpose of this function is just to create a new statement and then execute //a simple query (select @@version) to verify whether the connection //allows for any execution. public static void displayAnyData(Connection conn) {       Statement s3 = null; ResultSet rr = null; try {           s3 = conn.createStatement; System.out.println(&quot;\nStart trying to retreive data\n&quot;); rr = s3.executeQuery(&quot;select @@version&quot;);

boolean OK = rr.next; if (OK) System.out.println(&quot;result set got first row &quot; + rr.getString(1) ); else System.out.println(&quot;result set NO ROWS!&quot;); }       catch (Exception ex) {           System.out.println(&quot;Caught error in displayAnyData:\n\t&quot; + ex.getMessage); }       try {           if (rr != null) rr.close; if (s3 != null) s3.close; System.out.println(&quot;End trying to retreive data\n&quot;); }       catch (Exception ee) {           System.out.println(&quot;Error closing rr or s3 in displayData: &quot; + ee.getMessage); }   }

//The purpose of the createBlock function is just to set up a connection that will make //an update and then cause a second connection to block later on. //The second connection is set up to block. Then, we can cancel that query. //The cancellation of the query appears to be what really causes the //problem. Therefore, you must set up a situation to force that behavior to occur. public static void createBlock {       try {           //Make con 1. This connection is just used to set up a blocking //scenario. Therefore, con2 can block and then be canceled. System.out.println(&quot;making first con&quot;); Connection con1 = DriverManager.getConnection(strURL, strUid, strPass);

System.out.println(&quot;doing setup&quot;); // Make a table if it is required. Statement s1 = con1.createStatement; try {s1.executeUpdate(&quot;set nocount on&quot;);} catch (Exception e){} try {s1.executeUpdate(&quot;drop table mytable&quot;);} catch (Exception e){} try {s1.executeUpdate(&quot;create mytable joe (bar int)&quot;);} catch (Exception e){} try {s1.executeUpdate(&quot;insert mytable values(1)&quot;);} catch (Exception e){}

// Make proc if it is required. String makeproc = &quot;create proc myproc as begin select 'start tran' begin tran update mytable set bar = 2 commit tran select 'after tran' end&quot;; try {s1.executeUpdate(&quot;drop procedure myproc&quot;);} catch (Exception e){} try {s1.executeUpdate(makeproc);} catch (Exception e){}

// Use con 1, start tx, and lock table. System.out.println(&quot;setup completed&quot;); con1.setAutoCommit(false); s1.executeUpdate(&quot;update mytable set bar = 3&quot;); }       catch (Exception ex) {           System.out.println(&quot;Exception in createBlock: &quot; + ex.getMessage); }   } }

class iThread extends Thread {   private Statement m_s;

public iThread (Statement s)   { m_s = s;   }

public void run {       try {           System.out.println(&quot;thread 2 going to exec&quot;); m_s.execute(&quot;exec myproc&quot;); // This should stop responding (hang). System.out.println(&quot;thread 2 after exec, finished&quot;); }       catch (Exception e)        { System.out.println(&quot;second thread got &quot; + e.getMessage); }   } } Notes
 * In this code, replace,  , and   with the name of your computer that is running SQL Server, your user ID, and your password.
 * You may not receive a &quot;The operation was cancelled at the user's request&quot; exception if the stored procedure generated a result set before the procedure was actually canceled. The error message that states that the operation was canceled appears after the result set. If you modify the code in this section to fetch the results from the stored procedure, you receive a &quot;The operation was cancelled at the user's request&quot; exception when the next result set is fetched. Also, if the stored procedure is modified not to return a result set before the procedure is canceled, the exception will be raised without fetching the data.

Note The word &quot;canceled&quot; is spelled incorrectly in the exception error message.

<div class="references_section">