X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=6bed8bd979c2f62aec1b7922779bed3048716063;hb=35733d6e3c2b8706de5739d3d1ef6cc93a8ea5f6;hp=36b702114ca56316edc961a283bb6f6d38d881f0;hpb=49b403316469afb4f62c5b60401667640746af37;p=gigi.git diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index 36b70211..6bed8bd9 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; @@ -15,18 +13,11 @@ 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(); } @@ -35,10 +26,10 @@ public class DatabaseConnection { } 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); @@ -53,7 +44,8 @@ public class DatabaseConnection { 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; @@ -89,4 +81,30 @@ public class 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(); + } + } }