summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-06-11 18:40:49 +0200
committerSimon Rettberg2015-06-11 18:40:49 +0200
commite0005ceecfd9281230c4add7575b18ee88307774 (patch)
treea73bbcfc213df478c701aac120ae2b7c6e52bb1b /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java
parent[server] db stuff, new interface, ... (diff)
downloadtutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.gz
tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.xz
tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.zip
[server] On mah way (lots of restructuring, some early db classes, sql dump of current schema)
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java
new file mode 100644
index 00000000..24aaf1e8
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/MysqlConnection.java
@@ -0,0 +1,77 @@
+package org.openslx.bwlp.sat.database;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+public class MysqlConnection implements AutoCloseable {
+
+ private static final Logger LOGGER = Logger.getLogger(MysqlConnection.class);
+
+ private static final int CONNECTION_TIMEOUT_MS = 5 * 60 * 1000;
+
+ private final long deadline = System.currentTimeMillis() + CONNECTION_TIMEOUT_MS;
+
+ private final Connection rawConnection;
+
+ private boolean hasPendingQueries = false;
+
+ private List<MysqlStatement> openStatements = new ArrayList<>();
+
+ MysqlConnection(Connection rawConnection) {
+ this.rawConnection = rawConnection;
+ }
+
+ public MysqlStatement prepareStatement(String sql) throws SQLException {
+ if (!sql.startsWith("SELECT"))
+ hasPendingQueries = true;
+ MysqlStatement statement = new MysqlStatement(rawConnection, sql);
+ openStatements.add(statement);
+ return statement;
+ }
+
+ public void commit() throws SQLException {
+ rawConnection.commit();
+ hasPendingQueries = false;
+ }
+
+ public void rollback() throws SQLException {
+ rawConnection.rollback();
+ hasPendingQueries = false;
+ }
+
+ boolean isValid() {
+ return System.currentTimeMillis() < deadline;
+ }
+
+ @Override
+ public void close() {
+ if (hasPendingQueries) {
+ LOGGER.warn("Mysql connection had uncommited queries on .close()");
+ try {
+ rawConnection.rollback();
+ } catch (SQLException e) {
+ LOGGER.warn("Rolling back uncommited queries failed!", e);
+ }
+ }
+ if (!openStatements.isEmpty()) {
+ for (MysqlStatement statement : openStatements) {
+ statement.close();
+ }
+ openStatements.clear();
+ }
+ Database.returnConnection(this);
+ }
+
+ void release() {
+ try {
+ rawConnection.close();
+ } catch (SQLException e) {
+ // Nothing meaningful to do
+ }
+ }
+
+}