Fix for OdinMS database crash

Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Infraction Banned whoopdedoop is offline
    MemberRank
    May 2010 Join Date
    4Posts

    Fix for OdinMS database crash

    how this works:
    - at startup, load the user, pass and url for database
    - set a default timeout (5 minutes)
    - connect to db and send a request for wait_timeout
    - set timeout to returned timeout
    - when the method getconnection is called, the system validates the connection before returning

    Code:
    public class DatabaseConnection {
    	private static final Logger log = LoggerFactory.getErrLogger();
    	private static final HashMap<Integer, ConWrapper> connections =
    			new HashMap();
        private static String dbDriver, dbUrl, dbUser, dbPass;
    	private static boolean propsInited = false;
    	private static long connectionTimeOut = 5 * 60 * 1000; // 5 minutes
    
        private DatabaseConnection() {}
    
        public static Connection getConnection() {
    		Thread cThread = Thread.currentThread();
    		int threadID = (int) cThread.getId();
    		ConWrapper ret = connections.get(threadID);
    
    		if (ret == null) {
    			Connection retCon = connectToDB();
    			ret = new ConWrapper(retCon);
    			ret.id = threadID;
    			connections.put(threadID, ret);
    			log.info("[Database] Thread [" + threadID + "] has created a new Database Connection.");
    		}
    
    		return ret.getConnection();
        }
    
    	private static long getWaitTimeout(Connection con) {
    		Statement stmt = null;
    		ResultSet rs = null;
    		try {
    			stmt = con.createStatement();
    			rs = stmt.executeQuery("SHOW VARIABLES LIKE 'wait_timeout'");
    			if (rs.next()) {
    				return Math.max(1000, rs.getInt(2) * 1000 - 1000);
    			} else {
    				return -1;
    			}
    		} catch (SQLException ex) {
    			return -1;
    		} finally {
    			if (stmt != null) {
    				try {
    					stmt.close();
    				} catch (SQLException ex) {
    				} finally {
    					if (rs != null) {
    						try {
    							rs.close();
    						} catch (SQLException ex1) {}
    					}
    				}
    			}
    		}
    	}
    
    	private static Connection connectToDB() {
    		if (!propsInited) {
    			PropertyReader dbReader;
    
    			try {
    				dbReader = PropertyReader.load("db.cfg");
    			} catch (IOException ex) {
    				throw new DatabaseException(ex);
    			}
    
    			dbDriver = dbReader.getProperty("driver");
    			dbUrl = dbReader.getProperty("url");
    			dbUser = dbReader.getProperty("user");
    			dbPass = dbReader.getProperty("password");
    			try {
    				connectionTimeOut = Long.parseLong(dbReader.getProperty("timeout"));
    			} catch (Exception e) {
    				log.warn("[Database] Cannot read Connection Timeout Information, using default: " + connectionTimeOut);
    			}
    		}
    
    		try {
    			Class.forName(dbDriver);    // touch the MySQL driver
    		} catch (ClassNotFoundException e) {
    			throw new DatabaseException(e);
    		}
    
    		try {
    			Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
    			if (!propsInited) {
    				long timeout = getWaitTimeout(con);
    				if (timeout == -1) {
    					log.error("[Database] Failed to read Wait_Timeout, using " + connectionTimeOut + " instead.");
    				} else {
    					connectionTimeOut = timeout;
    					log.info("[Database] Timeout is " + connectionTimeOut + " milliseconds.");
    				}
    				propsInited = true;
    			}
    			return con;
    		} catch (SQLException e) {
    			throw new DatabaseException(e);
    		}
    	}
    
    	static class ConWrapper {
    		private long lastAccessTime = 0;
    		private Connection connection;
    		private int id;
    
    		public ConWrapper(Connection con) {
    			this.connection = con;
    		}
    
    		public Connection getConnection() {
    			if (expiredConnection()) {
    				log.info("[Database] Connection #" + id + " has gone stale. Reconnecting..");
    				try { // Assume that the connection is stale
    					connection.close();
    				} catch (Throwable err) {
    					// Who cares
    				}
    				this.connection = connectToDB();
    			}
    
    			lastAccessTime = System.currentTimeMillis(); // Record Access
    			return this.connection;
    		}
    
    		/**
    		 * Returns whether this connection has expired
    		 * @return
    		 */
    		public boolean expiredConnection() {
    			if (lastAccessTime == 0) {
    				return false;
    			}
    			try {
    				return System.currentTimeMillis() - lastAccessTime >= connectionTimeOut || connection.isClosed();
    			} catch (Throwable ex) {
    				return true;
    			}
    		}
    	}
    
        public static void closeAll() throws SQLException {
            for (ConWrapper con : connections.values()) {
                con.connection.close();
            }
    		connections.clear();
        }
    }
    have fun..

    edit: stop complaining about the import problems. if you can't fix it so there are no errors, you probably shouldnt be using it
    Last edited by whoopdedoop; 22-05-10 at 06:10 PM.


  2. #2
    win CioNide is offline
    MemberRank
    Jun 2008 Join Date
    2,560Posts

    Re: Fix for OdinMS database crash

    Didn't see any exploits.
    Looks good, what exactly would this be used for?

    I've never heard of MySQL DB crashing.

  3. #3
    Infraction Banned whoopdedoop is offline
    MemberRank
    May 2010 Join Date
    4Posts

    Re: Fix for OdinMS database crash

    this fixes the need to use ?autoReconnect=true in the database file, meaning servers can now stay up for months without the database connection timing out, as opposed to hours before.

  4. #4
    win CioNide is offline
    MemberRank
    Jun 2008 Join Date
    2,560Posts

    Re: Fix for OdinMS database crash

    So it's just an modified DatabaseConnection.java?
    :3

  5. #5
    Infraction Banned whoopdedoop is offline
    MemberRank
    May 2010 Join Date
    4Posts

    Re: Fix for OdinMS database crash

    Quote Originally Posted by CioNide View Post
    So it's just an modified DatabaseConnection.java?
    :3
    this is the same file that renoria's server uses, I just released it to help the ms community after being down in shit for so long, and of course i wouldnt put exploits in it, i have no interest in maplestory private servers.

  6. #6
    Ooo, shiny! FateJiki is offline
    MemberRank
    Feb 2008 Join Date
    1,057Posts

    Re: Fix for OdinMS database crash

    Quote Originally Posted by CioNide View Post
    So it's just an modified DatabaseConnection.java?
    :3
    Read the class name and there's your answer.

    Basically what this does is that normally threads would time-out on the MySQL Server , thus eventually crashing the entire SQL(They build up over time). If I read correctly, this should remove any un-used active threads, without manually having to kill them or add a wait_timeout.

  7. #7
    Infraction Banned whoopdedoop is offline
    MemberRank
    May 2010 Join Date
    4Posts

    Re: Fix for OdinMS database crash

    Quote Originally Posted by FateJiki View Post
    Read the class name and there's your answer.

    Basically what this does is that normally threads would time-out on the MySQL Server , thus eventually crashing the entire SQL(They build up over time). If I read correctly, this should remove any un-used active threads, without manually having to kill them or add a wait_timeout.
    no you're wrong, this system restarts any threads that did time out before returning the pointer to the active connection, in other words it makes sure the conneciton is valid before returning it

  8. #8
    Ooo, shiny! FateJiki is offline
    MemberRank
    Feb 2008 Join Date
    1,057Posts

    Re: Fix for OdinMS database crash

    Quote Originally Posted by whoopdedoop View Post
    no you're wrong, this system restarts any threads that did time out before returning the pointer to the active connection, in other words it makes sure the conneciton is valid before returning it
    Understood.

  9. #9
    offonline King Grub is offline
    MemberRank
    Aug 2009 Join Date
    Spring fieldLocation
    3,303Posts

    Re: Fix for OdinMS database crash

    Thank you David

    I just released it to help the ms community
    I really appreciate it and i'm sure others do too.

  10. #10
    Infraction Banned rice is offline
    MemberRank
    Nov 2009 Join Date
    2,905Posts

    Re: Fix for OdinMS database crash

    Wow, a good release.

  11. #11
    Account Upgraded | Title Enabled! LightPepsi is offline
    MemberRank
    Sep 2009 Join Date
    270Posts

    Re: Fix for OdinMS database crash

    opps....
    Last edited by LightPepsi; 22-05-10 at 07:35 PM.

  12. #12
    Member iChappie is offline
    MemberRank
    May 2010 Join Date
    73Posts

    Re: Fix for OdinMS database crash

    Quote Originally Posted by LightPepsi View Post
    leeched, this is released months ago.
    Got proof?

  13. #13
    Account Upgraded | Title Enabled! wietse02 is offline
    MemberRank
    Jul 2008 Join Date
    NetherlandsLocation
    657Posts

    Re: Fix for OdinMS database crash

    Thank you!

  14. #14
    ✞ Godlike ✞ Sjogern93 is offline
    MemberRank
    Jul 2008 Join Date
    SAEVO ♡Location
    792Posts

    Re: Fix for OdinMS database crash

    LightPepsi shut up. Don't bash anything into his face without providing some kind of proof at the same time. Your opinion doesn't matter without proof anyway.

    Good release, won't this remove some minor lag issues? Seeing if the old method just stacks timeouts, that would create lag, correct?

  15. #15
    Valued Member bagger is offline
    MemberRank
    May 2009 Join Date
    116Posts

    Re: Fix for OdinMS database crash

    thx for this... hopefully this will solve most or at least a lot of the database problems hehe



Page 1 of 2 12 LastLast

Advertisement