see the SqlTransaction class.
note. just using begin tran will not solve your problem. because your
check for existence and insert are two statements, you will still get
constraint errors.
if you really need to avoid the error (rather than catch and try again)
at the cost of scaling, you need to take an exclusive lock on the read
(this will prevent the other validator from reading the table) and
release at the update.
-- bruce (sqlwork.com)
Hi Andrew,
I agree to Bruce's suggestion here. Actually making the two operation(query
existing name from db table and insert new record with the name to db
table) in a transaction will only make the two operation set atomic, but
you will still encounter concurrent inserting issue that cause the
duplicated inserting error at inserting time. Also I have discussed with
other DB engineers and this scenario is not quite suitable to implement at
DB level(through transaction or explicit locking , no record to lock).
I think you can consider the following approachs to prevent duplicated
inserting in application code layer:
**Making the data accessing class (do the query and insert) methods a
contral entry point in your application. Thus, you can perform explicit
lock/exclusive against the methods calls. This will hit concurrent
processing performance but is acceptable if there won't frequent occur such
read/inserting operations in your application.
** Add a global inserting id/name list(a limited queue) in your
application, this list will contains all the current/recent item name being
inserted(in sequence). Thus, when you perform inserting, you will need to
check both the DB and this list to see whether there're duplicated existing
record or any one being inserted now.
How do you think?
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 28 Jun 2007 16:19 GMT
Hi Andrew,
Have you got any progress or does our suggestion in previous messages help
you some? If there is anything else we can help, please feel free to post
here.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.