FIX: You do not receive a "Timeout expired" error message after subsequent tries to execute a stored procedure even though you use the setQueryTimeout method to set a time-out through the SQL Server 2000 Driver for JDBC (894561)



The information in this article applies to:

  • Microsoft SQL Server 2000 Driver for JDBC

SYMPTOMS

When you use the Microsoft SQL Server 2000 Driver for JDBC, you may notice that you do not receive a "Timeout expired" error message after subsequent tries to execute a stored procedure. This behavior occurs even you set a time-out by using the setQueryTimeout method and that time-out has elapsed. The function call to execute a statement returns. However, you do not receive an exception and the record set is empty.

RESOLUTION

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

WORKAROUND

To work around this problem, use one of the following methods:
  • Run the DBCC FREEPROCCACHE statement before you call the second stored procedure.
  • Specify the "WITH RECOMPILE" option when you create the stored procedure. For example, see the following code:
    	CREATE PROCEDURE [dbo].[usp_myProc] WITH RECOMPILE
    	AS
    	SELECT * FROM tableWithManyRows
    	GO

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create the following stored procedure:
    	USE pubs
    	GO
    	CREATE PROCEDURE [dbo].[usp_myProc]
    	AS
    	SELECT * FROM aTableWithManyRows
    	GO
    
  2. Compile and then run the following Java code:
    	import java.sql.*;
    	public class Test 
    	{
    		public static void main(String[] args) throws Exception
    		{
    			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    
    			// Set up a connection.
    			String url = "jdbc:microsoft:sqlserver://<Server>:1433;databasename=jdbc;SelectMethod=cursor;";
    			Connection conn = DriverManager.getConnection(url, "<UserID>", "<Password>");
    			System.out.println("\nGot a connection.");
    
    			// Execute the stored procedure.
    			String strSQL = "{call usp_myProc()}";
    
    			CallableStatement cstmt = null;
    			ResultSet rs = null;
    
    			try
    			{
    				// QUERY 1
    				System.out.println("\nRunning the first query.");
    				long lStart = System.currentTimeMillis();
    				cstmt = conn.prepareCall(strSQL);
    				cstmt.setQueryTimeout(60);	// Set the time-out to a high value.
    				rs = cstmt.executeQuery();
    				long lEnd = System.currentTimeMillis();
    				System.out.println("Time taken = " + (lEnd - lStart) + " ms\n");
    
    				// QUERY 2
    				System.out.println("\nRunning the second query.");
    				cstmt = (CallableStatement) conn.prepareCall(strSQL);
    				cstmt.setQueryTimeout(1);	// Set the time-out to one (1) second.
    				lStart = System.currentTimeMillis();
    				rs = cstmt.executeQuery();
    				lEnd = System.currentTimeMillis();
    				System.out.println("Time taken = " + (lEnd - lStart) + " ms\n");
    
    				// The time-out should occur before the query has finished running.
    				throw new Exception("The second query should not have returned to this line.\n");
    			} 
    			catch (SQLException e)
    			{
    				// A time-out exception should occur.
    				if (e.getMessage().indexOf("Execution timeout expired") != -1) 
    				{
    					System.out.println("Query timed out as expected.");
    					e.printStackTrace();
    				} 
    				else 
    				{
    					throw e;
    				}
    			}
    			finally 
    			{
    				if (rs != null) 
    				{
    					rs.close();
    					rs = null;
    				}
    				if (cstmt != null) 
    				{
    					cstmt.close();
    					cstmt = null;
    				}
    			}
    
    			conn.close();
    			System.out.println("Reached the end.");
    		}
    	}
    
    
    Note In this code, replace <Server>, <UserID>, and <Password> with the name of your computer that is running SQL Server, your user ID, and your password.

REFERENCES

For more information about JDBC, click the following article number to view the article in the Microsoft Knowledge Base:

313100 How to get started with Microsoft JDBC

For more information about the standard terminology that Microsoft uses to describe software updates, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates


Modification Type:MajorLast Reviewed:3/23/2005
Keywords:kbBug kbQFE kbDatabase kbJDBC kbJava kbfix KB894561 kbAudDeveloper