This can be a specific version of this.
I wish to check should i be placing a replica row. Must I check it programmatically during my application layer:

if (exists(obj))
{
    throw new DuplicateObjectException();
}
HibernateSessionFactory.getSession().save(obj);

or must i catch the exception tossed through the database layer and triggered after i violate the contraint?

try
{
    HibernateSessionFactory.getSession().save(obj);
}
catch(ConstraintViolationException e)
{
    throw new DuplicateObjectException();
}

EDIT: Quite simply: although the constraint can there be to stay (it is good database design anyway, and that i can not be sure my application would be the just one being able to access the table) shall I depend around the constraint and take care of the exception its breach will raise, or I'd better check anyway?

EDIT2: Obviously I actually do check+place inside a transaction, securing the table to make sure not one other process is writing another record meanwhile

First, you must possess a primary key or unique constraint around the database to enforce this originality correctly - no doubt.

Considering the fact that the constraint is available, which way in the event you code within the application? My preference is always to try the place and catch the exceptions. Because most probably most card inserts will succeed, merely a couple of will fails as replicates (that is what "exception" suggests!): it's inefficient to do an is available check before every place, once the database will probably be carrying out its very own constraint checking anyway.

Also, it's theoretically feasible for the is available check to become wrong anyway - if another person handles to commit an archive with similar key value within the small interval involving the is available check as well as your place. Then, if you do not trap the database exception, you'll believe the place been successful much more fact it did not.

You have to catch the database exception unless of course you are able to guarantee that the application is the only person that ever card inserts rows (and ever will place rows) to your database.

EDIT: I might have get me wrong the question, however i would still reason that option B (HibernateSessionFactory throws the ConstraintException in the database) is the foremost option. Almost always there is a little chance that another application could place something within the sliver of your time involving the check and also the actual function call. Additionally, the only method to look into the dupe would be to perform one more query which is simply a pointless drain on performance.

My original knowledge of the question was that in option A the dupe check could be carried out internally (i.e. by utilizing just the data structures the program had already produced, with no query before the Place). My original answer is at reaction to this process.

You make sure that the item is available exclusively in application code, after which once satisfied that it doesn't, blithely save the item. But another concurrent client might place their very own object within the moment involving the two lines of code. So you'd obtain a Duplicate exception anyway, only this time around you do not catch it.

You have to do the save() and catch the exception. Otherwise you've got a race condition along with other concurrent clients focusing on exactly the same database.

Generally, I avoid coding that depends on errors being tossed because Used to do a problem. Sometimes, though, that's all you are able do. Inside your situation, I believe you can examine first.

This can break (permitting duplicate records) when the constraint will get dropped for whatever reason (typically maintenance work in which the DBA fails to re-enable it). You can examine with this situation inside the application.

However, it's good database design to achieve the database enforce the constraint (as you've quite appropriately stated) as others can also be while using database. Like a generalisation it is advisable to think that programs and databases reside in a M:M relationship - this can be many of the time.