【问题标题】:Firebird dot net provider doesn't fully execute query?Firebird dot net provider 没有完全执行查询?
【发布时间】:2011-08-25 18:26:52
【问题描述】:

我正在 c# 中使用 Firebird 的 dot net 提供程序运行许多 SQL 命令。具体来说,我正在更改数据库架构,并进行数据更新等。

作为我处理的一部分,我创建了一个新表,运行查询以从旧表中复制数据,然后删除旧表。

当我这样做时,firebird 会生成和错误:

不成功的元数据更新对象正在使用中

我做了一些查看,它似乎复制数据的查询尚未“清除”我们或其他什么。我的意思是,当我在 C# 执行暂停的情况下检查 Firebird 中的监控表时,我看到 MON$STATEMENTS 表中的查询处于非活动状态。这是在我运行了提交语句之后。

我的问题:

在我尝试运行下一个命令之前,有没有办法暂停、等待或强制查询完全完成?

当我在 ISQL 中运行相同的查询序列时,它可以完美运行。有什么不同的 ISQL 我可以强制 dot net Firebird 提供程序这样做,这样它就不会保持这个查询打开或什么?

所以作为参考,代码看起来像这样(显然这是一个非常简化的):

    // create the table
    string commandString  = "CREATE TABLE ...";

    // run the command in a transaction and commit it
    mtransaction = Connection.BeginTransaction( IsolationLevel.Serializable );
    FbCommand command = new FbCommand(commandString, Connection, mtransaction);
    command.ExecuteNonQuery();
    transaction.Commit();
    transaction.Dispose();
    transaction = null;

    // copy the data to the new table from the old
    commandString = "INSERT INTO ...";

    mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable);
    FbCommand command = new FbCommand(commandString, Connection, mtransaction);
    command.ExecuteNonQuery();
    transaction.Commit();
    transaction.Dispose();
    transaction = null;

    // drop the old table
    commandString = "DROP TABLE ...";

    mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable);
    FbCommand command = new FbCommand(commandString, Connection, mtransaction);
    command.ExecuteNonQuery();

    // this command fails with the exception
    // if I pause execution in c# before running this command, and 
    // use isql to look at the db I see the new table, and the data fully populated
    // and I also see the inactive insert command in MON$STATEMENTS
    transaction.Commit();
    transaction.Dispose();
    transaction = null;

【问题讨论】:

  • 作为参考,我使用的是 Firebird 2.1.0 windows build,我相信 DDEXProvider-2.0.1 用于 c# 方面

标签: c# firebird


【解决方案1】:

我遇到了同样的问题并验证了 Beau 的(粗略)修补程序。然而,我找到了一个简单的解决方案:在事务提交后处理命令!则不再需要重新连接。

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable);
FbCommand command = new FbCommand(commandString, Connection, mtransaction);
command.ExecuteNonQuery();
transaction.Commit();
command.Dispose(); // Thus!
transaction.Dispose();

问候, AtoN

【讨论】:

  • 不错的发现!没试过!
【解决方案2】:

好的,问题的可怕解决方案:

实际上,我可以通过关闭和处理连接,然后重新连接来实现此功能。这导致以某种方式删除“卡住”查询,然后我可以执行 table drop 命令。所以序列看起来像这样:

// create the table
string commandString  = "CREATE TABLE ...";

// run the command in a transaction and commit it
mtransaction = Connection.BeginTransaction( IsolationLevel.Serializable );
FbCommand command = new FbCommand(commandString, Connection, mtransaction);
command.ExecuteNonQuery();
transaction.Commit();
transaction.Dispose();
transaction = null;

// copy the data to the new table from the old
commandString = "INSERT INTO ...";

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable);
FbCommand command = new FbCommand(commandString, Connection, mtransaction);
command.ExecuteNonQuery();
transaction.Commit();
transaction.Dispose();
transaction = null;


// ------------------  
// Drop the connection entirely and start a new one
// so the table can be dropped 

Connection.Close();
Connection.Dispose();

// build connection string 
FbConnectionStringBuilder csb = new FbConnectionStringBuilder();
csb.DataSource ... etc...

// connect
Connection = new FbConnection(connectionString);
Connection.Open();

// Now have new connection that does not have weird
// lingering query, and table can now be dropped
// -----------------



// drop the old table
commandString = "DROP TABLE ...";

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable);
FbCommand command = new FbCommand(commandString, Connection, mtransaction);
command.ExecuteNonQuery();

// this no longer fails because the connection was complete closed
// and re-opened
transaction.Commit();
transaction.Dispose();
transaction = null;

注意:我对这个解决方案非常不满意。它有效,但我不知道为什么。对我来说,不得不这样做来删除一张桌子似乎是多余的和不必要的。我非常感谢任何人在这件事上可能提供的任何见解!!!

【讨论】:

    【解决方案3】:

    我相信我遇到过类似的事情。我的猜测是根本原因似乎是一个特性:MVCC。当我弄乱架构或删除表然后重新创建时,Visual Studio 通常将其保持打开状态。我刚刚重启服务,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多