我正在 MySql 中创建一个临时表,并希望在断开连接后它不会存在。我已经从代码中删除了所有内容,只留下了这个:
//create the first connection
var connection = new MySqlConnection(ConnectionString);
connection.Open();
//create a temp table
var cmd = connection.CreateCommand();
cmd.CommandText = "CREATE TEMPORARY TABLE SomeDummyTable(Column1 float)";
cmd.ExecuteNonQuery();
//read the new temp table to proove it's created
cmd = connection.CreateCommand();
cmd.CommandText = "select * from SomeDummyTable";
cmd.ExecuteNonQuery();
//close and dispose of the first connection
connection.Close();
connection.Dispose();
//create new connection
MySqlConnection connection2 = new MySqlConnection(ConnectionString);
connection2.Open();
var cmd2 = connection2.CreateCommand();
cmd2.CommandText = "select * from SomeDummyTable";
//this does not throw an exception, even though SomeTable is
//a temp table from the previous connection
cmd2.ExecuteNonQuery();
因此,我使用一个连接创建一个临时表,然后关闭该连接并打开另一个连接。我可以使用第二个连接查看临时表。是什么赋予了?我错过了什么?
此外:如果我关闭应用程序并重新打开它,则临时表不存在。可能是某种连接池之类的吗?
最佳答案
这似乎是 MySQL 连接池的错误,或者可能是您正在使用的特定提供程序的错误。根据我在粗略搜索中找到的内容,当创建临时表的连接关闭时,临时表应该自动删除。我不期望连接池会改变这种行为。
然后我想到了两种解决方法:
- 使用完临时表后,显式删除它。
- 通过将
Pooling=False;
添加到连接字符串来禁用连接池。
This MySQL bug report似乎很相关。显然其他连接属性没有被重置。也许临时表没有立即删除(或者以其他方式使后续连接无法访问)是同一问题的一部分。
You should report the bug.我搜索了“临时表连接池”的错误,但没有找到任何明确涵盖您的场景的错误。
关于c# - 连接关闭后,MySql 临时表仍然存在(来自.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768917/