How do you handle database exceptions in your application?
Are you trying to validate data prior passing it to DB or just relying on DB schema validation logic?
Do you try to recover from some kind of DB errors (e.g. timeouts)?
Here are some approaches:
- Validate data prior passing it to DB
- Left validation to DB and handle DB exceptions properly
- Validate on both sides
- Validate some obvious constraints in business logic and left complex validation to DB
What approach do you use? Why?
Updates:
I'm glad to see growing discussion.
Let’s try to sum up community answers.
Suggestions:
- Validate on both sides
- Check business logic constraints on client side, let DB do integrity checks from hamishmcn
- Check early to avoid bothering DB from ajmastrean
- Check early to improve user experience from Will
- Keep DB interacting code in place to simplify development from hamishmcn
- Object-relational mapping (NHibernate, Linq, etc.) can help you to deal with constrains from ajmastrean
- Client side validation is necessary for security reasons from Seb Nilsson
Do you have anything else to say?