X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=b52b63debeef44748860b861fb63a4f558a01ad4;hb=c65ba6025305b2c4eb238f19fe14e9ef9fd0d50b;hp=9f9193a7ad0f2fedafaa45fccb415fd22e33b8d2;hpb=c6ea5080ed8b31fb9844e2fad80b352d6a80bdf1;p=gigi.git diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index 9f9193a7..b52b63de 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -15,6 +15,7 @@ public class DatabaseConnection { HashMap statements = new HashMap(); private static Properties credentials; Statement adHoc; + public DatabaseConnection() { try { Class.forName(credentials.getProperty("sql.driver")); @@ -24,14 +25,12 @@ public class DatabaseConnection { tryConnect(); } + private void tryConnect() { try { - c = DriverManager.getConnection(credentials.getProperty("sql.url") - + "?zeroDateTimeBehavior=convertToNull", - credentials.getProperty("sql.user"), - credentials.getProperty("sql.password")); - PreparedStatement ps = c - .prepareStatement("SET SESSION wait_timeout=?;"); + c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull", + 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(); @@ -40,16 +39,19 @@ public class DatabaseConnection { e.printStackTrace(); } } + public PreparedStatement prepare(String query) throws SQLException { ensureOpen(); PreparedStatement statement = statements.get(query); if (statement == null) { - statement = c.prepareStatement(query); + statement = c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); statements.put(query, statement); } return statement; } + long lastAction = System.currentTimeMillis(); + private void ensureOpen() { if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) { try { @@ -64,6 +66,7 @@ public class DatabaseConnection { } lastAction = System.currentTimeMillis(); } + public static int lastInsertId(PreparedStatement query) throws SQLException { ResultSet rs = query.getGeneratedKeys(); rs.next(); @@ -71,19 +74,46 @@ public class DatabaseConnection { rs.close(); return id; } + static ThreadLocal instances = new ThreadLocal() { @Override protected DatabaseConnection initialValue() { return new DatabaseConnection(); } }; + public static DatabaseConnection getInstance() { return instances.get(); } + + public static boolean isInited() { + return credentials != null; + } + 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(); + } + } }