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;
public class MLogDatabaseTable extends MLogTarget {
- protected MSqlConnectionGenerator sqlConnectionGenerator = null;
+ protected MSqlConnectionPool sqlConnectionPool = null;
protected String logDatabaseTable = null;
protected String logDatabaseTablePrimaryKey = null;
/* */
- 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.");
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. */
return this.logDatabaseField;
}
- /* Sql connection. */
-
- protected MSqlConnection getSqlConnection() {
- return this.sqlConnection;
- }
-
/* Output. */
public void appendMessage(String message) throws MLoggingException {
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);