Skip to content

Commit 577761f

Browse files
34: Feature : show the time taken to establish the connection
1 parent 524f931 commit 577761f

File tree

12 files changed

+90
-30
lines changed

12 files changed

+90
-30
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Purpose
44
Measuring performance of SQL statements executed through JDBC.
55

6+
(click on the image below for an overview of the features)
67
[![Click here for an overview of the features](http://s159433608.onlinehome.fr/overview.png)](https://www.thinglink.com/scene/512018881544454146)
78

89

@@ -18,7 +19,7 @@ Although other tools already exist around JDBC performance monitoring ([log4jdbc
1819
- the connection between the monitored java application (JDBC proxy driver) and the console can be initiated from either side
1920
- Logging of bound values of prepared statements, including the name of the set* method called to bind the value (very helpful to distinguish setDate and setTimestamp to understand [why Oracle does not use an index](http://docs.oracle.com/cd/E16655_01/java.121/e17657/apxref.htm#JJDBC28919) )
2021
- Separate measure of statement execution time and result set iteration time
21-
- Measures commit/rollback times
22+
- Measures connection creation and commit/rollback durations
2223
- Handling of batched statements
2324
- Logging of SQLExceptions
2425
- Displays the `queryTimeout` of each statement (no value means 0 or no timeout) (since 0.5.0)
@@ -58,7 +59,8 @@ Although other tools already exist around JDBC performance monitoring ([log4jdbc
5859
The source code is available on GitHub : https://github.com/sylvainlaurent/JDBC-Performance-Logger
5960

6061
### How to build source
61-
Use Maven and a JDK 7, and run `mvn clean package` in the root directory of the git repository. The binary distribution is then available in `jdbc-perf-logger-gui`.
62+
Use Maven and a JDK >=7, and run `mvn clean package` in the root directory of the git repository. The binary distribution is then available in `jdbc-perf-logger-gui`. You need a JDK 8 to be able to run tests present in the module `jdbc-perf-logger-java8-tests`.
63+
6264
### How to create a release
6365
`mvn release:prepare release:perform` and answer the questions about version number.
6466

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/driver/WrappingDriver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public Connection connect(@Nullable final String url, @Nullable final Properties
6666
assert url != null;
6767
LOGGER.debug("connect url=[{}]", url);
6868
final String unWrappedUrl = extractUrlForWrappedDriver(url);
69+
final long startNanos = System.nanoTime();
6970
Connection connection = DriverManager.getConnection(unWrappedUrl, info);
71+
final long connectionCreationDuration = System.nanoTime() - startNanos;
7072

7173
final Properties cleanedConnectionProperties = new Properties();
7274
if (info != null) {
@@ -82,7 +84,7 @@ public Connection connect(@Nullable final String url, @Nullable final Properties
8284
connection = (Connection) Proxy.newProxyInstance(WrappingDriver.class.getClassLoader(),
8385
Utils.extractAllInterfaces(connection.getClass()), connectionInvocationHandler);
8486

85-
PerfLoggerRemoting.connectionCreated(connectionInvocationHandler);
87+
PerfLoggerRemoting.connectionCreated(connectionInvocationHandler, connectionCreationDuration);
8688

8789
return connection;
8890
}

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/logger/PerfLoggerRemoting.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ static InputStream openConfigFile(final String location) {
133133
private PerfLoggerRemoting() {
134134
}
135135

136-
public static void connectionCreated(final LoggingConnectionInvocationHandler connectionHandler) {
136+
public static void connectionCreated(final LoggingConnectionInvocationHandler connectionHandler,
137+
final long connectionCreationDuration) {
137138
final ConnectionInfo info = new ConnectionInfo(connectionHandler.getConnectionUuid(),
138139
connectionHandler.getConnectionId(), connectionHandler.getUrl(), new Date(),
139-
connectionHandler.getConnectionProperties());
140+
connectionCreationDuration, connectionHandler.getConnectionProperties());
140141
synchronized (connectionToInfo) {
141142
connectionToInfo.put(connectionHandler, info);
142143
postLog(info);

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/model/ConnectionInfo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ public class ConnectionInfo implements LogMessage {
1111
private final int connectionNumber;
1212
private final String url;
1313
private final Date creationDate;
14+
private final long connectionCreationDuration;
1415
/**
1516
* Connection props without password
1617
*/
1718
private final Properties connectionProperties;
1819

1920
public ConnectionInfo(final UUID uuid, final int connectionNumber, final String url, final Date creationDate,
20-
final Properties connectionProperties) {
21+
final long connectionCreationDuration, final Properties connectionProperties) {
2122
this.uuid = uuid;
2223
this.connectionNumber = connectionNumber;
2324
this.url = url;
2425
this.creationDate = creationDate;
26+
this.connectionCreationDuration = connectionCreationDuration;
2527
this.connectionProperties = connectionProperties;
2628
}
2729

@@ -45,13 +47,18 @@ public Properties getConnectionProperties() {
4547
return connectionProperties;
4648
}
4749

50+
public long getConnectionCreationDuration() {
51+
return connectionCreationDuration;
52+
}
53+
4854
@Override
4955
public String toString() {
5056
return "ConnectionInfo["//
5157
+ "connectionId=" + uuid//
5258
+ ", connectionNumber=" + connectionNumber//
5359
+ ", url=" + url//
5460
+ ", creationDate=" + creationDate//
61+
+ ", connectionCreationDuration=" + connectionCreationDuration//
5562
+ "]";
5663
}
5764
}

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/db/LogRepositoryReadJdbc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public DetailedViewStatementLog getStatementLog(final long id) {
207207
+ "statement_log.threadName, statement_log.exception, "//
208208
+ "statement_log.connectionId,"//
209209
+ "connection_info.connectionNumber, connection_info.url, connection_info.creationDate,"//
210-
+ "connection_info.connectionProperties "//
210+
+ "connection_info.connectionCreationDurationNanos, connection_info.connectionProperties "//
211211
+ "from statement_log join connection_info on (statement_log.connectionId=connection_info.connectionId) "//
212212
+ "where statement_log.id=?";
213213

@@ -231,10 +231,11 @@ public DetailedViewStatementLog getStatementLog(final long id) {
231231
final int connectionNumber = resultSet.getInt(i++);
232232
final String connectionUrl = resultSet.getString(i++);
233233
final Timestamp creationDate = resultSet.getTimestamp(i++);
234+
final long connectionCreationDurationNanos = resultSet.getLong(i++);
234235
final Properties connectionProperties = (Properties) resultSet.getObject(i++);
235236

236237
final ConnectionInfo connectionInfo = new ConnectionInfo(connectionId, connectionNumber,
237-
connectionUrl, creationDate, connectionProperties);
238+
connectionUrl, creationDate, connectionCreationDurationNanos, connectionProperties);
238239

239240
result = new DetailedViewStatementLog(logId, connectionInfo, tstamp.getTime(), statementType,
240241
rawSql, filledSql, threadName, exception);

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/db/LogRepositoryUpdateJdbc.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,15 @@ public synchronized void addBatchedNonPreparedStatementsLog(final BatchedNonPrep
319319
public synchronized void addConnection(final ConnectionInfo connectionInfo) {
320320
LOGGER.debug("addConnection:{}", connectionInfo);
321321
try (PreparedStatement stmt = connectionUpdate
322-
.prepareStatement("merge into connection_info (connectionId, connectionNumber, url, creationDate, connectionProperties)"//
323-
+ " key(connectionId) values (?,?,?,?,?)")) {
322+
.prepareStatement("merge into connection_info (connectionId, connectionNumber, url, creationDate, "//
323+
+ "connectionCreationDurationNanos, connectionProperties)"//
324+
+ " key(connectionId) values (?,?,?,?,?,?)")) {
324325
int i = 1;
325326
stmt.setObject(i++, connectionInfo.getUuid());
326327
stmt.setInt(i++, connectionInfo.getConnectionNumber());
327328
stmt.setString(i++, connectionInfo.getUrl());
328329
stmt.setTimestamp(i++, new Timestamp(connectionInfo.getCreationDate().getTime()));
330+
stmt.setLong(i++, connectionInfo.getConnectionCreationDuration());
329331
stmt.setObject(i++, connectionInfo.getConnectionProperties());
330332

331333
stmt.execute();

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/ui/PerfLoggerController.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import ch.sla.jdbcperflogger.console.db.LogSearchCriteria;
4343
import ch.sla.jdbcperflogger.console.db.ResultSetAnalyzer;
4444
import ch.sla.jdbcperflogger.console.net.AbstractLogReceiver;
45+
import ch.sla.jdbcperflogger.model.ConnectionInfo;
4546

4647
public class PerfLoggerController {
4748
private final static Logger LOGGER = LoggerFactory.getLogger(PerfLoggerController.class);
@@ -260,6 +261,7 @@ private void statementSelected(@Nullable final Long logId) {
260261
String txt2 = "";
261262
String connectionUrl = null;
262263
String connectionCreationDate = null;
264+
Long connectionCreationDurationMillis = null;
263265
String connectionPropertiesString = null;
264266
DetailedViewStatementLog statementLog = null;
265267
if (logId != null) {
@@ -290,10 +292,13 @@ private void statementSelected(@Nullable final Long logId) {
290292
}
291293
deltaTimestampBaseMillis = statementLog.getTimestamp();
292294

293-
connectionUrl = statementLog.getConnectionInfo().getUrl();
294-
connectionPropertiesString = statementLog.getConnectionInfo().getConnectionProperties().toString();
295+
final ConnectionInfo connectionInfo = statementLog.getConnectionInfo();
296+
connectionUrl = connectionInfo.getUrl();
297+
connectionPropertiesString = connectionInfo.getConnectionProperties().toString();
295298
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
296-
connectionCreationDate = format.format(statementLog.getConnectionInfo().getCreationDate());
299+
connectionCreationDate = format.format(connectionInfo.getCreationDate());
300+
connectionCreationDurationMillis = TimeUnit.NANOSECONDS.toMillis(connectionInfo
301+
.getConnectionCreationDuration());
297302
break;
298303
case RAW_SQL:
299304
if (statementLog.getStatementType() != null) {
@@ -345,6 +350,8 @@ private void statementSelected(@Nullable final Long logId) {
345350
perfLoggerPanel.txtFieldFilledSql.select(0, 0);
346351
perfLoggerPanel.connectionUrlField.setText(connectionUrl);
347352
perfLoggerPanel.connectionCreationDateField.setText(connectionCreationDate);
353+
perfLoggerPanel.connectionCreationDurationField
354+
.setText(connectionCreationDurationMillis != null ? connectionCreationDurationMillis.toString() : "");
348355
perfLoggerPanel.connectionPropertiesField.setText(connectionPropertiesString);
349356

350357
perfLoggerPanel.setDeltaTimestampBaseMillis(deltaTimestampBaseMillis);

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/ui/PerfLoggerPanel.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class PerfLoggerPanel extends JPanel {
122122
JTextField connectionCreationDateField;
123123
private JTextField sqlClauseField;
124124
JTextField connectionPropertiesField;
125+
JTextField connectionCreationDurationField;
125126

126127
public PerfLoggerPanel(final PerfLoggerController perfLoggerController) {
127128

@@ -545,7 +546,7 @@ public void actionPerformed(@Nullable final ActionEvent e) {
545546
final GridBagLayout gbl_panelConnectionInfo = new GridBagLayout();
546547
gbl_panelConnectionInfo.columnWidths = new int[] { 0, 0, 0, 0, 0 };
547548
gbl_panelConnectionInfo.rowHeights = new int[] { 0, 0, 0, 0 };
548-
gbl_panelConnectionInfo.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE };
549+
gbl_panelConnectionInfo.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
549550
gbl_panelConnectionInfo.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
550551
panelConnectionInfo.setLayout(gbl_panelConnectionInfo);
551552

@@ -559,8 +560,9 @@ public void actionPerformed(@Nullable final ActionEvent e) {
559560

560561
connectionUrlField = new JTextField();
561562
final GridBagConstraints gbc_connectionUrlField = new GridBagConstraints();
563+
gbc_connectionUrlField.gridwidth = 3;
562564
gbc_connectionUrlField.fill = GridBagConstraints.HORIZONTAL;
563-
gbc_connectionUrlField.insets = new Insets(0, 0, 5, 5);
565+
gbc_connectionUrlField.insets = new Insets(0, 0, 5, 0);
564566
gbc_connectionUrlField.gridx = 1;
565567
gbc_connectionUrlField.gridy = 0;
566568
panelConnectionInfo.add(connectionUrlField, gbc_connectionUrlField);
@@ -570,35 +572,51 @@ public void actionPerformed(@Nullable final ActionEvent e) {
570572
final GridBagConstraints gbc_lblCreated = new GridBagConstraints();
571573
gbc_lblCreated.anchor = GridBagConstraints.EAST;
572574
gbc_lblCreated.insets = new Insets(0, 0, 5, 5);
573-
gbc_lblCreated.gridx = 2;
574-
gbc_lblCreated.gridy = 0;
575+
gbc_lblCreated.gridx = 0;
576+
gbc_lblCreated.gridy = 1;
575577
panelConnectionInfo.add(lblCreated, gbc_lblCreated);
576578

577579
connectionCreationDateField = new JTextField();
578580
final GridBagConstraints gbc_connectionCreationDateField = new GridBagConstraints();
579581
gbc_connectionCreationDateField.fill = GridBagConstraints.HORIZONTAL;
580-
gbc_connectionCreationDateField.insets = new Insets(0, 0, 5, 0);
581-
gbc_connectionCreationDateField.gridx = 3;
582-
gbc_connectionCreationDateField.gridy = 0;
582+
gbc_connectionCreationDateField.insets = new Insets(0, 0, 5, 5);
583+
gbc_connectionCreationDateField.gridx = 1;
584+
gbc_connectionCreationDateField.gridy = 1;
583585
panelConnectionInfo.add(connectionCreationDateField, gbc_connectionCreationDateField);
584586
connectionCreationDateField.setColumns(15);
585587

588+
final JLabel lblCreationDuration = new JLabel("Creation duration (ms):");
589+
final GridBagConstraints gbc_lblCreationDuration = new GridBagConstraints();
590+
gbc_lblCreationDuration.anchor = GridBagConstraints.EAST;
591+
gbc_lblCreationDuration.insets = new Insets(0, 0, 5, 5);
592+
gbc_lblCreationDuration.gridx = 2;
593+
gbc_lblCreationDuration.gridy = 1;
594+
panelConnectionInfo.add(lblCreationDuration, gbc_lblCreationDuration);
595+
596+
connectionCreationDurationField = new JTextField();
597+
final GridBagConstraints gbc_creationDurationField = new GridBagConstraints();
598+
gbc_creationDurationField.insets = new Insets(0, 0, 5, 0);
599+
gbc_creationDurationField.fill = GridBagConstraints.HORIZONTAL;
600+
gbc_creationDurationField.gridx = 3;
601+
gbc_creationDurationField.gridy = 1;
602+
panelConnectionInfo.add(connectionCreationDurationField, gbc_creationDurationField);
603+
connectionCreationDurationField.setColumns(10);
604+
586605
final JLabel lblConectionProperties = new JLabel("Properties:");
587606
final GridBagConstraints gbc_lblConectionProperties = new GridBagConstraints();
588607
gbc_lblConectionProperties.anchor = GridBagConstraints.EAST;
589-
gbc_lblConectionProperties.insets = new Insets(0, 0, 5, 5);
608+
gbc_lblConectionProperties.insets = new Insets(0, 0, 0, 5);
590609
gbc_lblConectionProperties.gridx = 0;
591-
gbc_lblConectionProperties.gridy = 1;
610+
gbc_lblConectionProperties.gridy = 2;
592611
panelConnectionInfo.add(lblConectionProperties, gbc_lblConectionProperties);
593612
lblConectionProperties.setToolTipText("(Password property removed)");
594613

595614
connectionPropertiesField = new JTextField();
596615
final GridBagConstraints gbc_connectionPropertiesField = new GridBagConstraints();
597-
gbc_connectionPropertiesField.insets = new Insets(0, 0, 5, 0);
598616
gbc_connectionPropertiesField.fill = GridBagConstraints.HORIZONTAL;
599617
gbc_connectionPropertiesField.gridwidth = 3;
600618
gbc_connectionPropertiesField.gridx = 1;
601-
gbc_connectionPropertiesField.gridy = 1;
619+
gbc_connectionPropertiesField.gridy = 2;
602620
panelConnectionInfo.add(connectionPropertiesField, gbc_connectionPropertiesField);
603621
connectionPropertiesField.setColumns(10);
604622

jdbc-perf-logger-gui/src/main/resources/initdb.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SET UNDO_LOG 0;
77

88
create table if not exists connection_info
99
(id identity, connectionId UUID not null, connectionNumber int not null,
10-
url varchar not null, creationDate timestamp not null, connectionProperties other);
10+
url varchar not null, creationDate timestamp not null, connectionCreationDurationNanos bigInt, connectionProperties other);
1111

1212
create table if not exists statement_log
1313
(id identity, connectionId UUID not null, logId UUID not null, tstamp timestamp not null, statementType tinyInt not null,

jdbc-perf-logger-gui/src/test/java/ch/sla/jdbcperflogger/console/db/AbstractLogRepositoryTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,28 @@ public void tearDown() {
3333
repositoryRead.dispose();
3434
}
3535

36-
protected StatementLog insert1Log() {
36+
protected ConnectionInfo insert1Connection() {
3737
final Properties connProps = new Properties();
3838
connProps.setProperty("myprop", "myval");
39-
final ConnectionInfo connectionInfo = new ConnectionInfo(randomUUID(), 12, "jdbc:toto", new Date(), connProps);
39+
final ConnectionInfo connectionInfo = new ConnectionInfo(randomUUID(), 12, "jdbc:toto", new Date(), 12,
40+
connProps);
4041
repositoryUpdate.addConnection(connectionInfo);
42+
return connectionInfo;
43+
}
44+
45+
protected StatementLog insert1Log(final ConnectionInfo connectionInfo) {
4146

4247
final StatementLog log = new StatementLog(connectionInfo.getUuid(), randomUUID(), System.currentTimeMillis(),
4348
StatementType.BASE_NON_PREPARED_STMT, "myrawsql", Thread.currentThread().getName(), 123, true);
4449
repositoryUpdate.addStatementLog(log);
4550
return log;
4651
}
4752

53+
protected StatementLog insert1Log() {
54+
final ConnectionInfo connectionInfo = insert1Connection();
55+
return insert1Log(connectionInfo);
56+
}
57+
4858
protected int countRowsInTable(final String table) {
4959
try (Statement stmt = repositoryUpdate.connectionUpdate.createStatement()) {
5060
try (ResultSet rset = stmt.executeQuery("select count(1) from " + table)) {

0 commit comments

Comments
 (0)