From 3cdc9821597a79fcec9bf75a83be06b3dc2c1e4e Mon Sep 17 00:00:00 2001
From: Marco Zanon <info@marcozanon.com>
Date: Thu, 29 Oct 2015 13:54:40 +0000
Subject: [PATCH] Replaced MSqlConnectionGenerator with MSqlConnectionPool.

---
 .../macaco/logging/MLogDatabaseTable.java     | 38 +++++++------------
 1 file changed, 13 insertions(+), 25 deletions(-)

diff --git a/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java b/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java
index 575eed7..6930b87 100644
--- a/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java
+++ b/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java
@@ -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);
-- 
2.30.2