Hi,
I'm currently debugging a database app, with the client running in VB on the 2008 framework, and the database running in MSSQL Server 2005
At the moment, I'm writing the Save routine, for the final stage of a single user interaction, and I've noticed something odd.
The first table I'm adding a record to has an Identity column, which is being used to generate the company's ID number for the record, and which needs to be done first so that I can associate all the other components of the record with that ID number.
The Insert on that table goes fine, but a later stage is currently failing, meaning that the Insert is rolled back.
What I'm then seeing, though, is that the Identity is still being incremented, and so if I have a failed Save for Identity 15, which then needs rolling back, the next record will go in at Identity 16, leaving a gap in the ID numbers, which I can't really have.
I've verified that this happens even with an explicit call to transaction.Rollback() for my transaction chain; even if I call Rollback() directly after a successful insert to this table, without any errors, the record gets deleted, but the Identity column's value is still incremented.
What's the solution, here? I can't really have the application roll the Identity back by itself on a bad save, that seems too risky to me, but no alternative springs to mind. Presumably I'm missing something obvious, but it seems very strange to me that the Identity column isn't being rolled back along with the rest of the transaction.
Can anyone advise me as to how best to get the behaviour I want, i.e. that no matter the circumstances of exit or error, I have an unbroken, sequential series of ID numbers to use as references?
Cheers,
KFrost
I'm currently debugging a database app, with the client running in VB on the 2008 framework, and the database running in MSSQL Server 2005
At the moment, I'm writing the Save routine, for the final stage of a single user interaction, and I've noticed something odd.
The first table I'm adding a record to has an Identity column, which is being used to generate the company's ID number for the record, and which needs to be done first so that I can associate all the other components of the record with that ID number.
The Insert on that table goes fine, but a later stage is currently failing, meaning that the Insert is rolled back.
What I'm then seeing, though, is that the Identity is still being incremented, and so if I have a failed Save for Identity 15, which then needs rolling back, the next record will go in at Identity 16, leaving a gap in the ID numbers, which I can't really have.
I've verified that this happens even with an explicit call to transaction.Rollback() for my transaction chain; even if I call Rollback() directly after a successful insert to this table, without any errors, the record gets deleted, but the Identity column's value is still incremented.
What's the solution, here? I can't really have the application roll the Identity back by itself on a bad save, that seems too risky to me, but no alternative springs to mind. Presumably I'm missing something obvious, but it seems very strange to me that the Identity column isn't being rolled back along with the rest of the transaction.
Can anyone advise me as to how best to get the behaviour I want, i.e. that no matter the circumstances of exit or error, I have an unbroken, sequential series of ID numbers to use as references?
Cheers,
KFrost