![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Given a transaction that posts some data, and it bounces back with a MySqlException (in the .Net connector) because of a duplicate key/unique value, in general one needs to know which column and value were involved. Of course one could parse the error message, but that's a little tacky, and on a more practical level carries the risk that the wording could change in a future or localized version. Is there a programmatic way to do it? The MySqlException class doesn't seem to offer anything obvious. Thanks, -- "Always look on the bright side of life." To reply by email, replace no.spam with my last name. |
#3
| |||
| |||
|
|
In what cases would this not be a duplicate of the Primary Key? |
#4
| |||
| |||
|
|
Ed Prochak wrote: In what cases would this not be a duplicate of the Primary Key? I'm using a synthetic primary key, so in a given table there will typically be two unique keys, the synthetic primary and the domain level key. Even if I were using only one key, a transaction in general will add multiple records to multiple tables, so it's necessary to know which table and which record the duplicate occurred in. |
#5
| |||
| |||
|
|
I think there is no standard way to access this information because the reasoning is as follows: the application wanted to insert X,Y,Z within a transaction. If there is a constraint violation it does not matter what item caused the violation, the whole transaction cannot succeed. This goes with the assumption that the application had a reason it wanted to insert X,Y,Z and there is no alternative X,Y,R. |
|
Other than that, if the exception is thrown for each violating insert statement you could find out by wrapping each insert in try catch. But: if your DB happens to wait for COMMIT you don't have any other chance as to try to verify before insert. This method however has its own drawbacks and uncertainties... |
#6
| |||
| |||
|
|
Such as, X goes through, then Y... then the whole transaction bounces, because in the time before COMMIT, another user slipped in with a transaction that used X. |
#7
| |||
| |||
|
|
Given a transaction that posts some data, and it bounces back with a MySqlException (in the .Net connector) because of a duplicate key/unique value, in general one needs to know which column and value were involved. Of course one could parse the error message, but that's a little tacky, and on a more practical level carries the risk that the wording could change in a future or localized version. Is there a programmatic way to do it? The MySqlException class doesn't seem to offer anything obvious. Thanks, When we post invoices and invoice items both keys are system generated. |
#8
| |||
| |||
|
|
It shouldn't be that likely that another user "slips in" with the identical records to insert unless your artificial key is not protected against such collisions. Most scenarios involving natural keys would minimize such a collision. Of course, sequential artificial keys usually return whatever the correct next value is, so I don't understand how this "other user" is able to coopt that. How come it's so likely that more than one user wants to input data with the same values? That seems to be unusual. |
#9
| |||
| |||
|
|
When we post invoices and invoice items both keys are system generated. First we write the invoice number without the items, then we write the items once we know the the invoice number then we rewrite the keys to the items in the invoice file. We have a routine to get the next generated key. By generated it is just self incrementing. As the key is fetched it keeps the system from issuing the key to some other application. There is a similar painful process if you have to go back and write tax info. |
![]() |
| Thread Tools | |
| Display Modes | |
| |