Jump to content
  • Advertisement
Sign in to follow this  
Evil Steve

LibMySQL: Lost connection to MySQL server during query

This topic is 4994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I have a question for anyone with any libMySQL experience. I finally got a working version of my MUD running overnight last night, and I decided to have a look through the log this afternoon, and I found this:
[14/03/2005 17:02:53] WARNING: Failed to insert user to the database! Error: Lost connection to MySQL server during query
That caused the user to get disconnected, and they reconnected a few seconds later, and managed to register fine (I.e. the insert worked fine the second time). Does anyone know what cuased this? Was it just a timeout? It looks like it was about 14 hours since the database was last accessed before that call, so that seems likely. Secondly, does anyone know how to prevent this (Other than just trying again if it fails)? Here's the relevant source code:
bool CDatabase::InsertUser(CProfile* pUser)
char szBuff[4096];

   // Get user ID //
   pUser->m_dwUserID = GetNextUserID();
      return false;

   // Allocate user for internal list //
   CProfile* pInternalUser = new CProfile;
      Thread_SetError("Out of Memory!");
      return false;

   // Compose query //
   sprintf(szBuff,"INSERT INTO `users` (`userid`, `username`, "
      "`password`, `sex`, `class`, `showname`, `desc`, `email`, "
      "`website`, `hp`, `max_hp`, `mp`, `max_mp`, `stamina`, `max_stamina`, "
      "`firstip`, `firstlogin`, `lastip`, `lastlogin`, `lastloginlen`, "
      "`onlinetime`, `cash`, `balance`, `roomid`, `status`, `statustext`, "
      "`respawntime`, `permissions`, `kickcount`, `bancount`, `deathcount`, "
      "`logincount`, `experience`, `next_xp`, `level`, `avatarurl`, "
      "`avatartime`, `idletimeout`, `floodreduce`, `floodmax`, `maxbuffer`, "
      "`strength`, `intelligence`, `accuracy`, `avoid`, `melee`, `archery`, "
      "`cast`, `pick_lock`, `spot_trap`, `disarm_trap` "
      ") VALUES "
      "('%lu', '%s', '%s', '%lu', '%lu','%s', '', '', '', '%lu', '%lu', '%lu', '%lu', "
      "'%lu', '%lu', '%lu', '%lu', '%lu','%lu', '0', '0', '0', '0', '0', '0', '', '0', "
      "'0', '0', '0','0', '1', '%lu', '%lu', '1', '', '0', '%lu', '%lu', '%lu', '%lu', "
      "'%lu', '%lu', '%lu', '%lu', '%lu', '%lu', '%lu', '%lu', '%lu', '%lu');",

   // Do the query //
   if(mysql_query(m_pSQL,szBuff) != 0)
      return false;

   // Add to internal user list and return //
   m_mapUsers[pInternalUser->m_szUsername] = pInternalUser;
   return true;

You can probably just ignore the vast INSERT statement :P And before anyone says anything, Yes, I know I should be using prepared statements. But the version of MySQL that I have doesn't support them, and I can't get PHP working with the newer version.

Share this post

Link to post
Share on other sites
Sounds like a connection timeout issue. I had it happen with MySQL when there was no activity for a long time. The way to solve this is to have activity. Find out what your connection timeout is set, then query the server every once in a while with some trivial request (a heartbeat of sorts).

With a MUD that is relatively unpopulated you may have no activity for a while so the best thing that I have added to my MUD is a dynamic connection pool that establishes a connection to the DB if there are none during login process and keeps the connection floating around for 30 minutes (reusing them as needed), if inactive for over 30 mins it closes the connection.

Share this post

Link to post
Share on other sites
im not sure about LibMySQL, since i use MySQL++, however there should be some sort of ping() function. i call it once an hour just to be safe, but thats only because i dont know what the timeout value really is. if you find out, let me know [grin].

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!