Should I ignore the exception from the database insert?

I have two tables, A and B, and a simple table X that maintains the relationship between them. X contains AID and BID as the primary key.

I am using Linq-to-Sql to insert a relationship like:

public void InsertRelationship(int y, int z) {
  DataContext.X.InsertOnSubmit(new x { AID = y; BID = z });
}

      

The problem is that two calls to the InsertRelationship () function can be called in extreme circumstances, so an exception will be thrown because of the duplicate record. It doesn't matter to me as I know the relationship exists, so I ignore the exception.

Is it okay to ignore the exception in this case, or is it still bad practice? Should I check that the relationship doesn't exist yet before the insert? How will this affect performance?

Update

There is no way to avoid duplicate calls to the InsertRelationship () function. This is a web application, so I cannot stop the user from opening two separate windows and, for example, calling the method twice. This method will not be called twice via normal user interaction, but I am programming against the edge case here. The percentage of duplicates is likely to be very low, but I cannot be sure of the exact numbers.

+2


source to share


5 answers


The main problem with just ignoring the exception is that the exception can be thrown for other issues not related to duplicate record at all that you don't want to hide, such as SQL timeouts or deadlocks among others.

The best solution would be to create your application so that they are not present, the best would be to check for their existence before doing the insert.



If performance is critical, the use of nesting cannot be dispensed with, the ratio between insertion into a working insert is very small and you can tell that the raised exception is only due to the duplicate problem, you might be better off just catching the exception and ignoring it. But this if with a lot of conditions :-)

+2


source


Generally, you should check first.

But perhaps more precisely, you should create an interface for your application so that it doesn't show a case where even normal user interaction might even create a case that you will need to test.



Regardless, it's best to check.

+2


source


Throwing exceptions is expensive in terms of performance. Also, the exception you get is never guaranteed for the same reason every time. You should always check if the line exists before adding it again, as you know the problem might exist to begin with.

Exceptions for exceptional circumstances - this is not exclusive, it is part of the normal flow for creating data.

+2


source


Though unlikely, doesn't check for and insert a record for a race condition? If so, you must program a defense for it, which means you will catch and investigate any exceptions.

In this case, you should check the exception type and / or its underlying error number before choosing to ignore it. This way you can only ignore the exception (s) associated with attempts to insert a duplicate record (likely a violation of a primary key constraint). You will want to treat other types of Exceptions differently.

+1


source


If you are sure that a record is not inserted just because it already exists, it should be fine. Another wise ignoring exception can hide something else dangerous.

0


source







All Articles