我用 C# 编写了一个类,旨在提供在给定数据库连接上运行数据库事务的能力。但是,当我尝试运行代码时,我分别在 Oracle 和 SQL Server 上收到以下两个错误。查看我的代码是否有解决此问题的简单方法?
甲骨文
连接已经是本地或分布式事务的一部分
SQL Server 2008
SqlConnection 不支持并行事务
基类
public abstract class DbFactoryDatabaseTransaction
{
public void DoTransaction(IDatabaseConnectivityObjectBasicResponse databaseConnectivityObject)
{
databaseConnectivityObject.PrepareConnection();
DbTransaction dbTransaction = databaseConnectivityObject.DBFactoryDatabaseConnection.BeginTransaction();
try
{
ExecuteSql(databaseConnectivityObject, dbTransaction);
dbTransaction.Commit();
}
catch (Exception ex)
{
dbTransaction.Rollback();
databaseConnectivityObject.Close();
throw;
}
finally
{
dbTransaction.Dispose();
}
}
/// <summary>
/// A method to allow the caller to decide how the SQL statements are called as part of a transaction
/// </summary>
public abstract void ExecuteSql(IDatabaseConnectivityObjectBasicResponse databaseConnectivityObject, DbTransaction dbTransaction);
}
覆盖方法
public override void ExecuteSql(IDatabaseConnectivityObjectBasicResponse databaseConnectivityObject, DbTransaction dbTransaction)
{
//oracle
List<string> transactions = new List<string>
{
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (1, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))",
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (2, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))",
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (3, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))",
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (4, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))",
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (5, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))",
"INSERT INTO TMA_NOT_TO_ENTITY_QUEUE (RECEIVED_NOTICE_ID, NOTICE_TEXT, STATE_ID, TIME_RECEIVED) VALUES (6, 'This is a notice', 1, to_date('2012/08/15', 'yyyy/mm/dd'))"
};
databaseConnectivityObject.DBFactoryDatabaseCommand.Transaction = databaseConnectivityObject.DBFactoryDatabaseConnection.BeginTransaction();
foreach (var transaction in transactions)
{
databaseConnectivityObject.DBFactoryDatabaseCommand.CommandText = transaction;
databaseConnectivityObject.DBFactoryDatabaseCommand.CommandType = CommandType.Text;
databaseConnectivityObject.DBFactoryDatabaseCommand.Transaction = dbTransaction;
databaseConnectivityObject.DBFactoryDatabaseCommand.ExecuteNonQuery();
}
}
调用方法
public void RunTransaction()
{
IDatabaseConnectivityObjectBasicResponse databaseConnectivityObject = new DbProviderFactoryConnectionBasic();
DoTransaction(databaseConnectivityObject);
}
测试方法
[TestMethod()]
public void RunTransactionTest()
{
TmaNoticeToClusteredEntityValidation target = new TmaNoticeToClusteredEntityValidation(BindVariables, SqlFactory, Dialect);
target.RunTransaction();
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}