Microsoft KB Archive/894557

= FIX: The client connection may end when you insert an out-of-range datetime value in a server by using the SQL Server 2000 Driver for JDBC =

Article ID: 894557

Article Last Modified on 10/7/2005

-

APPLIES TO


 * Microsoft SQL Server 2000 Driver for JDBC

-





SYMPTOMS
When you insert an out-of-range datetime value in a Microsoft SQL Server-based server by using the Microsoft SQL Server 2000 Driver for JDBC, the server may receive the following error message and end the client connection:

2004-08-12 15:56:47.77 spid51 Error: 17805, Severity: 20, State: 3

2004-08-12 15:56:47.77 spid51 Invalid buffer received from client.

For example, the date &quot;March 15, 2002&quot; may be represented as &quot;2/03/15&quot;. This date will be interpreted as out-of-range according to the &quot;yyyy/MM/dd&quot; pattern. When the out-of-range error is detected by the JDBC driver, the client application may receive an exception that is similar to the following:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]The year, 2, is outside the range allowed by the SQL Server.

at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)

at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)

at com.microsoft.jdbc.sqlserver.tds.TDSDateTime. (Unknown Source)

at com.microsoft.jdbc.sqlserver.tds.TDSRPCParameter.write(Unknown Source)

at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.submitRequest(Unknown Source)

at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)

at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)

at Test.main(Test.java:40)

Subsequent tries to use the same connection may cause the following exception:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Connection reset

at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)

at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)

at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)

at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)

at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)

at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)

at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)

at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)

at Test.main(Test.java:58)



RESOLUTION
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 SQL Server 2000 Driver for JDBC 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:

http://support.microsoft.com/contactus/?ws=support

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.  Date        Time   Version   Size     File name - 1-Oct-2004  12:04  2.2.0043  286,953  Msbase.jar 1-Oct-2004 12:04  2.2.0043   67,167  Mssqlserver.jar 1-Oct-2004 12:04  2.2.0043   59,072  Msutil.jar



MORE INFORMATION
To work around the problem if the specified date indicates the year with a format of 19 or 20, format the date in the client code as follows: String dateString = &quot;2/03/15&quot; // Add &quot;0&quot; if the second character is &quot;/&quot;. String tmpString = dateString.substring(1,2); if(tmpString.equals(&quot;/&quot;)) { tmpString = &quot;0&quot; tmpString = tmpString.concat(dateString); dateString = tmpString; } According to the JDBC specification, if you use a four-character year pattern &quot; &quot; will interpret the year value literally. For example, the year value will be interpreted as 2 instead of 2002. Therefore, you must also use a two-character year pattern for the year. For more information, see the following code: try { // Change the Year figures from 4(yyyy) to 2(yy) //DateFormat formatter = new SimpleDateFormat(&quot;yyyy/MM/dd&quot;); DateFormat formatter = new SimpleDateFormat(&quot;yy/MM/dd&quot;);

// remaining code } To see this documentation, visit the following Web site:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

Steps to reproduce the behavior
  Create a table by using the following script: USE pubs GO CREATE TABLE t1(test_varchar VARCHAR(255), test_date DATETIME) GO   Compile and then run the following Java code: import java.io.*; import java.sql.*; import java.text.*; import java.util.*;

public class Test { public static void main(String[] args) { PreparedStatement pstmt = null; Connection connection = null; java.sql.Date targetDate; String dateString = &quot;2/03/15&quot; // March 15, 2002

try { Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot; ); connection = DriverManager.getConnection(&quot;jdbc:microsoft:sqlserver://:1433;databasename=pubs;&quot;, &quot;&quot;, &quot;&quot;);

try { DateFormat formatter = new SimpleDateFormat(&quot;yyyy/MM/dd&quot;); //DateFormat formatter = new SimpleDateFormat(&quot;yy/MM/dd&quot;); formatter.setLenient(false); targetDate = new java.sql.Date(formatter.parse(dateString).getTime); System.out.println(&quot;parsed without exception&quot;); } catch (ParseException e) { e.printStackTrace; return; }

try { System.out.println(&quot;before INSERT&quot;); pstmt = connection.prepareStatement(&quot;INSERT INTO t1 VALUES (?,?)&quot;); pstmt.setString(1, &quot;test&quot;); pstmt.setDate(2, targetDate); pstmt.executeUpdate; pstmt.close; } catch (Exception e) { e.printStackTrace; } finally { if (pstmt != null) pstmt.close; }

// Execute another query by using the same connection. try { System.out.println(&quot;before SELECT 0&quot;); pstmt = connection.prepareStatement(&quot;SELECT 0&quot;); ResultSet rs = pstmt.executeQuery; } catch (Exception e) { e.printStackTrace; } finally { if (pstmt != null) pstmt.close; }

connection.close; System.out.println(&quot;Finished.&quot;); } catch(Exception e) { e.printStackTrace; } } } Note In this code, replace the ,  , and   with the name of your computer that is running SQL Server, your user ID, and your password. 

