From: Marco Zanon Date: Thu, 29 Oct 2015 13:54:40 +0000 (+0000) Subject: Replaced MSqlConnectionGenerator with MSqlConnectionPool. X-Git-Tag: 4.0~3 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=36320ae253587b9e6fcfafde9798b81ebc7eda47;p=Macaco Replaced MSqlConnectionGenerator with MSqlConnectionPool. --- diff --git a/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java b/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java index 575eed7..6930b87 100644 --- a/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java +++ b/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 p = new LinkedHashMap(); 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);