1 package org.cacert.gigi.database;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.sql.Connection;
6 import java.sql.DriverManager;
7 import java.sql.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.util.HashMap;
12 import java.util.Properties;
14 import org.cacert.gigi.database.SQLFileManager.ImportType;
16 public class DatabaseConnection {
18 public static final int CURRENT_SCHEMA_VERSION = 1;
20 public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
24 private HashMap<String, GigiPreparedStatement> statements = new HashMap<String, GigiPreparedStatement>();
26 private static Properties credentials;
28 private Statement adHoc;
30 public DatabaseConnection() {
32 Class.forName(credentials.getProperty("sql.driver"));
33 } catch (ClassNotFoundException e) {
40 private void tryConnect() {
42 c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull", credentials.getProperty("sql.user"), credentials.getProperty("sql.password"));
43 PreparedStatement ps = c.prepareStatement("SET SESSION wait_timeout=?, time_zone='+0:00';");
45 ps.setInt(1, CONNECTION_TIMEOUT);
47 adHoc = c.createStatement();
51 } catch (SQLException e) {
56 public GigiPreparedStatement prepare(String query) {
58 GigiPreparedStatement statement = statements.get(query);
59 if (statement == null) {
61 statement = new GigiPreparedStatement(c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS));
62 } catch (SQLException e) {
65 statements.put(query, statement);
70 private long lastAction = System.currentTimeMillis();
72 private void ensureOpen() {
73 if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) {
75 ResultSet rs = adHoc.executeQuery("SELECT 1");
77 lastAction = System.currentTimeMillis();
79 } catch (SQLException e) {
84 lastAction = System.currentTimeMillis();
87 private static ThreadLocal<DatabaseConnection> instances = new ThreadLocal<DatabaseConnection>() {
90 protected DatabaseConnection initialValue() {
91 return new DatabaseConnection();
95 public static DatabaseConnection getInstance() {
96 return instances.get();
99 public static boolean isInited() {
100 return credentials != null;
103 public static void init(Properties conf) {
104 if (credentials != null) {
105 throw new Error("Re-initiaizing is forbidden.");
108 GigiResultSet rs = getInstance().prepare("SELECT version FROM schemeVersion ORDER BY version DESC LIMIT 1").executeQuery();
111 version = rs.getInt(1);
113 if (version == CURRENT_SCHEMA_VERSION) {
114 return; // Good to go
116 if (version > CURRENT_SCHEMA_VERSION) {
117 throw new Error("Invalid database version. Please fix this.");
122 private static void upgrade(int version) {
124 Statement s = getInstance().c.createStatement();
126 while (version < CURRENT_SCHEMA_VERSION) {
127 try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) {
128 if (resourceAsStream == null) {
129 throw new Error("Upgrade script from version " + version + " was not found.");
131 SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION);
135 s.addBatch("INSERT INTO schemeVersion SET version='" + version + "'");
136 System.out.println("UPGRADING Database to version " + version);
138 System.out.println("done.");
142 } catch (SQLException e) {
144 } catch (IOException e) {
149 public void beginTransaction() throws SQLException {
150 c.setAutoCommit(false);
153 public void commitTransaction() throws SQLException {
155 c.setAutoCommit(true);
158 public void quitTransaction() {
160 if ( !c.getAutoCommit()) {
162 c.setAutoCommit(true);
164 } catch (SQLException e) {