public void LockedTable() { string connStr = GetConnectionString(true); connStr = String.Format(@"Use Affected Rows=true;allow user variables=yes;Server=localhost;Port=3306; Database={0};Uid=root;Connect Timeout=35;default command timeout=90;charset=utf8", database0); execSQL(@"CREATE TABLE `t1` ( `Key` int(10) unsigned NOT NULL auto_increment, `Val` varchar(100) NOT NULL, `Val2` varchar(100) NOT NULL default '', PRIMARY KEY (`Key`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1"); execSQL(@"CREATE TABLE `t2` ( `Key` int(10) unsigned NOT NULL auto_increment, `Val` varchar(100) NOT NULL, PRIMARY KEY (`Key`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1"); execSQL("lock tables t2 read"); using (TransactionScope scope = new TransactionScope()) { using (MySqlConnection conn = new MySqlConnection(connStr)) using (MySqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = @"insert into t1 (Val,Val2) values (?value1, ?value2)";; cmd.CommandTimeout = 5; cmd.Parameters.AddWithValue("?value1", new Random().Next()); cmd.Parameters.AddWithValue("?value2", new Random().Next()); cmd.ExecuteNonQuery(); } using (MySqlConnection conn = new MySqlConnection(connStr)) using (MySqlCommand cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = @"insert into t2 (Val) values (?value)"; cmd.CommandTimeout = 5; cmd.Parameters.AddWithValue("?value", new Random().Next()); try { cmd.ExecuteNonQuery(); } catch (MySqlException ex) { Assert.IsTrue(ex.InnerException is TimeoutException); } } scope.Complete(); } MySqlPoolManager.ClearAllPools(); }
/// <summary> /// Assigns a new server driver to the connection object /// </summary> /// <param name="groupName">Group name</param> /// <param name="master">True if the server connection to assign must be a master</param> /// <param name="connection">MySqlConnection object where the new driver will be assigned</param> internal static void GetNewConnection(string groupName, bool master, MySqlConnection connection) { do { lock (thisLock) { if (!IsReplicationGroup(groupName)) { return; } ReplicationServerGroup group = GetGroup(groupName); ReplicationServer server = group.GetServer(master, connection.Settings); if (server == null) { throw new MySqlException(Resources.Replication_NoAvailableServer); } //根据主连接字符串配置的账号、密码、数据库、端口,使用新的server的连接字符串(不更新,每次都添加) string newServerConnectionString = $"{server.ConnectionString}uid={connection.Settings.UserID};pwd={connection.Settings.Password};Database={connection.Settings.Database};port={connection.Settings.Port};"; try { bool isNewServer = false; if (connection.driver == null || !connection.driver.IsOpen) { isNewServer = true; } else { MySqlConnectionStringBuilder msb = new MySqlConnectionStringBuilder(newServerConnectionString); if (!msb.Equals(connection.driver.Settings)) { isNewServer = true; } } if (isNewServer) { var builder = new MySqlConnectionStringBuilder(newServerConnectionString); MySqlPool pool = MySqlPoolManager.GetPool(builder); Driver driver = pool.GetConnection(); //Driver driver = Driver.Create(new MySqlConnectionStringBuilder(newServerConnectionString)); connection.driver = driver; } return; } catch (MySqlException ex) { connection.driver = null; MySqlTrace.LogError(ex.Number, ex.ToString()); if (ex.Number == 1042) { //只有连不上的时候才标记为不可用 server.IsAvailable = false; //连接失败时打印日志(故障情况) Console.WriteLine("GetNewConnection Before HandleFailover >> Error:{0},{1}Number={2},ConnectionString={3}", ex.Message, ex.InnerException == null ? "" : "InnerException:" + ex.InnerException.Message + ",", ex.Number, newServerConnectionString); server.ConnectionString = newServerConnectionString; // retry to open a failed connection and update its status group.HandleFailover(server, ex); } else { throw; } } } } while (true); }