尝试写出Clob时Oracle表被锁定

时间:2019-02-06 00:37:55

标签: c# .net oracle odp.net clob

我正在将格式化的文本写到Oracle表中的CLOB上。最终,在进程运行时,表将被锁定。当我们的DBA签出连接时,似乎我已经在表上创建了多个锁,而其他用户没有其他连接。为什么下面的代码最终会在表上创建锁?在创建锁之前,此代码通常每天需要花费几天时间运行几次,每次运行100次。似乎没有挂起的交易。

public void Update_Html_Out(string key, string shortTitle, string htmlText)
{
    byte[] newvalue = Encoding.Unicode.GetBytes(htmlText);

    string sql = "UPDATE html_out SET short_title = :short_title, actual_text = :clob WHERE key = :key";

    using (var conn = new OracleConnection(_connectionString))
    using (var cmd = new OracleCommand(sql, conn))
    {
        conn.Open();

        using (var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
        {
            cmd.Transaction = transaction;
            using (var clob = new OracleClob(conn))
            {
                clob.Write(newvalue, 0, newvalue.Length);

                cmd.Parameters.Add("short_title", shortTitle);
                cmd.Parameters.Add("clob", clob);
                cmd.Parameters.Add("key", key);

                cmd.ExecuteNonQuery();

                transaction.Commit();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试此代码。请注意,您需要保存CLOB,而不是BLOB(如您所说)

public void Update_Html_Out(string key, string shortTitle, string htmlText)
{
    string sql = @"UPDATE html_out SET 
                     short_title = :short_title, 
                     actual_text = :clob 
                   WHERE key = :key";

    using (var conn = new OracleConnection(_connectionString))
    {
        conn.Open();

        using (var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
        {
            using (var cmd = new OracleCommand(sql, conn))
            {
                cmd.Transaction = transaction;
                cmd.Parameters.Add("short_title", OracleDbType.Varchar2, shortTitle, ParameterDirection.Input);
                cmd.Parameters.Add("clob", OracleDbType.Clob, htmlText, ParameterDirection.Input);
                cmd.Parameters.Add("key", OracleDbType.Varchar2, key, ParameterDirection.Input);

                cmd.ExecuteNonQuery();
            }
            transaction.Commit();
        }
    }
}