MySql Last_Insert_Id() 的并发问题... (C#) [英] Concurrency Problem With MySql Last_Insert_Id()... (C#)

查看:111
本文介绍了MySql Last_Insert_Id() 的并发问题... (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定为什么下面的控制台应用程序没有为 last_insert_id() 产生预期的行为.我读过 last_insert_id() 返回特定连接的最后一个 auto_incremented 值,但在此代码中,两个连接返回相同的结果.有人能解释一下我哪里出错了吗?

I'm not really sure why the following console application doesn't produce the expected behavior for last_insert_id(). I've read that last_insert_id() returns the last auto_incremented value for a particular connection, but in this code, the same result is returned for both connections. Can someone explain where I've gone wrong?

    static void Main(string[] args)
    {
        string ConnectionString = "server=XXXX;database=XXXX;username=XXXX;password=XXXX;pooling=true;max pool size=100;min pool size=0";

        MySqlConnection conn1 = new MySqlConnection(ConnectionString);
        MySqlConnection conn2 = new MySqlConnection(ConnectionString);

        MySqlCommand command1 = new MySqlCommand();
        MySqlCommand command2 = new MySqlCommand();

        command1.Connection = conn1;
        command2.Connection = conn1;

        StringBuilder createTableCommandText = new StringBuilder();
        createTableCommandText.Append("Create Table TestTable (");
        createTableCommandText.Append("Id INT NOT NULL AUTO_INCREMENT, ");
        createTableCommandText.Append("str VARCHAR(20) NOT NULL, ");
        createTableCommandText.Append("PRIMARY KEY (Id));");

        StringBuilder insertCommandText = new StringBuilder();
        insertCommandText.Append("INSERT INTO TestTable (str) VALUES ('what is the dilleo?');");

        StringBuilder getLastInsertId = new StringBuilder();
        getLastInsertId.Append("SELECT LAST_INSERT_ID();");

        conn1.Open();
        conn2.Open();

        command1.CommandText = createTableCommandText.ToString();
        command1.ExecuteNonQuery();

        command1.CommandText = insertCommandText.ToString();
        command2.CommandText = insertCommandText.ToString();

        command1.ExecuteNonQuery();
        command2.ExecuteNonQuery();

        command1.CommandText = getLastInsertId.ToString();
        Console.WriteLine("Command 1:  {0}", command1.ExecuteScalar().ToString());
        command2.CommandText = getLastInsertId.ToString();
        Console.WriteLine("Command 2:  {0}", command2.ExecuteScalar().ToString());

        conn1.Close();
        conn2.Close();

        Console.ReadLine();
    }

我知道这不会发生,因为顶部连接字符串中的池化,因为我尝试在没有该部分字符串的情况下运行程序,但我仍然得到相同的结果(即命令 1 和命令 2last_insert_id() 显示相同的值).欢迎任何想法!

I know that this is not happening because of the pooling in the connection string at top, since I tried running the program without that part of the string, and I still got the same results (i.e. that both Command 1 and Command 2 displayed the same value for last_insert_id()). Any ideas are welcome!

非常感谢,

安德鲁

推荐答案

看这里:

command1.Connection = conn1;
command2.Connection = conn1;

您对两个命令使用相同的连接.

You are using the same connection for both commands.

这篇关于MySql Last_Insert_Id() 的并发问题... (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆