]> WPIA git - gigi.git/blob - src/org/cacert/gigi/database/DatabaseConnection.java
[EMPTY] Formatting with configured formatter.
[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
19         public DatabaseConnection() {
20                 try {
21                         Class.forName(credentials.getProperty("sql.driver"));
22                 } catch (ClassNotFoundException e) {
23                         e.printStackTrace();
24                 }
25                 tryConnect();
26
27         }
28
29         private void tryConnect() {
30                 try {
31                         c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull",
32                                 credentials.getProperty("sql.user"), credentials.getProperty("sql.password"));
33                         PreparedStatement ps = c.prepareStatement("SET SESSION wait_timeout=?;");
34                         ps.setInt(1, CONNECTION_TIMEOUT);
35                         ps.execute();
36                         ps.close();
37                         adHoc = c.createStatement();
38                 } catch (SQLException e) {
39                         e.printStackTrace();
40                 }
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, Statement.RETURN_GENERATED_KEYS);
48                         statements.put(query, statement);
49                 }
50                 return statement;
51         }
52
53         long lastAction = System.currentTimeMillis();
54
55         private void ensureOpen() {
56                 if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) {
57                         try {
58                                 ResultSet rs = adHoc.executeQuery("SELECT 1");
59                                 rs.close();
60                                 lastAction = System.currentTimeMillis();
61                                 return;
62                         } catch (SQLException e) {
63                         }
64                         statements.clear();
65                         tryConnect();
66                 }
67                 lastAction = System.currentTimeMillis();
68         }
69
70         public static int lastInsertId(PreparedStatement query) throws SQLException {
71                 ResultSet rs = query.getGeneratedKeys();
72                 rs.next();
73                 int id = rs.getInt(1);
74                 rs.close();
75                 return id;
76         }
77
78         static ThreadLocal<DatabaseConnection> instances = new ThreadLocal<DatabaseConnection>() {
79                 @Override
80                 protected DatabaseConnection initialValue() {
81                         return new DatabaseConnection();
82                 }
83         };
84
85         public static DatabaseConnection getInstance() {
86                 return instances.get();
87         }
88
89         public static boolean isInited() {
90                 return credentials != null;
91         }
92
93         public static void init(Properties conf) {
94                 if (credentials != null) {
95                         throw new Error("Re-initiaizing is forbidden.");
96                 }
97                 credentials = conf;
98         }
99
100         public void beginTransaction() throws SQLException {
101                 c.setAutoCommit(false);
102         }
103
104         public void commitTransaction() throws SQLException {
105                 c.commit();
106                 c.setAutoCommit(true);
107         }
108
109         public void quitTransaction() {
110                 try {
111                         if (!c.getAutoCommit()) {
112                                 c.rollback();
113                                 c.setAutoCommit(true);
114                         }
115                 } catch (SQLException e) {
116                         e.printStackTrace();
117                 }
118         }
119 }