1.

If You Are Given A Choice To Implement The Code To Either Insert A Record Or Update If Already Exist, Which Approach Will You Follow ? 1. Insert Into The Db Table. If Exception Occurs, Update The Existing Record. 2. Check If The Record Exists And Update It If It Exists, If Not Insert A New Record.

Answer»

In first case, there would be 2 DB calls in WORST case and 1 in BEST case. In 2nd approach there will be always 2 DB calls.

Decision on the approach should depend on the following CONSIDERATIONS -

1. How costly is the call to DB. Are we using indices , hibernate etc

If calls to DB are costly , 1st approach should be the choice.

2. Exception Book KEEPING load upon exception.

The benefit of SAVING 1st call in approach 1 should be bigger than the Book keeping for the exception.

3. Probability of the exception in first apparoach.

If the DB Table is almost empty, it makes sense to follow Approach 1 as majority of the 1st calls will pass through without exception.

In first case, there would be 2 DB calls in worst case and 1 in best case. In 2nd approach there will be always 2 DB calls.

Decision on the approach should depend on the following considerations -

1. How costly is the call to DB. Are we using indices , hibernate etc

If calls to DB are costly , 1st approach should be the choice.

2. Exception Book keeping load upon exception.

The benefit of saving 1st call in approach 1 should be bigger than the Book keeping for the exception.

3. Probability of the exception in first apparoach.

If the DB Table is almost empty, it makes sense to follow Approach 1 as majority of the 1st calls will pass through without exception.



Discussion

No Comment Found