]> WPIA git - gigi.git/blob - src/org/cacert/gigi/database/DatabaseConnection.java
Changing GigiConfig Exchange format to tar.
[gigi.git] / src / org / cacert / gigi / database / DatabaseConnection.java
1 package org.cacert.gigi.database;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.HashMap;
9 import java.util.Properties;
10 import java.sql.Statement;
11
12 public class DatabaseConnection {
13         public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
14         Connection c;
15         HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
16         private static Properties credentials;
17         Statement adHoc;
18         public DatabaseConnection() {
19                 try {
20                         Class.forName(credentials.getProperty("sql.driver"));
21                 } catch (ClassNotFoundException e) {
22                         e.printStackTrace();
23                 }
24                 tryConnect();
25
26         }
27         private void tryConnect() {
28                 try {
29                         c = DriverManager.getConnection(credentials.getProperty("sql.url")
30                                         + "?zeroDateTimeBehavior=convertToNull",
31                                         credentials.getProperty("sql.user"),
32                                         credentials.getProperty("sql.password"));
33                         PreparedStatement ps = c
34                                         .prepareStatement("SET SESSION wait_timeout=?;");
35                         ps.setInt(1, CONNECTION_TIMEOUT);
36                         ps.execute();
37                         ps.close();
38                         adHoc = c.createStatement();
39                 } catch (SQLException e) {
40                         e.printStackTrace();
41                 }
42         }
43         public PreparedStatement prepare(String query) throws SQLException {
44                 ensureOpen();
45                 PreparedStatement statement = statements.get(query);
46                 if (statement == null) {
47                         statement = c.prepareStatement(query);
48                         statements.put(query, statement);
49                 }
50                 return statement;
51         }
52         long lastAction = System.currentTimeMillis();
53         private void ensureOpen() {
54                 if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) {
55                         try {
56                                 ResultSet rs = adHoc.executeQuery("SELECT 1");
57                                 rs.close();
58                                 lastAction = System.currentTimeMillis();
59                                 return;
60                         } catch (SQLException e) {
61                         }
62                         statements.clear();
63                         tryConnect();
64                 }
65                 lastAction = System.currentTimeMillis();
66         }
67         public static int lastInsertId(PreparedStatement query) throws SQLException {
68                 ResultSet rs = query.getGeneratedKeys();
69                 rs.next();
70                 int id = rs.getInt(1);
71                 rs.close();
72                 return id;
73         }
74         static ThreadLocal<DatabaseConnection> instances = new ThreadLocal<DatabaseConnection>() {
75                 @Override
76                 protected DatabaseConnection initialValue() {
77                         return new DatabaseConnection();
78                 }
79         };
80         public static DatabaseConnection getInstance() {
81                 return instances.get();
82         }
83         public static void init(Properties conf) {
84                 if (credentials != null) {
85                         throw new Error("Re-initiaizing is forbidden.");
86                 }
87                 credentials = conf;
88         }
89 }