Skip to content
Snippets Groups Projects
Commit ae5a5fa6 authored by Goik Martin's avatar Goik Martin
Browse files

Allow configurable auto commit

parent 07e13305
No related branches found
No related tags found
Loading
......@@ -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!");
......
......@@ -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);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment