数据库异常处理的最佳实践 [英] Database exception handling best practices

查看:146
本文介绍了数据库异常处理的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你是如何处理在应用程序数据库异常?结果
你试图将它传递给DB或仅仅依靠数据库架构验证逻辑之前验证数据?结果
你尝试从某种DB错误(例如超时)?

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)?

下面是一些方法:


  1. 将它传递给数据库之前验证数据

  2. 左验证,DB和处理DB异常正常

  3. 验证两侧

  4. 验证业务逻辑中一些明显的制约和左复杂的验证为DB

你用什么方法?为什么呢?

What approach do you use? Why?

更新:

我很高兴看到越来越多的讨论。结果
让我们尝试总结社群的答案。

I'm glad to see growing discussion.
Let’s try to sum up community answers.

建议:


  • 验证两侧

  • 检查业务逻辑制约
    客户端,让DB做完整性检查<一href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39406\">from hamishmcn

  • 检查早期以避免打扰DB <一个href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39461\">from ajmastrean

  • 检查早改善用户体验<一个href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39428\">from将

  • 保持DB交互code到位
    简化开发<一个href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39406\">from hamishmcn

  • 对象 - 关系映射(NHibernate的,LINQ的,等等)可以帮助你处理约束<一个href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39461\">from ajmastrean

  • 客户端验证是必要的安全原因,<一个href=\"http://stackoverflow.com/questions/39371/database-exception-handling-best-practices#39487\">from勒布尼尔森

  • 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? This is converted to Validation specific question. We are missing the core, i.e. "Database related Error best practices" which ones to handle and Which ones to Bubble up?

推荐答案

@aku:DRY是好的,但它并不总是可能的。验证是一个名额,你将有三个完全不同的,不相关的地方验证不仅是可能的,但绝对需要:中的用户界面,业务逻辑中,并在数据库中

@aku: DRY is nice, but its not always possible. Validation is one of those places, as you will have three completely different and unrelated places where validation is not only possible but absolutely needed: Within the UI, within the business logic, and within the database.

想想一个Web应用程序。要减少出行给服务器,让你的客户端包括数据输入的JavaScript验证。但是你不能信任用户输入什么,所以你必须触摸数据库之前,你的业务逻辑中进行验证。和数据库必须有它自己的验证,以prevent数据损坏。

Think of a web application. You want to reduce trips to the server, so you include javascript validation of client data entry. But you can't trust what the user enters, so you must perform validation within your business logic before touching the database. And the database must have its own validation in order to prevent data corruption.

有一个组件内统一这三种不同类型的验证,没有干净的方式。

There's no clean way to unify these three different types of validation within a single component.

有被做了一些尝试,以统一像像P和政策注射器内验证交叉责任;群P的<一个href=\"http://blogs.msdn.com/tomholl/archive/2007/02/23/announcing-the-policy-injection-application-block.aspx\">Policy注塑应用程序块与验证应用程序块相结合,但这些仍code为主。如果你有验证,这不是在code,你还是要单独保存并行逻辑...

There are some attempts being made to unify cross-cutting responsibilities like validation within policy injectors like the P&P group's Policy Injection Application Block combined with their Validation Application Block, but these are still code based. If you have validation that's not in code, you still have to maintain parallel logic separately...

这篇关于数据库异常处理的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆