]> jfr.im git - irc/rizon/acid.git/commitdiff
Periodically ping the database since this autoReconnect parameter doesn't seem to...
authorAdam <redacted>
Sat, 29 Aug 2015 15:13:28 +0000 (11:13 -0400)
committerAdam <redacted>
Sat, 29 Aug 2015 15:13:28 +0000 (11:13 -0400)
acid/acidictive.example.yml
acid/src/main/java/net/rizon/acid/sql/SQL.java

index df58e592ea88bb156d9d5c6b03aa4f1d29dc0426..f935915538a2973d977e9da9d524ae0bc19778f6 100644 (file)
@@ -77,7 +77,7 @@ general:
 database:
  -
   name: acidcore
-  host: jdbc:mysql://192.168.1.2:3306/acidcore?autoReconnect=true
+  host: jdbc:mysql://192.168.1.2:3306/acidcore
   user: adam
   pass: moo
 
index 5060726343c2704fb760e5ed9aa3b7b5decba1e5..4fcb8d1307abd8b568af1819372722423e3e03f2 100644 (file)
@@ -165,6 +165,7 @@ public class SQL extends Thread
        @Override
        public void run()
        {
+               long lastQuery = System.currentTimeMillis();
                while (true)
                {
                        PreparedStatement q = null;
@@ -180,7 +181,7 @@ public class SQL extends Thread
 
                                        try
                                        {
-                                               this.queryLock.wait();
+                                               this.queryLock.wait(60 * 1000L);
                                        }
                                        catch (InterruptedException e) { }
                                }
@@ -191,6 +192,7 @@ public class SQL extends Thread
                                try
                                {
                                        q.executeUpdate();
+                                       lastQuery = System.currentTimeMillis();
 
                                        log.log(Level.FINE, "Successfully executed " + q + " in worker thread");
                                }
@@ -201,6 +203,20 @@ public class SQL extends Thread
 
                                this.close(q, null);
                        }
+                       
+                       if (System.currentTimeMillis() - lastQuery > 60 * 1000)
+                       {
+                               lastQuery = System.currentTimeMillis();
+                               
+                               try (PreparedStatement stmt = this.con.prepareStatement("SELECT 1"))
+                               {
+                                       stmt.execute();
+                               }
+                               catch (SQLException ex)
+                               {
+                                       ex.printStackTrace();
+                               }
+                       }
                }
        }
 
@@ -217,8 +233,6 @@ public class SQL extends Thread
 
        public static SQL getConnection(final String name)
        {
-               SQL sql = null;
-
                try
                {
                        for (Database d : Acidictive.conf.database)
@@ -226,7 +240,7 @@ public class SQL extends Thread
                                if (!d.name.equals(name))
                                        continue;
 
-                               return sql = new SQL(d.host, d.user, d.pass);
+                               return new SQL(d.host, d.user, d.pass);
                        }
                }
                catch (ClassNotFoundException ex)
@@ -238,6 +252,6 @@ public class SQL extends Thread
                        throw new RuntimeException(e.getMessage());
                }
 
-               return sql;
+               return null;
        }
 }