From ae5a5fa68afc3749fe0b193c4a919f04ad692be7 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Thu, 30 Oct 2014 19:24:17 +0100
Subject: [PATCH] Allow configurable auto commit

---
 .../mi/sda2/account/TransferDriver.java        |  2 +-
 .../mi/sda2/account/sql/DbHandler.java         | 18 +++++++++++-------
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/TransferDriver.java b/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/TransferDriver.java
index 13abde372..3e5b85117 100644
--- a/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/TransferDriver.java
+++ b/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/TransferDriver.java
@@ -70,7 +70,7 @@ public class TransferDriver extends Application {
     @Override
     public void start(final Stage primaryStage) throws SQLException {
     	
-    	dbh.connect(Connection.TRANSACTION_SERIALIZABLE);
+    	dbh.connect(Connection.TRANSACTION_SERIALIZABLE, false);
 
 
         primaryStage.setTitle("Account transfer!");
diff --git a/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/sql/DbHandler.java b/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/sql/DbHandler.java
index 4807fa16a..5111f6c76 100644
--- a/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/sql/DbHandler.java
+++ b/Sda2/P/account/src/main/java/de/hdm_stuttgart/mi/sda2/account/sql/DbHandler.java
@@ -15,19 +15,21 @@ public class DbHandler {
 	static private final Logger log = Logger.getLogger(DbHandler.class);
 	
 	Connection conn = null;
+	boolean autoCommit;
 	PreparedStatement updateAccount; 
 
 	private String getConnectionName() {
 		return Conf.get("DbHandler.user") + '@' + Conf.get("DbHandler.jdbcUrl");
 	}
 	
-	public boolean connect(final int isolationLevel) {
+	public boolean connect(final int isolationLevel, boolean autoCommit) {
+		this.autoCommit = autoCommit;
 		try {
 			conn = DriverManager.getConnection(
 					Conf.get("DbHandler.jdbcUrl"),
 					Conf.get("DbHandler.user"),
 					Conf.get("DbHandler.password"));
-			conn.setAutoCommit(false);
+			conn.setAutoCommit(autoCommit);
 			conn.setTransactionIsolation(isolationLevel);
 			updateAccount = conn.prepareStatement("UPDATE Account SET balance = balance + ? WHERE number = ?");
 			log.info("Connection '" + getConnectionName() + "' established" ); 
@@ -57,11 +59,13 @@ public class DbHandler {
 	}
 	
 	public void commit() {
-		try {
-			conn.commit();
-			log.info("Changes successfully committed");
-		} catch (SQLException e) {
-			ExceptionDialog.showExceptionAndExit("Committing transaction failed: ", e, 1);
+		if (!autoCommit) {
+			try {
+				conn.commit();
+				log.info("Changes successfully committed");
+			} catch (SQLException e) {
+				ExceptionDialog.showExceptionAndExit("Committing transaction failed: ", e, 1);
+			}
 		}
 	}
 	
-- 
GitLab