--- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Wed Sep 24 22:53:30 2014 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java Thu Sep 25 17:50:40 2014 +0200
@@ -17,6 +17,8 @@
*/
package info.globalcode.sql.dk;
+import static info.globalcode.sql.dk.jmx.ConnectionManagement.incrementCounter;
+import static info.globalcode.sql.dk.jmx.ConnectionManagement.resetCounter;
import info.globalcode.sql.dk.batch.Batch;
import info.globalcode.sql.dk.batch.BatchException;
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
@@ -24,6 +26,8 @@
import info.globalcode.sql.dk.configuration.Property;
import info.globalcode.sql.dk.formatting.ColumnsHeader;
import info.globalcode.sql.dk.formatting.Formatter;
+import info.globalcode.sql.dk.jmx.ConnectionManagement;
+import info.globalcode.sql.dk.jmx.ConnectionManagement.COUNTER;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@@ -50,10 +54,23 @@
private final DatabaseDefinition databaseDefinition;
private final Connection connection;
private final Properties properties;
+ /**
+ * Could be null = JMX is disabled → must check, see functions in
+ * {@linkplain ConnectionManagement}
+ */
+ private final ConnectionManagement connectionMBean;
- public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
+ /**
+ *
+ * @param databaseDefinition DB url, name, password etc.
+ * @param properties additional properties from CLI
+ * @param connectionMBean JMX management bean | null = disabled JMX reporting
+ * @throws SQLException
+ */
+ public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties, ConnectionManagement connectionMBean) throws SQLException {
this.databaseDefinition = databaseDefinition;
this.properties = properties;
+ this.connectionMBean = connectionMBean;
if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
@@ -97,6 +114,9 @@
}
private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException {
+ incrementCounter(connectionMBean, COUNTER.COMMAND);
+ resetCounter(connectionMBean, COUNTER.RECORD_CURRENT);
+
try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
log.log(Level.FINE, "Statement prepared");
sqlCommand.parametrize(ps);
@@ -135,6 +155,9 @@
int columnCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
+ incrementCounter(connectionMBean, COUNTER.RECORD_CURRENT);
+ incrementCounter(connectionMBean, COUNTER.RECORD_TOTAL);
+
formatter.writeStartRow();
for (int i = 1; i <= columnCount; i++) {