#1631 - Change logging for SqlUpdate ... such that it is more consistent with other SQL logging wrt JDBC batch

This commit is contained in:
rob bygrave
2019-02-07 17:20:49 +13:00
parent 0c03a8b925
commit 9e1d7ab722
20 changed files with 192 additions and 109 deletions
@@ -3,8 +3,10 @@ package io.ebeaninternal.server.core;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiSqlUpdate;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.lib.util.Str;
import io.ebeaninternal.server.persist.BatchControl;
import io.ebeaninternal.server.persist.PersistExecute;
import io.ebeaninternal.server.persist.TrimLogSql;
/**
* Persist request specifically for CallableSql.
@@ -25,12 +27,12 @@ public final class PersistRequestUpdateSql extends PersistRequest {
private String tableName;
private String description;
private boolean addBatch;
private boolean forceNoBatch;
private boolean batchThisRequest;
public PersistRequestUpdateSql(SpiEbeanServer server, SpiSqlUpdate sqlUpdate,
SpiTransaction t, PersistExecute persistExecute, boolean forceNoBatch) {
@@ -124,10 +126,9 @@ public final class PersistRequestUpdateSql extends PersistRequest {
* Specify the type of statement executed. Used to automatically register
* with the transaction event.
*/
public void setType(SqlType sqlType, String tableName, String description) {
public void setType(SqlType sqlType, String tableName) {
this.sqlType = sqlType;
this.tableName = tableName;
this.description = description;
}
/**
@@ -137,6 +138,20 @@ public final class PersistRequestUpdateSql extends PersistRequest {
this.bindLog = bindLog;
}
public void startBind(boolean batchThisRequest) {
this.batchThisRequest = batchThisRequest;
super.startBind(batchThisRequest);
}
/**
* Log the sql bind used with jdbc batch.
*/
public void logSqlBatchBind() {
if (transaction.isLogSql()) {
transaction.logSql(Str.add(" -- bind(", bindLog, ")"));
}
}
/**
* Perform post execute processing.
*/
@@ -145,9 +160,8 @@ public final class PersistRequestUpdateSql extends PersistRequest {
if (startNanos > 0) {
persistExecute.collectSqlUpdate(label, startNanos, rowCount);
}
if (transaction.isLogSummary()) {
String m = description + " table[" + tableName + "] rows[" + rowCount + "] bind[" + bindLog + "]";
transaction.logSummary(m);
if (transaction.isLogSql() && !batchThisRequest) {
transaction.logSql(Str.add(TrimLogSql.trim(updateSql.getGeneratedSql()), "; -- bind(", bindLog, ") rows(", String.valueOf(rowCount), ")"));
}
if (updateSql.isAutoTableMod()) {
@@ -15,14 +15,16 @@ public class Str {
// determine a decent buffer size
int len = 16 + s0.length() + s1.length();
for (String arg1 : args) {
len += arg1.length();
len += (arg1 == null) ? 0 : arg1.length();
}
// append all the strings into the buffer
StringBuilder sb = new StringBuilder(len);
sb.append(s0).append(s1);
for (String arg : args) {
sb.append(arg);
if (arg != null) {
sb.append(arg);
}
}
return sb.toString();
}
@@ -95,15 +95,10 @@ class ExeUpdateSql {
sql = BindParamsParser.parse(bindParams, sql);
updateSql.setGeneratedSql(sql);
boolean logSql = request.isLogSql();
PreparedStatement pstmt;
if (batchThisRequest) {
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
pstmt = pstmtFactory.getPstmt(t, request.isLogSql(), sql, request);
} else {
if (logSql) {
t.logSql(TrimLogSql.trim(sql));
}
pstmt = pstmtFactory.getPstmt(t, sql, request.isGetGeneratedKeys());
}
@@ -120,6 +115,9 @@ class ExeUpdateSql {
// derive the statement type (for TransactionEvent)
parseUpdate(sql, request);
if (batchThisRequest) {
request.logSqlBatchBind();
}
return pstmt;
}
@@ -127,16 +125,16 @@ class ExeUpdateSql {
private void determineType(String word1, String word2, String word3, PersistRequestUpdateSql request) {
if (word1.equalsIgnoreCase("UPDATE")) {
request.setType(SqlType.SQL_UPDATE, word2, "UpdateSql");
request.setType(SqlType.SQL_UPDATE, word2);
} else if (word1.equalsIgnoreCase("DELETE")) {
request.setType(SqlType.SQL_DELETE, word3, "DeleteSql");
request.setType(SqlType.SQL_DELETE, word3);
} else if (word1.equalsIgnoreCase("INSERT")) {
request.setType(SqlType.SQL_INSERT, word3, "InsertSql");
request.setType(SqlType.SQL_INSERT, word3);
} else {
request.setType(SqlType.SQL_UNKNOWN, null, "UnknownSql");
request.setType(SqlType.SQL_UNKNOWN, null);
}
}
@@ -160,7 +158,7 @@ class ExeUpdateSql {
if (spaceCount < 2) {
// unknown so no automatic L2 cache invalidation performed (so it should instead)
// be done explicitly via the server.externalModification() method
request.setType(SqlType.SQL_UNKNOWN, null, "UnknownSql");
request.setType(SqlType.SQL_UNKNOWN, null);
} else {
// try to determine if it is insert, update or delete and the table involved