sql - JDBC constraint exception details -
i'm using jdbc persist data sql server , want insert new row table has primary key, several foreign keys, , several uniqueness constraints on various columns. wondering how handle constraint violations can identify fields need corrected (in order report meaningful user).
e.g:
table test: id (primary), fk_id (foreign key), field1 (unique), field2 (unique)
inserting duplicate value in field1 or field2 gives:
errorcode: 2627 sqlstate: 23000 violation of unique key constraint '<unique constraint name>'. cannot insert duplicate key in object 'dbo.test'. duplicate key value (<value>). however, isn't enough determine field caused exception (so can tell user, correct field1 or field2), short of parsing error message , using constraint name (which doesn't feel right , seems brittle solution).
the other way can think of not rely on exceptions , instead query existing rows same value (manually checking uniqueness before insertion). however, requires potentially many queries in serializable transaction (reducing concurrency). worse, transaction can deadlock , rollback if timing unfortunately conflicts transaction.
is there better way achieve i'm trying without having parse (locale specific) exception messages? 'best practice' way?
Comments
Post a Comment