Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
0002571: Improve SQL Logging
  • Loading branch information
mmichalek committed Sep 22, 2016
1 parent 1aa51b0 commit 6906107
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Expand Up @@ -47,6 +47,10 @@ public class LogSqlBuilder {
protected boolean logSqlParametersInline = true;

public void logSql(Logger loggerArg, String sql, Object[] args, int[] types, long executionTime) {
logSql(loggerArg, null, sql, args, types, executionTime);
}

public void logSql(Logger loggerArg, String message, String sql, Object[] args, int[] types, long executionTime) {
boolean longRunning = executionTime >= logSlowSqlThresholdMillis;
if (loggerArg.isDebugEnabled() || longRunning) {
StringBuilder logEntry = new StringBuilder();
Expand All @@ -55,6 +59,10 @@ public void logSql(Logger loggerArg, String sql, Object[] args, int[] types, lon
}
logEntry.append("(" + executionTime + "ms.) ");

if (message != null) {
logEntry.append(message).append(" ");
}

if (logSqlParametersInline) {
logEntry.append(buildDynamicSqlForLog(sql, args, types));
} else {
Expand Down
Expand Up @@ -429,10 +429,6 @@ public void prepare(String sql) {
public int addRow(Object marker, Object[] args, int[] argTypes) {
int rowsUpdated = 0;
try {
if (log.isDebugEnabled()) {
log.debug("Adding {} {}", ArrayUtils.toString(args), inBatchMode ? " in batch mode"
: "");
}
if (args != null) {
jdbcSqlTemplate.setValues(pstmt, args, argTypes, jdbcSqlTemplate.getLobHandler().getDefaultHandler());
}
Expand All @@ -441,12 +437,19 @@ public int addRow(Object marker, Object[] args, int[] argTypes) {
marker = new Integer(markers.size() + 1);
}
markers.add(marker);
long start = System.currentTimeMillis();
pstmt.addBatch();
long end = System.currentTimeMillis();
logSqlBuilder.logSql(log, "addBatch()", psql, args, argTypes, (end-start));

if (markers.size() >= jdbcSqlTemplate.getSettings().getBatchSize()) {
rowsUpdated = flush();
}
} else {
long start = System.currentTimeMillis();
pstmt.execute();
long end = System.currentTimeMillis();
logSqlBuilder.logSql(log, psql, args, argTypes, (end-start));
rowsUpdated = pstmt.getUpdateCount();
}
} catch (SQLException ex) {
Expand Down

0 comments on commit 6906107

Please sign in to comment.