]> git.mxchange.org Git - addressbook-swing.git/commitdiff
Having one database connection for all tables is more efficient
authorRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 13:19:08 +0000 (15:19 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 13:20:04 +0000 (15:20 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java

index 900d5a8948d329f00f867e2fd3f6bcee89080242..212a78e823096100fe3ef3af8b2a0d9ae6c654dc 100644 (file)
@@ -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);
                }