internal static void GetNewConnection(string groupName, bool master, MySqlConnection connection)
 {
     while (true)
     {
         object obj = ReplicationManager.thisLock;
         lock (obj)
         {
             if (ReplicationManager.IsReplicationGroup(groupName))
             {
                 ReplicationServerGroup group  = ReplicationManager.GetGroup(groupName);
                 ReplicationServer      server = group.GetServer(master, connection.Settings);
                 if (server == null)
                 {
                     throw new MySqlException(Resources.Replication_NoAvailableServer);
                 }
                 try
                 {
                     bool flag2 = false;
                     if (connection.driver == null || !connection.driver.IsOpen)
                     {
                         flag2 = true;
                     }
                     else if (!new MySqlConnectionStringBuilder(server.ConnectionString).Equals(connection.driver.Settings))
                     {
                         flag2 = true;
                     }
                     if (flag2)
                     {
                         Driver driver = Driver.Create(new MySqlConnectionStringBuilder(server.ConnectionString));
                         connection.driver = driver;
                     }
                 }
                 catch (MySqlException ex)
                 {
                     connection.driver  = null;
                     server.IsAvailable = false;
                     MySqlTrace.LogError(ex.Number, ex.ToString());
                     if (ex.Number == 1042)
                     {
                         group.HandleFailover(server, ex);
                         continue;
                     }
                     throw;
                 }
             }
         }
         break;
     }
 }
Exemplo n.º 2
0
        /// <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>
        public static void GetNewConnection(string groupName, bool master, MySqlConnection connection)
        {
            do
            {
                if (!IsReplicationGroup(groupName))
                {
                    return;
                }

                ReplicationServerGroup group  = GetGroup(groupName);
                ReplicationServer      server = group.GetServer(master);

                if (server == null)
                {
                    throw new MySqlException(Properties.Resources.Replication_NoAvailableServer);
                }

                Driver driver = Driver.Create(new MySqlConnectionStringBuilder(server.ConnectionString));
                if (connection.driver == null ||
                    driver.Settings.ConnectionString != connection.driver.Settings.ConnectionString)
                {
                    connection.Close();
                    connection.hasBeenOpen = false;
                    try
                    {
                        connection.driver = driver;
                        connection.Open();
                        return;
                    }
                    catch (Exception)
                    {
                        // retry to open a failed connection and update its status
                        connection.driver  = null;
                        server.IsAvailable = false;

                        BackgroundWorker worker = new BackgroundWorker();
                        worker.DoWork += delegate(object sender, DoWorkEventArgs e)
                        {
                            bool isRunning            = false;
                            ReplicationServer server1 = e.Argument as ReplicationServer;
                            int retryTime             = ReplicationManager.GetGroup(groupName).RetryTime;
#if !RT
                            System.Timers.Timer timer = new System.Timers.Timer(retryTime * 1000.0);


                            System.Timers.ElapsedEventHandler elapsedEvent = delegate(object sender1, System.Timers.ElapsedEventArgs e1)
                            {
                                if (isRunning)
                                {
                                    return;
                                }
                                try
                                {
                                    isRunning = true;
                                    using (MySqlConnection connectionFailed = new MySqlConnection(server.ConnectionString))
                                    {
                                        connectionFailed.Open();
                                        server1.IsAvailable = true;
                                        timer.Stop();
                                    }
                                }
                                catch
                                {
                                    MySqlTrace.LogWarning(0,
                                                          string.Format(Properties.Resources.Replication_ConnectionAttemptFailed, server1.Name));
                                }
                                finally
                                {
                                    isRunning = false;
                                }
                            };
                            timer.Elapsed += elapsedEvent;
                            timer.Start();
                            elapsedEvent(sender, null);
#else
                            Windows.UI.Xaml.DispatcherTimer timer = new Windows.UI.Xaml.DispatcherTimer();
                            TimeSpan ts = new TimeSpan(retryTime * 1000);
                            System.EventHandler <object> elapsedEvent = (TickSender, TickEventArgs) =>
                            {
                                if (isRunning)
                                {
                                    return;
                                }
                                try
                                {
                                    isRunning = true;
                                    using (MySqlConnection connectionFailed = new MySqlConnection(server.ConnectionString))
                                    {
                                        connectionFailed.Open();
                                        server1.IsAvailable = true;
                                        timer.Stop();
                                    }
                                }
                                catch
                                {
                                    MySqlTrace.LogWarning(0,
                                                          string.Format(Properties.Resources.Replication_ConnectionAttemptFailed, server1.Name));
                                }
                                finally
                                {
                                    isRunning = false;
                                }
                            };
                            timer.Tick += elapsedEvent;
                            elapsedEvent(sender, null);
                            timer.Start();
#endif
                        };

                        worker.RunWorkerAsync(server);
                    }
                }
                else
                {
                    return;
                }
            } while (true);
        }
Exemplo n.º 3
0
        public static void GetNewConnection(string groupName, bool master, MySqlConnection connection)
        {
            while (true)
            {
                if (!ReplicationManager.IsReplicationGroup(groupName))
                {
                    break;
                }
                ReplicationServerGroup group  = ReplicationManager.GetGroup(groupName);
                ReplicationServer      server = group.GetServer(master);
                if (server == null)
                {
                    goto Block_2;
                }
                Driver driver = Driver.Create(new MySqlConnectionStringBuilder(server.ConnectionString));
                if (connection.driver == null || driver.Settings.ConnectionString != connection.driver.Settings.ConnectionString)
                {
                    connection.Close();
                    connection.hasBeenOpen = false;
                    try
                    {
                        connection.driver = driver;
                        connection.Open();
                    }
                    catch (Exception)
                    {
                        connection.driver  = null;
                        server.IsAvailable = false;
                        BackgroundWorker backgroundWorker = new BackgroundWorker();
                        backgroundWorker.DoWork += delegate(object sender, DoWorkEventArgs e)
                        {
                            bool isRunning            = false;
                            ReplicationServer server1 = e.Argument as ReplicationServer;
                            int   retryTime           = ReplicationManager.GetGroup(groupName).RetryTime;
                            Timer timer = new Timer((double)retryTime * 1000.0);
                            ElapsedEventHandler elapsedEventHandler = delegate(object sender1, ElapsedEventArgs e1)
                            {
                                if (isRunning)
                                {
                                    return;
                                }
                                try
                                {
                                    isRunning = true;
                                    using (MySqlConnection mySqlConnection = new MySqlConnection(server.ConnectionString))
                                    {
                                        mySqlConnection.Open();
                                        server1.IsAvailable = true;
                                        timer.Stop();
                                    }
                                }
                                catch
                                {
                                    MySqlTrace.LogWarning(0, string.Format(Resources.Replication_ConnectionAttemptFailed, server1.Name));
                                }
                                finally
                                {
                                    isRunning = false;
                                }
                            };
                            timer.Elapsed += elapsedEventHandler;
                            timer.Start();
                            elapsedEventHandler(sender, null);
                        };
                        backgroundWorker.RunWorkerAsync(server);
                        continue;
                    }
                    return;
                }
                return;
            }
            return;

Block_2:
            throw new MySqlException(Resources.Replication_NoAvailableServer);
        }
 internal static ReplicationServer GetServer(string groupName, bool isMaster)
 {
     return(ReplicationManager.GetGroup(groupName).GetServer(isMaster));
 }
Exemplo n.º 5
0
        public static ReplicationServer GetServer(string groupName, bool isMaster)
        {
            ReplicationServerGroup group = ReplicationManager.GetGroup(groupName);

            return(group.GetServer(isMaster));
        }