コード例 #1
0
        protected override DbConnection CreateConnection()
        {
            for (int i = 0; i < 5; ++i)
            {
                try
                {
                    if (string.IsNullOrEmpty(ConnectString))
                    {
                        ConnectString = UpdateConnectString.GetNew();
                    }
                    var conn = new MySqlConnection(ConnectString);
                    conn.Open();
                    return(conn);
                }
                catch (Exception e)
                {
                    // mysql authentication error
                    if (e.Message.ToLower().StartsWith("authentication to host"))
                    {
                        Thread.Sleep(1000);
                        ConnectString = UpdateConnectString.GetNew();
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            throw new SpiderException("Can't get any connect string.");
        }
コード例 #2
0
        public override void InitPipeline(ISpider spider)
        {
            if (!IsEnabled)
            {
                return;
            }

            if (string.IsNullOrEmpty(ConnectString))
            {
                if (UpdateConnectString == null)
                {
                    throw new SpiderException("Can't find ConnectString or IUpdateConnectString.");
                }
                else
                {
                    for (int i = 0; i < 5; ++i)
                    {
                        try
                        {
                            ConnectString = UpdateConnectString.GetNew();
                            break;
                        }
                        catch (Exception e)
                        {
                            spider.Log("Update ConnectString failed.", LogLevel.Error, e);
                            Thread.Sleep(1000);
                        }
                    }

                    if (string.IsNullOrEmpty(ConnectString))
                    {
                        throw new SpiderException("Can't updadate ConnectString via IUpdateConnectString.");
                    }
                }
            }

            base.InitPipeline(spider);

            if (Mode == PipelineMode.Update)
            {
                return;
            }

            NetworkCenter.Current.Execute("db-init", () =>
            {
                using (DbConnection conn = CreateConnection())
                {
                    var command         = conn.CreateCommand();
                    command.CommandText = GetCreateSchemaSql();
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();

                    command.CommandText = GetCreateTableSql();
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            });
        }
コード例 #3
0
        public override void InitPipeline(ISpider spider)
        {
            if (ConnectionStringSettings == null)
            {
                if (UpdateConnectString == null)
                {
                    throw new SpiderException("ConnectionStringSettings or IUpdateConnectString are unfound.");
                }
                else
                {
                    for (int i = 0; i < 5; ++i)
                    {
                        try
                        {
                            ConnectionStringSettings = UpdateConnectString.GetNew();
                            break;
                        }
                        catch (Exception e)
                        {
                            Logger.MyLog(Spider.Identity, $"Update ConnectString failed.", LogLevel.Error, e);
                            Thread.Sleep(1000);
                        }
                    }

                    if (ConnectionStringSettings == null)
                    {
                        throw new SpiderException("Can not update ConnectionStringSettings via IUpdateConnectString.");
                    }
                }
            }

            base.InitPipeline(spider);


            foreach (var metadata in EntityAdapters.Values)
            {
                if (!metadata.InsertModel)
                {
                    continue;
                }

                NetworkCenter.Current.Execute("dbi", () =>
                {
                    using (var conn = ConnectionStringSettings.GetDbConnection())
                    {
                        var command         = conn.CreateCommand();
                        command.CommandText = GenerateIfDatabaseExistsSql(metadata, conn.ServerVersion);

                        if (Convert.ToInt16(command.ExecuteScalar()) == 0)
                        {
                            command.CommandText = GenerateCreateDatabaseSql(metadata, conn.ServerVersion);
                            command.CommandType = CommandType.Text;
                            command.ExecuteNonQuery();
                        }

                        command.CommandText = GenerateCreateTableSql(metadata);
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }
                });
            }
        }