Re: Unexpected Exception when inserting data in SQL 2000
- From: Joe Weinstein <joeNOSPAM@xxxxxxx>
- Date: Tue, 11 Apr 2006 10:47:30 -0700
andrew.elmhorst@xxxxxxxxx wrote:
It's varchar. I have more information and a way to consistently
reproduce it. If a duplicate key insert is done the very first time I
call .execute() on the preparedstatement, the preparedstatement throws
the same SQLException every subsequent time it is used.
The jTDS and MSSQL 2000 JDBC driver both work fine in that scenario.
I traced the call it is making to the database: It uses sp_prepexec,
which is supposed to prepare and execute the statement. Apparently it's
not recovering properly if the statement does not execute the first
time because of a database error!
Yep, duped it:
C:\new_ms_driver>java foo
Driver version is 1.0.809.102
Got expected exception
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__#foo_
_5D401C1C'. Cannot insert duplicate key in object '#foo________________________________________
________________________________________________________________________000000000C82'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source
)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getMoreResults(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.seekToOutParams(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.getPrepStmtHandle(Unknown So
urce)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source
)
at foo.main(foo.java:37)
from this code:
DatabaseMetaData dd = c.getMetaData();
System.out.println("Driver version is " + dd.getDriverVersion() );
Statement stmt = c.createStatement();
stmt.execute("create table #foo(bar varchar(50) primary key)");
stmt.execute("insert into #foo values ('joe')");
PreparedStatement p = c.prepareStatement("insert into #foo values(?)");
try {
p.setString(1, "joe");
p.executeUpdate();
} catch (Exception expected) {
System.out.println("Got expected exception");
}
p.setString(1, "andrew");
p.executeUpdate(); // fails here
.
- Follow-Ups:
- Re: Unexpected Exception when inserting data in SQL 2000
- From: andrew . elmhorst
- Re: Unexpected Exception when inserting data in SQL 2000
- References:
- Unexpected Exception when inserting data in SQL 2000
- From: andrew . elmhorst
- Re: Unexpected Exception when inserting data in SQL 2000
- From: Joe Weinstein
- Re: Unexpected Exception when inserting data in SQL 2000
- From: andrew . elmhorst
- Unexpected Exception when inserting data in SQL 2000
- Prev by Date: Re: Escape character weirdness
- Next by Date: Re: Unexpected Exception when inserting data in SQL 2000
- Previous by thread: Re: Unexpected Exception when inserting data in SQL 2000
- Next by thread: Re: Unexpected Exception when inserting data in SQL 2000
- Index(es):
Relevant Pages
|