Split commit and returnToPool methods into proper classes

This commit is contained in:
Rsl1122 2018-07-30 16:28:56 +03:00
parent a93ffaca7e
commit 5d5fe7c452
3 changed files with 35 additions and 30 deletions

View File

@ -79,6 +79,22 @@ public class MySQLDB extends SQLDB {
super.close();
}
@Override
public void returnToPool(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
Log.toLog(this.getClass(), e);
}
}
@Override
public void commit(Connection connection) {
returnToPool(connection);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -90,7 +106,6 @@ public class MySQLDB extends SQLDB {
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), dataSource);
}
}

View File

@ -69,7 +69,7 @@ public abstract class SQLDB extends Database {
public SQLDB(Supplier<Locale> locale) {
this.locale = locale;
usingMySQL = getName().equals("MySQL");
usingMySQL = this instanceof MySQLDB;
serverTable = new ServerTable(this);
securityTable = new SecurityTable(this);
@ -260,34 +260,9 @@ public abstract class SQLDB extends Database {
public abstract Connection getConnection() throws SQLException;
/**
* Commits changes to the .db file when using SQLite Database.
* <p>
* MySQL has Auto Commit enabled.
*/
public void commit(Connection connection) {
try {
if (!usingMySQL) {
connection.commit();
}
} catch (SQLException e) {
if (!e.getMessage().contains("cannot commit")) {
Log.toLog(this.getClass(), e);
}
} finally {
returnToPool(connection);
}
}
public abstract void commit(Connection connection);
public void returnToPool(Connection connection) {
try {
if (usingMySQL && connection != null) {
connection.close();
}
} catch (SQLException e) {
Log.toLog(this.getClass(), e);
}
}
public abstract void returnToPool(Connection connection);
/**
* Reverts transaction when using SQLite Database.

View File

@ -141,6 +141,22 @@ public class SQLiteDB extends SQLDB {
super.close();
}
@Override
public void commit(Connection connection) {
try {
connection.commit();
} catch (SQLException e) {
if (!e.getMessage().contains("cannot commit")) {
Log.toLog(this.getClass(), e);
}
}
}
@Override
public void returnToPool(Connection connection) {
// Connection pool not in use, no action required.
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -152,7 +168,6 @@ public class SQLiteDB extends SQLDB {
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), dbName);
}
}