Replaced MSqlConnectionGenerator with MSqlConnectionPool.
authorMarco Zanon <info@marcozanon.com>
Thu, 29 Oct 2015 13:54:40 +0000 (13:54 +0000)
committerMarco Zanon <info@marcozanon.com>
Thu, 29 Oct 2015 13:54:40 +0000 (13:54 +0000)
src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java

index 575eed70079c439b5231ebb5c82e0079d0e85cfa..6930b873af273d12efe0fc498034fb850fcd7c90 100644 (file)
@@ -8,7 +8,7 @@ package com.marcozanon.macaco.logging;
 
 import com.marcozanon.macaco.sql.MSqlConnection;
 import com.marcozanon.macaco.sql.MSqlConnectionFailureException;
-import com.marcozanon.macaco.sql.MSqlConnectionGenerator;
+import com.marcozanon.macaco.sql.MSqlConnectionPool;
 import com.marcozanon.macaco.sql.MSqlStatementException;
 import com.marcozanon.macaco.sql.MSqlTable;
 import com.marcozanon.macaco.text.MText;
@@ -18,7 +18,7 @@ import java.util.LinkedHashMap;
 
 public class MLogDatabaseTable extends MLogTarget {
 
-    protected MSqlConnectionGenerator sqlConnectionGenerator = null;
+    protected MSqlConnectionPool sqlConnectionPool = null;
 
     protected String logDatabaseTable = null;
     protected String logDatabaseTablePrimaryKey = null;
@@ -28,11 +28,11 @@ public class MLogDatabaseTable extends MLogTarget {
 
     /* */
 
-    public MLogDatabaseTable(MSqlConnectionGenerator sqlConnectionGenerator, String logDatabaseTable, String logDatabaseTablePrimaryKey, String logDatabaseField) throws MLoggingException {
+    public MLogDatabaseTable(MSqlConnectionPool sqlConnectionPool, String logDatabaseTable, String logDatabaseTablePrimaryKey, String logDatabaseField) throws MLoggingException {
         super();
         //
-        if (null == sqlConnectionGenerator) {
-            throw new IllegalArgumentException("Invalid 'sqlConnectionGenerator': null.");
+        if (null == sqlConnectionPool) {
+            throw new IllegalArgumentException("Invalid 'sqlConnectionPool': null.");
         }
         if (MText.isEmpty(logDatabaseTable)) {
             throw new IllegalArgumentException("Invalid 'logDatabaseTable': null or empty.");
@@ -44,25 +44,19 @@ public class MLogDatabaseTable extends MLogTarget {
             throw new IllegalArgumentException("Invalid 'logDatabaseField': null or empty.");
         }
         //
-        this.sqlConnectionGenerator = sqlConnectionGenerator;
+        this.sqlConnectionPool = sqlConnectionPool;
         this.logDatabaseTable = logDatabaseTable;
         this.logDatabaseTablePrimaryKey = logDatabaseTablePrimaryKey;
         this.logDatabaseField = logDatabaseField;
     }
 
     public void close() throws MLoggingException {
-        try {
-            this.getSqlConnection().close();
-        }
-        catch (MSqlConnectionFailureException exception) {
-            throw new MLoggingException("Could not close Sql connection.", exception);
-        }
     }
 
-    /* Sql connection generator. */
+    /* Sql connection pool. */
 
-    protected MSqlConnectionGenerator getSqlConnectionGenerator() {
-        return this.sqlConnectionGenerator;
+    protected MSqlConnectionPool getSqlConnectionPool() {
+        return this.sqlConnectionPool;
     }
 
     /* Database logging parameters. */
@@ -79,12 +73,6 @@ public class MLogDatabaseTable extends MLogTarget {
         return this.logDatabaseField;
     }
 
-    /* Sql connection. */
-
-    protected MSqlConnection getSqlConnection() {
-        return this.sqlConnection;
-    }
-
     /* Output. */
 
     public void appendMessage(String message) throws MLoggingException {
@@ -107,13 +95,13 @@ public class MLogDatabaseTable extends MLogTarget {
                 separator.append(" ");
             }
             //
-            if (null == this.getSqlConnection()) {
-                this.sqlConnection = this.getSqlConnectionGenerator().getNewConnection();
-            }
+            MSqlConnection sqlConnection = this.getSqlConnectionPool().popConnection();
             //
             LinkedHashMap<String, Object> p = new LinkedHashMap<String, Object>();
             p.put(this.getLogDatabaseField(), timestamp + separator + message);
-            (new MSqlTable(this.getSqlConnection(), this.getLogDatabaseTable(), this.getLogDatabaseTablePrimaryKey())).setRecord(p, null);
+            (new MSqlTable(sqlConnection, this.getLogDatabaseTable(), this.getLogDatabaseTablePrimaryKey())).setRecord(p, null);
+            //
+            this.getSqlConnectionPool().pushConnection(sqlConnection);
         }
         catch (MSqlConnectionFailureException exception) {
             throw new MLoggingException("Could not write to database table.", exception);