使用 Oracle DB 和 .NET 时的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1233714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practices when using oracle DB and .NET
提问by Vivek
What are the best practices or pit falls that we need to be aware of when using Microsoft Oracle provider in a web service centric .NET application?
在以 Web 服务为中心的 .NET 应用程序中使用 Microsoft Oracle 提供程序时,我们需要注意哪些最佳实践或陷阱?
回答by LBushkin
Some practices we employ based on our production experience:
我们根据我们的生产经验采用的一些做法:
- Validate connections when retrieving them from the connection pool.
- Write your service code to not assume that connections are valid - failure to do so can cause quite a bit of grief especially in production environments
- Wherever possible, explicitly close and dispose connections after using them (
using(conn){}
blocks work well) - In a service, you should use connections for the shortest time possible - particularly if you are looking to create a scalable solution.
- Consider using explicit timouts on requests appropriate to the typical duration of a request. The last thing you want is to have one type of request that hangs to potentially block your whole system.
- Wherever possible use bind variables to avoid hard parses at the database (this can be a performance nightmare if you don't start out with this practice). Using bind variables also protect you from basic SQL-injection attacks.
- Make sure you have adequate diagnostic support built into your system - consider creating a wrapper around the Oracle ADO calls so that you can instrument, log, and locate all of them.
- Consider using stored procedures or views when possible to push query semantics and knowledge of the data model into the database. This allows easier profileing and query tuning.
- Alternatively, consider use a good ORM library (EF, Hibernate, etc) to encapsulate data access - particularly if you perform both read and write operations.
- Extending on the above - don't pepper your code with dozens of individually written SQL fragments. This quickly becomes a maintainability nightmare.
- If you are committed to Oracle as a database, don't be afraid to use Oracle-specific features. The ODP library provides access to most features - such as returning table cursors, batch operations, etc.
- Oracle treats empty strings ("") and NULLs as equivalent - .NET does not. Normalize your string treatment as appropriate for Oracle.
- Consider using NVARCHAR2 instead of VARCHAR2 if you will store Unicode .NET string directly in your database. Otherwise, convert all unicode strings to conform to the core ASCII subset. Failure to do so can cause all sorts of confusing and evil data corruption problems.
- 从连接池中检索连接时验证连接。
- 编写您的服务代码不要假设连接是有效的 - 不这样做会导致相当多的痛苦,尤其是在生产环境中
- 在可能的情况下,在使用连接后明确关闭并处理它们(
using(conn){}
块运行良好) - 在服务中,您应该在尽可能短的时间内使用连接 - 特别是如果您希望创建可扩展的解决方案。
- 考虑对适合于典型请求持续时间的请求使用显式超时。您最不想做的事情是让一种类型的请求挂起以可能阻塞整个系统。
- 尽可能使用绑定变量来避免对数据库进行硬解析(如果您不开始使用这种做法,这可能是性能噩梦)。使用绑定变量还可以保护您免受基本的 SQL 注入攻击。
- 确保您的系统内置了足够的诊断支持 - 考虑围绕 Oracle ADO 调用创建一个包装器,以便您可以检测、记录和定位所有这些调用。
- 如果可能,请考虑使用存储过程或视图将查询语义和数据模型知识推送到数据库中。这允许更容易的分析和查询调整。
- 或者,考虑使用一个好的 ORM 库(EF、Hibernate 等)来封装数据访问 - 特别是如果您同时执行读取和写入操作。
- 扩展上述内容 - 不要用数十个单独编写的 SQL 片段来填充您的代码。这很快就变成了可维护性的噩梦。
- 如果您致力于将 Oracle 作为数据库使用,请不要害怕使用 Oracle 特定的特性。ODP 库提供对大多数功能的访问——例如返回表游标、批处理操作等。
- Oracle 将空字符串 ("") 和 NULL 视为等效的 - .NET 不会。根据 Oracle 的需要标准化您的字符串处理。
- 如果要将 Unicode .NET 字符串直接存储在数据库中,请考虑使用 NVARCHAR2 而不是 VARCHAR2。否则,转换所有 unicode 字符串以符合核心 ASCII 子集。不这样做可能会导致各种令人困惑和邪恶的数据损坏问题。
回答by Christian13467
Some more tips:
还有一些提示:
- Avoid using Microsoft Oracle provider because it goes out of support (http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx)
- If you're commited to Oracle use oracle specific features and link Oracle.DataAccess assembly to your code
- If you're not sure and want to be flexible, use System.Data.Common classes and load oracle provider through
- 避免使用 Microsoft Oracle 提供程序,因为它不再受支持 ( http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx)
- 如果您致力于使用 Oracle 特定功能并将 Oracle.DataAccess 程序集链接到您的代码
- 如果您不确定并希望灵活,请使用 System.Data.Common 类并通过以下方式加载 oracle 提供程序
回答by ConsultUtah
The Oracle Providers work fine in an ASP.NET application, but be aware of:
Oracle Providers 在 ASP.NET 应用程序中工作正常,但请注意:
- Matching the right version of the oracle client 32-bit or 64-bit with your application pool
- 32-bit client for a 32-bit app pool, 64-bit client for a 64-bit app pool.
- Permissions - grant the app pool user rights to the oracle client directory (c:\oracle\product\10.2.0\client_1)
- 将正确版本的 oracle 客户端 32 位或 64 位与您的应用程序池相匹配
- 32 位应用程序池的 32 位客户端,64 位应用程序池的 64 位客户端。
- 权限 - 授予应用程序池用户权限到 oracle 客户端目录 (c:\oracle\product\10.2.0\client_1)
This doesn't have anything to do with ASP.NET, but it is important to note that Oracle stores empty string and null both as null, so if you need to know that something was empty and not null, you need to add an additional column to track that...
这与 ASP.NET 没有任何关系,但需要注意的是,Oracle 将空字符串和 null 都存储为 null,因此如果您需要知道某些内容为空而不是 null,则需要添加一个额外的列来跟踪...