X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=9cbcaeb5140fc58487d4602949942634166e123f;hb=093f4797bc1e84c4963a67093f2b21393656edab;hp=c1eed41b5391d6ff18e75faf3fd5a916137aeb6b;hpb=ea6ee43a84f9a1f055d97ff0de8196569154e4d0;p=gigi.git diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index c1eed41b..9cbcaeb5 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -1,7 +1,5 @@ package org.cacert.gigi.database; -import java.io.FileInputStream; -import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -9,35 +7,41 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Properties; +import java.sql.Statement; public class DatabaseConnection { + public static final int CONNECTION_TIMEOUT = 24 * 60 * 60; Connection c; HashMap statements = new HashMap(); - static Properties credentials = new Properties(); - static { - try { - credentials.load(new FileInputStream("config/sql.properties")); - } catch (IOException e) { - e.printStackTrace(); - } - } + private static Properties credentials; + Statement adHoc; public DatabaseConnection() { try { - Class.forName(credentials.getProperty("driver")); + Class.forName(credentials.getProperty("sql.driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } + tryConnect(); + + } + private void tryConnect() { try { - c = DriverManager.getConnection(credentials.getProperty("url") + c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull", - credentials.getProperty("user"), - credentials.getProperty("password")); + credentials.getProperty("sql.user"), + credentials.getProperty("sql.password")); + PreparedStatement ps = c + .prepareStatement("SET SESSION wait_timeout=?;"); + ps.setInt(1, CONNECTION_TIMEOUT); + ps.execute(); + ps.close(); + adHoc = c.createStatement(); } catch (SQLException e) { e.printStackTrace(); } - } public PreparedStatement prepare(String query) throws SQLException { + ensureOpen(); PreparedStatement statement = statements.get(query); if (statement == null) { statement = c.prepareStatement(query); @@ -45,7 +49,21 @@ public class DatabaseConnection { } return statement; } - + long lastAction = System.currentTimeMillis(); + private void ensureOpen() { + if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) { + try { + ResultSet rs = adHoc.executeQuery("SELECT 1"); + rs.close(); + lastAction = System.currentTimeMillis(); + return; + } catch (SQLException e) { + } + statements.clear(); + tryConnect(); + } + lastAction = System.currentTimeMillis(); + } public static int lastInsertId(PreparedStatement query) throws SQLException { ResultSet rs = query.getGeneratedKeys(); rs.next(); @@ -62,4 +80,27 @@ public class DatabaseConnection { public static DatabaseConnection getInstance() { return instances.get(); } + public static void init(Properties conf) { + if (credentials != null) { + throw new Error("Re-initiaizing is forbidden."); + } + credentials = conf; + } + public void beginTransaction() throws SQLException { + c.setAutoCommit(false); + } + public void commitTransaction() throws SQLException { + c.commit(); + c.setAutoCommit(true); + } + public void quitTransaction() { + try { + if (!c.getAutoCommit()) { + c.rollback(); + c.setAutoCommit(true); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } }