0

我正在创建一种将大量新记录批量插入(导入)到 ORACLE 数据库中的机制。我正在使用多个线程和依赖事务:

线程的创建:

const int ThreadCount = 4;
using (TransactionScope transaction = new TransactionScope())
{
    List<Thread> threads = new List<Thread>(threadCount);
    for (int i = 0; i < ThreadCount; i++)
    {
        Thread thread = new Thread(WorkerThread);
        thread.Start(Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete));
        threads.Add(thread);
    }
    threads.ForEach(thread => thread.Join());
    transaction.Complete();
}

执行实际工作的方法:

private void WorkerThread(object transaction)
{
    using (DependentTransaction dTx = (DependentTransaction)transaction)
    using (TransactionScope ts = new TransactionScope(dTx))
    {
         // The actual work, the inserts on the database, are executed here.
    }
}

System.Data.OracleClient.OracleException在此操作期间,我收到message类型的异常ORA-24757: duplicate transaction identifier

我究竟做错了什么?我是否以不正确的方式执行依赖事务?它与Oracle不兼容吗?如果是这样,是否有解决方法?

4

1 回答 1

1

我不知道您为什么会遇到此异常,但解决方法可能是:

const int ThreadCount = 4;
using (var connection = new OracleConnection(MyConnectionstring))
{
    connection.Open();
    using (var transaction = connection.BeginTransaction())
    {
        List<Thread> threads = new List<Thread>(ThreadCount);
        for (int i = 0; i < ThreadCount; i++)
        {
            Thread thread = new Thread(WorkerThread);
            thread.Start(transaction);
            threads.Add(thread);
        }
        threads.ForEach(thread => thread.Join());

        transaction.Commit();
    }
}

工人阶级看起来像:

private void WorkerThread(object transaction)
{
    OracleTransaction trans = (OracleTransaction) transaction;

    using (OracleCommand command = trans.Connection.CreateCommand())
    {
        command.CommandText = "INSERT INTO mytable (x) values (1)  ";
        command.ExecuteNonQuery();
    }
}
于 2013-06-14T09:03:01.200 回答