MySQL OdbcCommand命令有时会挂起?

时间:2013-01-03 18:30:51

标签: c# .net mysql odbc

我在C#中运行一个循环,它在Windows 8 64位环境中使用MySQL ODBC 5.1驱动程序读取文件并对MySQL数据库进行更新。

操作很简单

  1. Count +1
  2. 查看文件是否存在
  3. 加载XML文件(XDocument)
  4. 从XDocument
  5. 获取数据
  6. 打开ODBCConnection
  7. 针对MySQL数据库运行几个存储过程来存储数据
  8. 关闭ODBCConnection
  9. 问题是,过了一段时间它会挂起,例如OdbcCommand.ExecuteNonQuery。它并不总是与它相同的SP?

    这是一个真正的问题,我需要循环60 000个文件,但一次只能持续1000个。

    编辑1: 这个问题似乎始终存在于时间:

    public bool addPublisherToGame(int inPublisherId, int inGameId)
            {
                string sqlStr;
                OdbcCommand commandObj;
                try
                {
                    sqlStr = "INSERT INTO games_publisher_binder (gameId, publisherId) VALUE(?,?)";
    
                    commandObj = new OdbcCommand(sqlStr, mainConnection);
                    commandObj.Parameters.Add("@gameId", OdbcType.Int).Value = inGameId;
                    commandObj.Parameters.Add("@publisherId", OdbcType.Int).Value = inPublisherId;
    
                    if (Convert.ToInt32(executeNonQuery(commandObj)) > 0)
                        return true;
                    else
                        return false;
    
                }
                catch (Exception ex)
                {
                    throw (loggErrorMessage(this.ToString(), "addPublisherToGame", ex, -1, "", ""));
                }
                finally
                {
    
                }
            }
    
    
    protected object executeNonQuery(OdbcCommand inCommandObj)
            {
                try
                {
                    //FileStream file = new FileStream("d:\\test.txt", FileMode.Append, FileAccess.Write);
    
                    //System.IO.StreamWriter stream = new System.IO.StreamWriter(file);
                    //stream.WriteLine(DateTime.Now.ToString() + " - " + inCommandObj.CommandText);
                    //stream.Close();
                    //file.Close();
    
                    //mainConnection.Open();
                    return inCommandObj.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
    

    我可以看到in参数是正确的

    连接的打开和关闭是通过top方法完成的(最终)。

    编辑2:

    这是将提取信息并保存到数据库的方法:

    public Boolean addBoardgameToDatabase(XElement boardgame, GameFactory gameFactory)
            {
                int incomingGameId = -1;
                XElement tmpElement;
                string primaryName = string.Empty;
                List<string> names = new List<string>();
                GameStorage externalGameStorage;
    
                int retry = 3;
                try
                {
                    if (boardgame.FirstAttribute != null &&
                        boardgame.FirstAttribute.Value != null)
                    {
                        while (retry > -1)
                        {
                            try
                            {
                                incomingGameId = int.Parse(boardgame.FirstAttribute.Value);
    
                                #region Find primary name
                                tmpElement = boardgame.Elements("name").Where(c => c.Attribute("primary") != null).FirstOrDefault(a => a.Attribute("primary").Value.Equals("true"));
    
                                if (tmpElement != null)
                                    primaryName = tmpElement.Value;
                                else
                                    return false;
                                #endregion
    
                                externalGameStorage = new GameStorage(incomingGameId,
                                                                    primaryName,
                                                                    string.Empty,
                                                                    getDateTime("1/1/" + boardgame.Element("yearpublished").Value),
                                                                    getInteger(boardgame.Element("minplayers").Value),
                                                                    getInteger(boardgame.Element("maxplayers").Value),
                                                                    boardgame.Element("playingtime").Value,
                                                                    0, 0, false);
    
                                gameFactory.updateGame(externalGameStorage);
                                gameFactory.updateGameGrade(incomingGameId);
    
                                gameFactory.removeDesignersFromGame(externalGameStorage.id);
                                foreach (XElement designer in boardgame.Elements("boardgamedesigner"))
                                {
                                    gameFactory.updateDesigner(int.Parse(designer.FirstAttribute.Value), designer.Value);
                                    gameFactory.addDesignerToGame(int.Parse(designer.FirstAttribute.Value), externalGameStorage.id);
                                }
    
                                gameFactory.removePublishersFromGame(externalGameStorage.id);
                                foreach (XElement publisher in boardgame.Elements("boardgamepublisher"))
                                {
                                    gameFactory.updatePublisher(int.Parse(publisher.FirstAttribute.Value), publisher.Value, string.Empty);
                                    gameFactory.addPublisherToGame(int.Parse(publisher.FirstAttribute.Value), externalGameStorage.id);
                                }
    
                                foreach (XElement element in boardgame.Elements("name").Where(c => c.Attribute("primary") == null))
                                    names.Add(element.Value);
    
                                gameFactory.removeGameNames(incomingGameId);
    
                                foreach (string name in names)
                                    if (name != null && name.Length > 0)
                                        gameFactory.addGameName(incomingGameId, name);
    
                                return true;
                            }
                            catch (Exception)
                            {
    
                                retry--;
                                if (retry < 0)
                                    return false;
                            }
                        }
    
                    }
    
                    return false;
                }
                catch (Exception ex)
                {
                    throw (new Exception(this.ToString() + ".addBoardgameToDatabase : " + ex.Message, ex));
                }
            }
    

    然后我们向前迈出了一步,这个方法会触发addBoardgameToDatabase:

    private void StartThreadToHandleXmlFile(int gameId)
            {
                FileInfo fileInfo;
                XDocument xmlDoc;
                Boolean gameAdded = false;
                GameFactory gameFactory = new GameFactory();
    
                try
                {
                    fileInfo = new FileInfo(_directory + "\\" + gameId.ToString() + ".xml");
    
                    if (fileInfo.Exists)
                    {
                        xmlDoc = XDocument.Load(fileInfo.FullName);
    
                        if (addBoardgameToDatabase(xmlDoc.Element("boardgames").Element("boardgame"), gameFactory))
                        {
                            gameAdded = true;
                            fileInfo.Delete();
                        }
                        else
                            return;
                    }
    
                    if (!gameAdded)
                    {
                        gameFactory.InactivateGame(gameId);
                        fileInfo.Delete();
                    }
                }
                catch (Exception)
                { throw; }
                finally
                {
                    if(gameFactory != null)
                        gameFactory.CloseConnection();
                }
            }
    

    最后是最高级别:

    public void UpdateGames(string directory)
    {
        DirectoryInfo dirInfo;
        FileInfo fileInfo;
        Thread thread;
    
        int gameIdToStartOn = 1;
    
        dirInfo = new DirectoryInfo(directory);
        if(dirInfo.Exists)
        {
            _directory = directory;
            fileInfo = dirInfo.GetFiles("*.xml").OrderBy(c=> int.Parse(c.Name.Replace(".xml",""))).FirstOrDefault();
    
            gameIdToStartOn = int.Parse(fileInfo.Name.Replace(".xml", ""));
    
            for (int gameId = gameIdToStartOn; gameId < 500000; gameId++) 
            {
                try
                { StartThreadToHandleXmlFile(gameId); }
                catch(Exception){}
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

  1. 通过向您的connectionstring添加“Pooling = true”来使用SQL连接池。
  2. 确保正确关闭连接和文件。
  3. 您可以创建一个大型查询并仅执行一次,我认为它比60.000次查询松散得多!
  4. 你能展示一下你的代码吗?