]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/database/DatabaseConnection.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / database / DatabaseConnection.java
index 36b702114ca56316edc961a283bb6f6d38d881f0..6bed8bd979c2f62aec1b7922779bed3048716063 100644 (file)
@@ -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<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
-       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();
+               }
+       }
 }