C#MySql TransactionScope
我的系统在sames MySql中有两个数据库。业务是确保数据完全插入同一事务中的两个数据库。以下是我的样本:C#MySql TransactionScope,c#,mysql,C#,Mysql,我的系统在sames MySql中有两个数据库。业务是确保数据完全插入同一事务中的两个数据库。以下是我的样本: public void test() { int returnValue = 0; System.IO.StringWriter writer = new System.IO.StringWriter(); try { using (TransactionScope scope = new TransactionScope())
public void test()
{
int returnValue = 0;
System.IO.StringWriter writer = new System.IO.StringWriter();
try
{
using (TransactionScope scope = new TransactionScope())
{
returnValue = TestA(returnValue, writer);
returnValue = TestB(returnValue, writer);
scope.Complete();
}
}
catch (Exception ex)
{
}
}
private static int TestB(int returnValue, System.IO.StringWriter writer)
{
using (MySqlConnection connection2 = new MySqlConnection("server=localhost;database=test;user id=root;password=root;port=3307;characterset=utf8;connectiontimeout=72000;"))
{
connection2.Open();
// Execute the second command in the second database.
returnValue = 0;
MySqlCommand command2 = new MySqlCommand("Insert tbb (`time`)value ('10:00:00')", connection2);
returnValue = command2.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
}
return returnValue;
}
private static int TestA(int returnValue, System.IO.StringWriter writer)
{
using (MySqlConnection connection1 = new MySqlConnection("server=localhost;database=test1;user id=root;password=root;port=3307;characterset=utf8;connectiontimeout=72000;"))
{
connection1.Open();
// Create the SqlCommand object and execute the first command.
MySqlCommand command1 = new MySqlCommand("Insert tb1 (`Name`, `Value`)value ('ai', '2017-04-26')", connection1);
returnValue = command1.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command1: {0}", returnValue);
}
return returnValue;
}
当我运行时,出现错误:
多个同时连接或具有不同连接的连接
同一事务中的连接字符串当前不可用
支持
为什么会这样
如果无法修复,请给我其他解决方案
private static int TestA(int returnValue, System.IO.StringWriter writer)
{
.... USE SAME CONNECTION AS TESTA HERE
// Create the SqlCommand object and execute the first command.
****THIS*****
MySqlCommand command1 = new MySqlCommand("Insert `test1`.`tb1` (`Name`, `Value`)value ('ai', '2017-04-26')", connection1);
****THIS*****
returnValue = command1.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command1: {0}", returnValue);
}
return returnValue;
}
所以,我的意思是。。。。不要使用2个连接。使用1连接并将数据库名称与表一起指定
MySqlConnection conn = new MySqlConnection();
private int TestB(int returnValue, System.IO.StringWriter writer)
{
MySqlConnection conn = new MySqlConnection("server=localhost;database=test;user id=root;password=root;port=3307;characterset=utf8;connectiontimeout=72000;");
conn.Open();
// Execute the second command in the second database.
returnValue = 0;
MySqlCommand command2 = new MySqlCommand("Insert tbb (`time`)value ('10:00:00')", conn);
returnValue = command2.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
conn.Close();
return returnValue;
}
private int TestA(int returnValue, System.IO.StringWriter writer)
{
conn = new MySqlConnection("server=localhost;database=test1;user id=root;password=root;port=3307;characterset=utf8;connectiontimeout=72000;");
conn.Open();
// Create the SqlCommand object and execute the first command.
MySqlCommand command1 = new MySqlCommand("Insert tb1 (`Name`, `Value`)value ('ai', '2017-04-26')", conn);
returnValue = command1.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command1: {0}", returnValue);
conn.Close();
return returnValue;
}
不能在一个
TransactionScope
内创建多个MySqlConnection
对象;如果您这样做,您将得到您看到的错误。这被追踪为
您可能需要升级到(与MySQL.Data兼容的OSS MySQL连接器库)。错误很明显。它不受支持。您可以尝试只使用1个连接,然后在第二个查询中插入“test1..tb1”请给我其他解决方案基本上两个连接同时使用相同的服务器实例,而不关闭以前的连接,这会导致并发问题。解决方案是为每个连接使用不同的
TransactionScope
(在完成每个事务后使用Complete
方法)&includeautoinclude=false
@tetsuyayayamamoto如果包含连接autoinclude=false
,则两个事务将分开工作。无法。由于两个表位于不同的数据库中。如果它是同一个服务器,则可以。。。好。。。我想哦,伙计们,为我的错误道歉。您的解决方案工作正常。谢谢但我有一个问题。如果两个数据库是分开的,怎么做呢?也许您需要将这两个数据库的当前单个事务范围分成两个事务范围。在使用不同的数据库启动第二个连接之前,请确保第一个连接已完成(随后将关闭并处理)。@TetsuyaYamamoto um,但如何确保两个数据都完全插入数据库?根据你的观点,如果第二个连接有问题,第一个连接不能反向滚动,我想简单地在第一个上添加conn.Close()
,使用块就可以了,但是当看到这篇文章时,我认为两个连接都需要单独的TransactionScope
。。我只是稍微修改一下他的代码。他说不工作。。我认为这是不可能的。。如果这不起作用。他首先清理数据库本身的连接,而不是代码。