在 .NET 中使用 oracle 数据库 事务提交,事务保存点
注意:ODP.NET 驱动程序针对 oracle 数据库访问进行了优化,因此可以获得最佳性能,并且它们还支持 oracle 数据库的丰富特性,如 BFILE、BLOB、CLOB、XMLType 等。如果您正在开发基于 oracle 数据库的 .NET 应用程序,那么就特性和性能来讲,ODP.NET 无疑是最佳的选择。
数据库模式设置 首先,您需要设置数据库模式,在此我们使用一个简化的 Web 商店示例。您必须首先创建一个名为 store 的用户并按以下方式将所需的权限授予该用户(您必须首先以拥有 Create USER 权限的用户身份登录数据库才能创建用户): Create USER store IDENTIFIED BY store; GRANT connect, resource TO store; 注意:您会在源代码文件 db1.sql 中找到前两个语句和该部分中出现的设置 store 模式的其他语句。 接下的语句以 store 用户身份进行连接: CONNECT store/store; 以下语句创建了所需的两个数据库表,名称分别为 product_types 和 products: Create TABLE product_types ( product_type_id INTEGER CONSTRAINT product_types_pk PRIMARY KEY, name VARCHAR2(10) NOT NULL ); Create TABLE products ( product_id INTEGER CONSTRAINT products_pk PRIMARY KEY, product_type_id INTEGER CONSTRAINT products_fk_product_types REFERENCES product_types(product_type_id), name VARCHAR2(30) NOT NULL, description VARCHAR2(50), price NUMBER(5, 2) ); 注意:如果您在一个不同的模式中为 store 用户创建了这些数据库表,那么您将需要修改示例配置文件(您稍后将看到)中的模式名称。 表 product_types 用于存储示例在线商店可能库存的产品类型的名称,表 products 包含了所销售产品的详细信息。 下面的 Insert 语句为表 product_types 和 products 添加行: Insert INTO product_types ( product_type_id, name ) VALUES ( 1, 'Book' ); Insert INTO product_types ( product_type_id, name ) VALUES ( 2, 'DVD' ); Insert INTO products ( product_id, product_type_id, name, description, price ) VALUES ( 1, 1, 'Modern Science', 'A description of modern science', 19.95 ); Insert INTO products ( product_id, product_type_id, name, description, price ) VALUES ( 2, 1, 'Chemistry', 'Introduction to Chemistry', 30.00 ); Insert INTO products ( product_id, product_type_id, name, description, price ) VALUES ( 3, 2, 'Supernova', 'A star explodes', 25.99 ); Insert INTO products ( product_id, product_type_id, name, description, price ) VALUES ( 4, 2, 'Tank War', 'Action movie about a future war', 13.95 ); COMMIT; 接下来,您将了解有关数据库事务的内容。 数据库事务简介 数据库事务是由一组 SQL 语句组成的一个逻辑工作单元。您可以把事务看作是一组不可分的 SQL 语句,这些语句作为一个整体永久记录在数据库中或一并撤销。比如在银行帐户之间转移资金:一条 Update 语句将从一个帐户的资金总数中减去一部分,另一条 Update 语句将把资金加到另一个帐户中。减操作和加操作必须永久记录在数据库中,或者必须一并撤销 — 否则将损失资金。这个简单的示例仅使用了两条 Update 语句,但一个更实际的事务可能包含许多 Insert、Update 和 Delete 语句。 要永久记录一个事务中的 SQL 语句的结果,您可以通过 COMMIT 语句来执行提交。要撤销 SQL 语句的结果,您可以使用 ROLLBACK 语句来执行回滚,这会把所有的行重设为它们原来的状态。只要您事先没有与数据库断开,则您在执行回滚之前所做的任何修改都将被撤销。您还可以设置一个保存点,以便将事务回滚至该特定的点,同时保持事务中的其他语句原封不动。 在 C# 和 VB.NET 中使用数据库事务 您可以使用 oracleTransaction 类的一个对象来表示一个事务。OracleTransaction 类包含多个属性,其中的两个为 Connection(指定与事务关联的数据库连接)和 IsolationLevel(指定事务隔离级别);本文稍后将向您介绍更多有关事务隔离级别的内容。 oracleTransaction 类包含许多操控事务的方法。您可以使用 Commit() 方法永久提交 SQL 语句,并可以使用 Rollback() 撤销这些语句。您还可以使用 Save() 在事务中设置一个保存点。 我现在将带着您逐步完成两个示例程序 — 一个用 C# 编写 (TransExample1.cs),另一个用 VB.NET 编写 (TransExample1.vb)。这些程序演示了如何执行一个包含了两条 Insert 语句的事务。第一条 Insert 语句将在表 product_types 中添加一行,第二条将在表 products 中添加一行。 导入命名空间 以下 C# 程序语句指定在程序中使用 System 和 oracle.DataAcess.Client 命名空间: using System; using oracle.DataAccess.Client; 下面是等价的 VB.NET 语句: Imports System Imports oracle.DataAccess.Client oracle.DataAccess.Client 命名空间是 ODP.NET 的一部分,它包含许多类,其中有 oracleConnection、OracleCommand 和 oracleTransaction。示例程序用到了这些类。 第 1 步 创建一个 oracleConnection 对象连接到 oracle 数据库,然后打开该连接。 在 C# 中: oracleConnection myOracleConnection = new oracleConnection( "User Id=store;Password=store;Data Source=ORCL" ); myOracleConnection.Open(); 在 VB.NET 中: Dim myOracleConnection As New oracleConnection( _ "User Id=store;Password=store;Data Source=ORCL") myOracleConnection.Open() User Id 和 Password 属性指定了您所要连接到的模式的数据库用户和口令。Data Source 属性指定了数据库的 oracle Net 服务名称;初始数据库的默认服务名称为 orCL。如果您使用的不是初始数据库,或者您的服务名称不同,那么您需要在程序中修改 Data Source 属性的设置。 第 2 步 创建一个 oracleTransaction 对象,然后调用 oracleConnection 对象的 BeginTransaction() 方法启动事务。 在 C# 中: oracleTransaction myOracleTransaction = myOracleConnection.BeginTransaction(); In VB.NET: Dim myOracleTransaction As oracleTransaction = _ myOracleConnection.BeginTransaction() 第 3 步 创建一个 oracleCommand 对象,用于存储 SQL 语句。 在 C# 中: oracleCommand myOracleCommand = myOracleConnection.CreateCommand(); 在 VB.NET 中: Dim myOracleCommand As oracleCommand = myOracleConnection.CreateCommand 因为 oracleCommand 对象使用 oracleConnection 对象的 CreateCommand() 方法创建的,所以它自动使用在第 2 步中为 oracleConnection 对象设置的事务。 第 4 步 将 oracleCommand 对象的 CommandText 属性设为向表 product_types 中添加一行的第一条 Insert 语句。 在 C# 中: myOracleCommand.CommandText = "Insert INTO product_types (" + " product_type_id, name" + ") VALUES (" + " 3, 'Magazine'" + ")"; 在 VB.NET 中: myOracleCommand.CommandText = _ "Insert INTO product_types (" & _ " product_type_id, name" & _ ") VALUES (" & _ " 3, 'Magazine'" & _ ")" 第 5 步 使用 oracleCommand 对象的 ExecuteNonQuery() 方法运行 Insert 语句。 在 C# 中: myOracleCommand.ExecuteNonQuery(); 在 VB.NET 中: myOracleCommand.ExecuteNonQuery() 第 6 和第 7 步 将 oracleCommand 对象的 CommandText 属性设为向表 Products 中添加一行的第二条 Insert 语句,并运行它。 在 C# 中: myOracleCommand.CommandText = "Insert INTO products (" + " product_id, product_type_id, name, description, price" + ") VALUES (" + " 5, 3, 'Oracle Magazine', 'Magazine about oracle', 4.99" + ")"; myOracleCommand.ExecuteNonQuery(); 在 VB.NET 中: myOracleCommand.CommandText = _ "Insert INTO products (" & _ " product_id, product_type_id, name, description, price" & _ ") VALUES (" & _ " 5, 3, 'Oracle Magazine', 'Magazine about oracle', 4.99" & _ ")" myOracleCommand.ExecuteNonQuery() 第 8 步 使用 oracleTransaction 对象的 Commit() 方法提交数据库中的事务。 在 C# 中: myOracleTransaction.Commit(); 在 VB.NET 中: myOracleTransaction.Commit() 在完成 Commit() 方法之后,由 Insert 语句添加的两行将在数据库中永久记录。 第 9 步 使用 Close() 方法关闭 oracleConnection 对象。 在 C# 中: myOracleConnection.Close(); 在 VB.NET 中: myOracleConnection.Close() 编译并运行示例程序 要编译 C# 示例程序,您可以使用 csc 命令运行 C# 编译器。因为程序使用 oracle Data Access DLL,所以您应使用 /r 选项指定该 DLL 的完整路径,例如: csc TransExample1.cs /r:C:/oracle/product/10.1.0/ Client_1/bin/Oracle.DataAccess.dll 注意:您需要用您计算机上的相应路径来替换该 DLL 的路径。此外,如果您的计算机找不到 csc 编译器,那么您可能需要运行 Microsoft sdkvars.bat 脚本来首先设置 .NET SDK 的环境变量;您可以在安装 .NET SDK 的 bin 目录中找到该脚本。 如果您遇到以下错误: Example1.cs(10,7):error CS0246:The type or namespace name 'Oracle' could not be found (are you missing a using directive or an assembly reference?) 这说明您没有在编译命令中正确指定 oracle Data Access DLL。(有关设置的信息,请参阅 John Paul Cook 的技术文章“在 oracle 数据库上构建 .NET 应用程序”。) 下面是用于编译 VB.NET 程序的等价命令: vbc TransExample1.vb /r:C:/oracle/product/10.1.0/ Client_1/bin/Oracle.DataAccess.dll /r:system.dll /r:system.data.dll 接下来,输入以下命令,运行示例: TransExample1 您将看到程序的输出。不过,如果您遇到类似以下的异常: An exception was thrown Message = orA-12514:TNS:listener does not currently know of service requested in connect descriptor 这说明 oracleConnection 对象的连接字符串中的 Data Source 的设置不正确。您应当咨询您的 DBA 或查阅 oracle Net 文档以获得更多详细信息。 如果您使用的是 VS .NET,那么您可以遵循以下指示来编译和运行 C# 程序 TransExample1.cs: 创建一个新的 C# 控制台应用程序。File>New Project,然后选择 Visual C# Projects,Console Application。 将项目命名为 TransExample1。 用 TransExample1.cs 中的代码替换 VS .NET 生成的所有代码。 选择 Project>Add Reference 添加对 oracle.DataAccess.dll 的引用,然后浏览至您安装 ODP.NET 的目录(在我的计算机上,它是 C:/oracle/product/10.1.0/Client_1/bin/Oracle.DataAccess.dll),然后双击 oracle.DataAccess.dll。 选择 Debug>Start without Debugging 运行该程序。 要编译和运行 TransExample1.vb,您可以执行类似的一系列步骤,但第 1 步应选择一个 Visual Basic 控制台应用程序,并在第 3 步用 TransExample1.vb 中的代码替换生成的代码。 查看程序的运行结果 当您运行完 C# 或 VB .NET 程序时,您可以在 SQL*Plus 中使用以下 Select 语句查看事务的结果: Select p.product_id, p.product_type_id, pt. name, p.name, p.description, p.price FROM products p, product_types pt Where p.product_type_id = pt.product_type_id AND p.product_id = 5; 您将看到以下结果: PRODUCT_ID PRODUCT_TYPE_ID NAME NAME ---------- --------------- ---------- ----------------------- DESCRIPTION PRICE -------------------------------------------------- ---------- 5 3 Magazine oracle Magazine Magazine about oracle 4.99 接下来,您将了解如何设置事务保存点。 在 .NET 程序中设置事务保存点 正如本文前面所提到的那样,您可以设置一个保存点,以便将事务回滚至该特定的点,同时保持事务中的其他语句原封不动。您可以使用 oracleTransaction 类的 Save() 方法在事务中设置保存点。 如果您有一个非常长的事务并且希望能够仅回滚到某个特定的时间点,那么您可能要使用保存点。例如,您可能想对 10 个产品做一些更改,然后设置一个保存点,然后再对另 10 个产品做更改;如果您在进行第二批更改时出现了错误,那么您可以回滚至保存点,使您的第一批更改原封不动。 我将带您逐步完成演示如何使用保存点的 C# (TransExample2.cs) 示例程序和 VB.NET (TransExample2.vb) 示例程序中的相关新步骤。这些程序向表 products 中添加一行,设置一个保存点,向表 products 中添加另一行,回滚至保存点,然后从表 products 中读取这些行。在回滚至保存点后,只有添加到表 products 中的第一行保留了下来:第二行将已被删除。 第 1 到第 3 步与“在 C# 和 VB.NET 中使用数据库事务”部分中所示的步骤相同,因此在这里将其省略。 第 4 步 向表 products 中添加一行,该行的产品 ID 为 6。 在 C# 中: myOracleCommand.CommandText = "Insert INTO products (" + " product_id, product_type_id, name, description, price" + ") VALUES (" + " 6, 2, 'Man from Another World', 'Man from Venus lands on Earth', 24.99" + ")"; myOracleCommand.ExecuteNonQuery(); 在 VB.NET 中: myOracleCommand.CommandText = _ "Insert INTO products (" & _ " product_id, product_type_id, name, description, price" & _ ") VALUES (" & _ " 6, 2, 'Man from Another World', 'Man from Venus lands on Earth', 24.99" & _ ")" myOracleCommand.ExecuteNonQuery() 第 5 步 使用 oracleTransaction 的 Save() 方法设置一个名为 SaveProduct 的保存点。 在 C# 中: myOracleTransaction.Save("SaveProduct"); 在 VB.NET 中: myOracleTransaction.Save("SaveProduct") 第 6 步 向表 products 中添加另一行,该行的产品 ID 为 7。 在 C# 中: myOracleCommand.CommandText = "Insert INTO products (" + " product_id, product_type_id, name, description, price" + ") VALUES (" + " 7, 2, 'Z-Files', 'Mysterious stories', 14.99" + ")"; myOracleCommand.ExecuteNonQuery(); 在 VB.NET 中: myOracleCommand.CommandText = _ "Insert INTO products (" & _ " product_id, product_type_id, name, description, price" & _ ") VALUES (" & _ " 7, 2, 'Z-Files', 'Mysterious stories', 14.99" & _ ")" myOracleCommand.ExecuteNonQuery() 第 7 步 回滚到先前在第 5 步中设置的 SaveProduct 保存点。 在 C# 中: myOracleTransaction.Rollback("SaveProduct"); 在 VB.NET 中: myOracleTransaction.Rollback("SaveProduct") 完成回滚后,在第 6 步中添加的第二行已被删除,而在第 4 步中添加的第一行保留了下来。 TransExample2.cs 和 TransExample2.vb 中剩下的步骤显示表 products 的内容,回滚整个事务并从数据库断开。 用于 Microsoft Transaction Server 的 oracle 事务服务的快速说明 Microsoft Transaction Server 是一个运行在互联网或网络服务器上的专有事务处理系统。Microsoft Transaction Server 为客户端计算机部署和管理应用程序和数据库事务请求。 Microsoft Transaction Server 是以服务器为中心的三层体系结构模型的一个组件。这种方法实现了将应用程序的表示、业务逻辑和数据元素清晰地分布到在一个网络中连接的不同计算机上。无需专门集成,您就可以在与 oracle 数据库服务器 8.0.6 版或更高版本连接的 Microsoft Transaction Server 中部署一个组件,但首先您必须安装 oracle Services for Microsoft Transaction Server。 结论 在本文中,您系统学习了在 .NET 程序中使用数据库事务。您了解了如何创建 oracleTransaction 对象并用它们将事务提交给数据库,如何使用保存点部分回滚一个事务,以及 oracle 数据库如何分离并发事务。 |