package com.marcozanon.macaco.database;
import com.marcozanon.macaco.MObject;
+import com.marcozanon.macaco.logging.MLogListener;
import com.marcozanon.macaco.text.MText;
import java.sql.Connection;
import java.sql.DriverManager;
protected String url = null;
protected String username = null;
protected String password = null;
+ protected MLogListener logListener = null;
protected Connection connection = null;
/* */
- public MDatabaseConnection(String driver, String url, String username, String password) throws MDatabaseConnectionFailureException {
+ public MDatabaseConnection(String driver, String url, String username, String password, MLogListener logListener) throws MDatabaseConnectionFailureException {
super();
//
if (MText.isBlank(driver)) {
this.url = url;
this.username = username;
this.password = password;
+ this.setLogListener(logListener);
// Load driver.
try {
Class.forName(this.getDriver()).newInstance();
// Set SQL mode to standard ANSI.
try {
this.getConnection().createStatement().executeUpdate("SET SQL_MODE='ANSI'");
+ this.logStatement("### SET SQL_MODE='ANSI' ###");
}
catch (SQLException exception) {
throw new MDatabaseConnectionFailureException("Could not set SQL mode to ANSI.", exception);
this.getConnection().setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
this.getConnection().setAutoCommit(false);
this.setTransactionStatus(MDatabaseConnection.TransactionStatus.SUCCESSFUL);
+ this.logStatement("### BEGIN TRANSACTION ###");
}
catch (SQLException exception) {
throw new MSqlTransactionException("Could not start transaction.", exception);
this.getConnection().rollback();
this.getConnection().setAutoCommit(true);
this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED);
+ this.logStatement("### ROLLBACK ###");
}
catch (SQLException exception) {
this.setTransactionStatus(MDatabaseConnection.TransactionStatus.FAILED);
this.getConnection().commit();
this.getConnection().setAutoCommit(true);
this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED);
+ this.logStatement("### COMMIT ###");
return MDatabaseConnection.TransactionStatus.SUCCESSFUL;
}
catch (SQLException exception) {
// Execute the statement and parse the results.
preparedStatement.execute();
results = new MSqlStatementResults(preparedStatement);
+ this.logStatement(preparedStatement.toString());
}
catch (SQLException exception) {
if (MDatabaseConnection.TransactionStatus.SUCCESSFUL == this.getTransactionStatus()) {
return new MSqlTable(this, table, primaryKey);
}
+ /* Logging. */
+
+ public void setLogListener(MLogListener logListener) {
+ this.logListener = logListener;
+ }
+
+ protected MLogListener getLogListener() {
+ return this.logListener;
+ }
+
+ protected void logStatement(String statement) {
+ MLogListener logListener = this.getLogListener();
+ if (null != logListener) {
+ logListener.onMessageLogging(statement);
+ }
+ }
+
}
package com.marcozanon.macaco.database;
import com.marcozanon.macaco.MObject;
+import com.marcozanon.macaco.logging.MLogListener;
import com.marcozanon.macaco.text.MText;
public class MDatabaseConnectionGenerator extends MObject {
protected String url = null;
protected String username = null;
protected String password = null;
+ protected MLogListener logListener = null;
/* */
- public MDatabaseConnectionGenerator(String driver, String url, String username, String password) {
+ public MDatabaseConnectionGenerator(String driver, String url, String username, String password, MLogListener logListener) {
super();
//
if (MText.isBlank(driver)) {
this.url = url;
this.username = username;
this.password = password;
+ this.logListener = logListener;
}
/* Driver. */
return this.password;
}
+ /* Log listener. */
+
+ public MLogListener getLogListener() {
+ return this.logListener;
+ }
+
/* Generator. */
public MDatabaseConnection getNewDatabaseConnection() throws MDatabaseConnectionFailureException {
- return new MDatabaseConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword());
+ return new MDatabaseConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword(), this.getLogListener());
}
public boolean isGeneratorFor(MDatabaseConnection databaseConnection) {
if (!databaseConnection.getPassword().equals(this.getPassword())) {
return false;
}
+ if (databaseConnection.getLogListener() != this.getLogListener()) {
+ return false;
+ }
//
return true;
}
package com.marcozanon.macaco.database;
import com.marcozanon.macaco.MObject;
+import com.marcozanon.macaco.logging.MLogListener;
import java.util.LinkedList;
public class MDatabaseConnectionPool extends MObject {
/* */
- public MDatabaseConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize) throws MDatabaseConnectionFailureException {
+ public MDatabaseConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize, MLogListener logListener) throws MDatabaseConnectionFailureException {
super();
//
if (0 > minimumSize) {
throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s < %s ('minimumSize').", maximumSize, minimumSize));
}
//
- this.databaseConnectionGenerator = new MDatabaseConnectionGenerator(driver, url, username, password);
+ this.databaseConnectionGenerator = new MDatabaseConnectionGenerator(driver, url, username, password, logListener);
this.minimumSize = minimumSize;
this.maximumSize = maximumSize;
//