public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
Connection c;
HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
- private static Properties credentials = new Properties();
+ private static Properties credentials;
Statement adHoc;
public DatabaseConnection() {
try {
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;
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();
+ }
+ }
}