From 15d31fb3c878ba93a7a8c2ce1d2ab3b16abf320f Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 29 Jul 2015 15:19:08 +0200 Subject: [PATCH] =?utf8?q?Having=20one=20database=20connection=20for=20all?= =?utf8?q?=20tables=20is=20more=20efficient=20Signed-off-by:Roland=20H?= =?utf8?q?=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../backend/mysql/MySqlDatabaseBackend.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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 900d5a89..212a78e8 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); } -- 2.39.5