From: Roland Haeder Date: Wed, 29 Jul 2015 13:19:08 +0000 (+0200) Subject: Having one database connection for all tables is more efficient X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=15d31fb3c878ba93a7a8c2ce1d2ab3b16abf320f;p=jfinancials-swing.git Having one database connection for all tables is more efficient Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java index 900d5a8..212a78e 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java @@ -38,7 +38,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas /** * An instance of a datbase connection */ - private Connection connection; + private static Connection connection; /** * Constructor with table name @@ -59,6 +59,15 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas // Trace message this.getLogger().trace("CALLED!"); //NOI18N + // Is the connection already there? + if (MySqlDatabaseBackend.connection instanceof Connection) { + // Already connected + this.getLogger().debug("Connection is already established."); //NOI18N + + // No need to connect + return; + } + // Generate connection string String connect = String.format("jdbc:mysql://%s/%s", //NOI18N this.getProperty("org.mxchange.addressbook.database.mysql.host"), //NOI18N @@ -69,7 +78,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N // Now get a connection instance back - this.connection = DriverManager.getConnection( + MySqlDatabaseBackend.connection = DriverManager.getConnection( connect, this.getProperty("org.mxchange.addressbook.database.mysql.login"), //NOI18N this.getProperty("org.mxchange.addressbook.database.mysql.password") //NOI18N @@ -87,11 +96,11 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas @Override public void doShutdown () { // This should not happen: - assert(this.connection == null) : "connection is null"; + assert(MySqlDatabaseBackend.connection == null) : "connection is null"; //NOI18N try { // Close down database connection - this.connection.close(); + MySqlDatabaseBackend.connection.close(); } catch (final SQLException ex) { this.abortProgramWithException(ex); }