简体   繁体   中英

Violation of Primary Key Constraint on Read

I have a web service which has a number of data access methods. This particular method only reads data. It does not do any inserts, updates, and no triggers are executed to cause an insert or update. We had a bug reported that was logged as a Violation of Primary Key constraint. As I said before this method does not insert anything. Here is the call stack:

The METHOD_NAME() method failed with an unhandled exception.
 [EXCEPTIONS] #1:SqlException(Violation of PRIMARY KEY constraint 'CONSTRAINT_NAME'. Cannot insert duplicate key in object 'dbo.TABLE_NAME'.
The statement has been terminated.) [SITE] Logged in Anon.DLL [DETAILS] Level=Error
[STACK]    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Anon.DataAccess.Common.DataAccessBase`4.ExecuteReader() in Anon\DataAccessBase.cs:line 183
   at Anon.DataAccess.GetSomething() in Anon\SomethingDal.cs:line 31
   at Anon.Service.Manager.GetSomething() in Anon\Manager.cs:line 53
   at Anon.WebService.GetSomething() in Anon\WebService.asmx.cs:line 47

There are other methods in this service that could generate that error but they would have to be called seperately. How could I get an insert error when I am only doing a read. Is it possible the two methods ran at the same time and the error was reported to the wrong connection?

This works correctly 99.9999% of the time but this one time something got crossed up.

Something is definitely getting inserted that is violating a pk constraint. It be a usage log, error log, or some other sort of auditing table whose purpose is to log usage of stored procedures. It could also be a function or a stored procedure that is invoked by the query that you're expecting to be read only.

It's generally not a good idea to insert the primary key, but to let it auto-increment on the DB side. There is a system variable you can call to get the ID of the key that was created by your query.

But really, depending on what you're trying to do, there are different ways to solve the issue.

In the end, something is wrong with your program logic as it's trying to insert a non-unique value into a unique column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM