这怎么可能是同一个 sql 连接?
How can this be the same sql connection?
"select LAST_INSERT_ID()"与连接有关。所以我在 Mariadb 5.5.25 中测试,代码如下:
string strConn = "server=localhost;userid=root;password=admin;database=changde2018;charset=utf8;Allow Zero Datetime=True";
using (MySqlConnection connection = new MySqlConnection(strConn))
{
connection.Open();
MySqlCommand cmd = MySqlCommand("insert into t_data (gid) values(665)", connection);// gid is a autoincrease value
cmd.ExecuteNonQuery();
connection.Close();
}
using (MySqlConnection connection = new MySqlConnection(strConn))
{
DataTable dt2 = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter("select LAST_INSERT_ID()", connection);
da.Fill(dt2);
Console.WriteLine(dt2.Rows[0][0].ToString());//expect 0, but it's as same as the last inserted value
}
我在 navicat 中通过“显示状态 'Threads%'”查询。上面的exe代码后,'threads_connected'只增加了一位。这证明 Mariadb 确实将它们视为相同的连接。这就是为什么,我使用了两个不同的 MySqlConnection 对象,是因为相同的连接字符串吗?
正如 mysqlconnector.Net 文档在 connection pooling 上所说(突出显示是我的):
The MySQL Connector/Net supports connection pooling for better
performance and scalability with database-intensive applications. This
is enabled by default. You can turn it off or adjust its performance
characteristics using the connection string options Pooling,
Connection Reset, Connection Lifetime, Cache Server Properties, Max
Pool Size and Min Pool Size. See Section 5.1.1, “Creating a
Connector/Net Connection String” for further information.
Connection pooling works by keeping the native connection to the
server live when the client disposes of a MySqlConnection.
Subsequently, if a new MySqlConnection object is opened, it will be
created from the connection pool, rather than creating a new native
connection. This improves performance.
由于默认情况下连接池是打开的,当您关闭并处理第一个 mysqlconnection 对象并被下一段代码重用时,数据库连接并未关闭。
您可以使用 sql 中的 connection_id() 函数完全验证这一点。
"select LAST_INSERT_ID()"与连接有关。所以我在 Mariadb 5.5.25 中测试,代码如下:
string strConn = "server=localhost;userid=root;password=admin;database=changde2018;charset=utf8;Allow Zero Datetime=True";
using (MySqlConnection connection = new MySqlConnection(strConn))
{
connection.Open();
MySqlCommand cmd = MySqlCommand("insert into t_data (gid) values(665)", connection);// gid is a autoincrease value
cmd.ExecuteNonQuery();
connection.Close();
}
using (MySqlConnection connection = new MySqlConnection(strConn))
{
DataTable dt2 = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter("select LAST_INSERT_ID()", connection);
da.Fill(dt2);
Console.WriteLine(dt2.Rows[0][0].ToString());//expect 0, but it's as same as the last inserted value
}
我在 navicat 中通过“显示状态 'Threads%'”查询。上面的exe代码后,'threads_connected'只增加了一位。这证明 Mariadb 确实将它们视为相同的连接。这就是为什么,我使用了两个不同的 MySqlConnection 对象,是因为相同的连接字符串吗?
正如 mysqlconnector.Net 文档在 connection pooling 上所说(突出显示是我的):
The MySQL Connector/Net supports connection pooling for better performance and scalability with database-intensive applications. This is enabled by default. You can turn it off or adjust its performance characteristics using the connection string options Pooling, Connection Reset, Connection Lifetime, Cache Server Properties, Max Pool Size and Min Pool Size. See Section 5.1.1, “Creating a Connector/Net Connection String” for further information.
Connection pooling works by keeping the native connection to the server live when the client disposes of a MySqlConnection. Subsequently, if a new MySqlConnection object is opened, it will be created from the connection pool, rather than creating a new native connection. This improves performance.
由于默认情况下连接池是打开的,当您关闭并处理第一个 mysqlconnection 对象并被下一段代码重用时,数据库连接并未关闭。
您可以使用 sql 中的 connection_id() 函数完全验证这一点。