mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Add SpiTxnLogger as main point to log SQL, SUM and TXN messages on a per transaction basis
This commit is contained in:
1 parent
bc4d9837c6
commit
9720cd8189
31 files changed
+307
-198
No files matched your search
@@ -10,18 +10,25 @@ package io.ebeaninternal.api;
|
||||
public interface SpiLogManager {
|
||||
|
||||
/**
|
||||
* Return the SQL logger.
|
||||
* Enable bind logging.
|
||||
*/
|
||||
boolean enableBindLog();
|
||||
|
||||
/**
|
||||
* Logger used for general transactions.
|
||||
*/
|
||||
SpiTxnLogger logger();
|
||||
|
||||
/**
|
||||
* Logger used for read only transactions.
|
||||
*/
|
||||
SpiTxnLogger readOnlyLogger();
|
||||
|
||||
/**
|
||||
* Hmmmm, return the SQL logger for logging truncate statements.
|
||||
* <p>
|
||||
* Maybe we should get rid of this
|
||||
*/
|
||||
SpiLogger sql();
|
||||
|
||||
/**
|
||||
* Return the TXN logger.
|
||||
*/
|
||||
SpiLogger txn();
|
||||
|
||||
/**
|
||||
* Return the Summary logger.
|
||||
*/
|
||||
SpiLogger sum();
|
||||
|
||||
}
|
||||
@@ -27,10 +27,6 @@ public interface SpiTransaction extends Transaction {
|
||||
*/
|
||||
String getLabel();
|
||||
|
||||
/**
|
||||
* Return the string prefix with the transaction id and label used in logging.
|
||||
*/
|
||||
String getLogPrefix();
|
||||
|
||||
/**
|
||||
* Return true if generated SQL and Bind values should be logged to the
|
||||
@@ -47,12 +43,14 @@ public interface SpiTransaction extends Transaction {
|
||||
/**
|
||||
* Log a message to the SQL logger.
|
||||
*/
|
||||
void logSql(String msg);
|
||||
void logSql(String... msg);
|
||||
|
||||
/**
|
||||
* Log a message to the SUMMARY logger.
|
||||
*/
|
||||
void logSummary(String msg);
|
||||
void logSummary(String... msg);
|
||||
|
||||
void logTxn(String... args);
|
||||
|
||||
/**
|
||||
* Register a "Deferred Relationship" that requires an additional update later.
|
||||
|
||||
@@ -137,10 +137,6 @@ public abstract class SpiTransactionProxy implements SpiTransaction {
|
||||
transaction.setDocStoreBatchSize(batchSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogPrefix() {
|
||||
return transaction.getLogPrefix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogSql() {
|
||||
@@ -153,15 +149,20 @@ public abstract class SpiTransactionProxy implements SpiTransaction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSql(String msg) {
|
||||
public void logSql(String... msg) {
|
||||
transaction.logSql(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSummary(String msg) {
|
||||
public void logSummary(String... msg) {
|
||||
transaction.logSummary(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTxn(String... args) {
|
||||
transaction.logTxn(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSkipCache(boolean skipCache) {
|
||||
transaction.setSkipCache(skipCache);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
/**
|
||||
* Per Transaction logging of SQL, TXN and Summary messages.
|
||||
*/
|
||||
public interface SpiTxnLogger {
|
||||
|
||||
String id();
|
||||
|
||||
/**
|
||||
* Is debug logging enabled.
|
||||
*/
|
||||
boolean isLogSql();
|
||||
|
||||
/**
|
||||
* Is summary logging enabled.
|
||||
*/
|
||||
boolean isLogSummary();
|
||||
|
||||
/**
|
||||
* Log a SQL message.
|
||||
*/
|
||||
void sql(String[] msg);
|
||||
|
||||
/**
|
||||
* Log a Summary message.
|
||||
*/
|
||||
void sum(String[] msg);
|
||||
|
||||
/**
|
||||
* Log a Transaction message.
|
||||
*/
|
||||
void txn(String[] args);
|
||||
|
||||
/**
|
||||
* Transaction Committed.
|
||||
*/
|
||||
void notifyCommit();
|
||||
|
||||
/**
|
||||
* Query only transaction completed.
|
||||
*/
|
||||
void notifyQueryOnly();
|
||||
|
||||
/**
|
||||
* Transaction Rolled back.
|
||||
*/
|
||||
void notifyRollback(Throwable cause);
|
||||
}
|
||||
@@ -151,7 +151,7 @@ public abstract class AbstractSqlQueryRequest implements CancelableQuery {
|
||||
}
|
||||
if (isLogSql()) {
|
||||
long micros = (System.nanoTime() - startNano) / 1000L;
|
||||
transaction.logSql(Str.add(TrimLogSql.trim(sql), "; --bind(", bindLog, ") --micros(", micros + ")"));
|
||||
transaction.logSql(TrimLogSql.trim(sql), "; --bind(", bindLog, ") --micros(", String.valueOf(micros), ")");
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
|
||||
@@ -691,7 +691,7 @@ public final class OrmQueryRequest<T> extends BeanRequest implements SpiOrmQuery
|
||||
/**
|
||||
* Log the SQL if the logLevel is appropriate.
|
||||
*/
|
||||
public void logSql(String sql) {
|
||||
public void logSql(String... sql) {
|
||||
transaction.logSql(sql);
|
||||
}
|
||||
|
||||
|
||||
@@ -932,16 +932,16 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
|
||||
String name = beanDescriptor.name();
|
||||
switch (type) {
|
||||
case INSERT:
|
||||
transaction.logSummary("Inserted [" + name + "] [" + (idValue == null ? "" : idValue) + draft);
|
||||
transaction.logSummary("Inserted [" , name , "] [" , (idValue == null ? "" : idValue.toString()) , draft);
|
||||
break;
|
||||
case UPDATE:
|
||||
transaction.logSummary("Updated [" + name + "] [" + idValue + draft);
|
||||
transaction.logSummary("Updated [" , name , "] [" , idValue.toString() , draft);
|
||||
break;
|
||||
case DELETE:
|
||||
transaction.logSummary("Deleted [" + name + "] [" + idValue + draft);
|
||||
transaction.logSummary("Deleted [" , name , "] [" , idValue.toString() , draft);
|
||||
break;
|
||||
case DELETE_SOFT:
|
||||
transaction.logSummary("SoftDelete [" + name + "] [" + idValue + draft);
|
||||
transaction.logSummary("SoftDelete [" , name , "] [" , idValue.toString() , draft);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
+2
-6
@@ -1,12 +1,8 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.CallableSql;
|
||||
import io.ebeaninternal.api.BindParams;
|
||||
import io.ebeaninternal.api.*;
|
||||
import io.ebeaninternal.api.BindParams.Param;
|
||||
import io.ebeaninternal.api.SpiCallableSql;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.api.TransactionEventTable;
|
||||
import io.ebeaninternal.server.persist.PersistExecute;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
@@ -86,7 +82,7 @@ public final class PersistRequestCallableSql extends PersistRequest {
|
||||
persistExecute.collectSqlCall(label, startNanos);
|
||||
}
|
||||
if (transaction.isLogSummary()) {
|
||||
transaction.logSummary("CallableSql label[" + callableSql.getLabel() + "]" + " rows[" + rowCount + "]" + " bind[" + bindLog + "]");
|
||||
transaction.logSummary("CallableSql label[", callableSql.getLabel(), "] rows[", String.valueOf(rowCount), "] bind[", bindLog, "]");
|
||||
}
|
||||
|
||||
// register table modifications with the transaction event
|
||||
|
||||
@@ -83,7 +83,7 @@ public final class PersistRequestOrmUpdate extends PersistRequest {
|
||||
OrmUpdateType ormUpdateType = ormUpdate.getOrmUpdateType();
|
||||
String tableName = ormUpdate.getBaseTable();
|
||||
if (transaction.isLogSummary()) {
|
||||
transaction.logSummary(ormUpdateType + " table[" + tableName + "] rows[" + rowCount + "] bind[" + bindLog + "]");
|
||||
transaction.logSummary(ormUpdateType.toString(), " table[", tableName, "] rows[", String.valueOf(rowCount), "] bind[", bindLog, "]");
|
||||
}
|
||||
if (ormUpdate.isNotifyCache()) {
|
||||
// add the modification info to the TransactionEvent
|
||||
|
||||
@@ -148,7 +148,7 @@ public final class PersistRequestUpdateSql extends PersistRequest {
|
||||
*/
|
||||
public void logSqlBatchBind() {
|
||||
if (transaction.isLogSql()) {
|
||||
transaction.logSql(Str.add(" -- bind(", bindLog, ")"));
|
||||
transaction.logSql(" -- bind(", bindLog, ")");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ public final class PersistRequestUpdateSql extends PersistRequest {
|
||||
persistExecute.collectSqlUpdate(label, startNanos);
|
||||
}
|
||||
if (transaction.isLogSql() && !batchThisRequest) {
|
||||
transaction.logSql(Str.add(TrimLogSql.trim(updateSql.getGeneratedSql()), "; -- bind(", bindLog, ") rows(", String.valueOf(rowCount), ")"));
|
||||
transaction.logSql(TrimLogSql.trim(updateSql.getGeneratedSql()), "; -- bind(", bindLog, ") rows(", String.valueOf(rowCount), ")");
|
||||
}
|
||||
if (updateSql.isAutoTableMod()) {
|
||||
// add the modification info to the TransactionEvent
|
||||
|
||||
@@ -123,7 +123,7 @@ public final class RelationalQueryRequest extends AbstractSqlQueryRequest {
|
||||
public void logSummary() {
|
||||
if (transaction.isLogSummary()) {
|
||||
long micros = (System.nanoTime() - startNano) / 1000L;
|
||||
transaction.logSummary("SqlQuery rows[" + rows + "] micros[" + micros + "] bind[" + bindLog + "]");
|
||||
transaction.logSummary("SqlQuery rows[", String.valueOf(rows), "] micros[", String.valueOf(micros), "] bind[", bindLog, "]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,22 +2,30 @@ package io.ebeaninternal.server.logger;
|
||||
|
||||
import io.ebeaninternal.api.SpiLogManager;
|
||||
import io.ebeaninternal.api.SpiLogger;
|
||||
import io.ebeaninternal.api.SpiTxnLogger;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public final class DLogManager implements SpiLogManager {
|
||||
|
||||
private final SpiLogger sql;
|
||||
private final SpiLogger summary;
|
||||
private final SpiLogger txn;
|
||||
private final boolean useIds;
|
||||
private final AtomicLong counter = new AtomicLong(1000);
|
||||
private final DTxnLogger readOnly;
|
||||
|
||||
public DLogManager(SpiLogger sql, SpiLogger summary, SpiLogger txn) {
|
||||
this.sql = sql;
|
||||
this.summary = summary;
|
||||
this.txn = txn;
|
||||
this.useIds = txn.isDebug();
|
||||
this.readOnly = new DTxnLogger(null, sql, summary, txn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiLogger txn() {
|
||||
return txn;
|
||||
public boolean enableBindLog() {
|
||||
return sql.isDebug();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,8 +34,13 @@ public final class DLogManager implements SpiLogManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiLogger sum() {
|
||||
return summary;
|
||||
public SpiTxnLogger logger() {
|
||||
String id = useIds ? Long.toString(counter.incrementAndGet()) : "";
|
||||
return new DTxnLogger(id, sql, summary, txn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiTxnLogger readOnlyLogger() {
|
||||
return readOnly;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package io.ebeaninternal.server.logger;
|
||||
|
||||
import io.ebeaninternal.api.SpiLogger;
|
||||
import io.ebeaninternal.api.SpiTxnLogger;
|
||||
import io.ebeaninternal.server.util.Str;
|
||||
|
||||
final class DTxnLogger implements SpiTxnLogger {
|
||||
|
||||
private final String id;
|
||||
private final String logPrefix;
|
||||
private final SpiLogger sql;
|
||||
private final SpiLogger sum;
|
||||
private final SpiLogger txn;
|
||||
|
||||
DTxnLogger(String id, SpiLogger sql, SpiLogger sum, SpiLogger txn) {
|
||||
this.id = id;
|
||||
this.logPrefix = id == null ? "" : "txn[" + id + "] ";
|
||||
this.sql = sql;
|
||||
this.sum = sum;
|
||||
this.txn = txn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogSql() {
|
||||
return sql.isDebug();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogSummary() {
|
||||
return sum.isDebug();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sql(String[] msg) {
|
||||
sql.debug(Str.add(logPrefix, msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sum(String[] msg) {
|
||||
sum.debug(Str.add(logPrefix, msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void txn(String[] msg) {
|
||||
txn.debug(Str.add(logPrefix, msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyCommit() {
|
||||
txn.debug(Str.add(logPrefix, "Commit"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyQueryOnly() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyRollback(Throwable cause) {
|
||||
if (txn.isDebug()) {
|
||||
String msg = logPrefix + "Rollback";
|
||||
if (cause != null) {
|
||||
msg += " error: " + formatThrowable(cause);
|
||||
}
|
||||
txn.debug(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private String formatThrowable(Throwable e) {
|
||||
if (e == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
formatThrowable(e, sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void formatThrowable(Throwable e, StringBuilder sb) {
|
||||
sb.append(e.toString());
|
||||
StackTraceElement[] stackTrace = e.getStackTrace();
|
||||
if (stackTrace.length > 0) {
|
||||
sb.append(" stack0: ");
|
||||
sb.append(stackTrace[0]);
|
||||
}
|
||||
Throwable cause = e.getCause();
|
||||
if (cause != null) {
|
||||
sb.append(" cause: ");
|
||||
formatThrowable(cause, sb);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -313,7 +313,7 @@ public final class BatchControl {
|
||||
BatchedBeanHolder[] bsArray = beanHolderArray();
|
||||
Arrays.sort(bsArray, depthComparator);
|
||||
if (transaction.isLogSummary()) {
|
||||
transaction.logSummary("BatchControl flush " + Arrays.toString(bsArray));
|
||||
transaction.logSummary("BatchControl flush " , Arrays.toString(bsArray));
|
||||
}
|
||||
for (BatchedBeanHolder beanHolder : bsArray) {
|
||||
beanHolder.executeNow();
|
||||
|
||||
@@ -44,7 +44,7 @@ public final class Binder {
|
||||
this.dbExpressionHandler = dbExpressionHandler;
|
||||
this.dataTimeZone = dataTimeZone;
|
||||
this.multiValueBind = multiValueBind;
|
||||
this.enableBindLog = logManager.sql().isDebug();
|
||||
this.enableBindLog = logManager.enableBindLog();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -672,7 +672,7 @@ public final class DefaultPersister implements Persister {
|
||||
if (idList != null) {
|
||||
q.where().idIn(idList);
|
||||
if (t.isLogSummary()) {
|
||||
t.logSummary("-- DeleteById of " + descriptor.name() + " ids[" + idList + "] requires fetch of foreign key values");
|
||||
t.logSummary("-- DeleteById of ", descriptor.name(), " ids[", idList.toString(), "] requires fetch of foreign key values");
|
||||
}
|
||||
List<?> beanList = server.findList(q, t);
|
||||
deleteCascade(beanList, t, deleteMode, false);
|
||||
@@ -681,7 +681,7 @@ public final class DefaultPersister implements Persister {
|
||||
} else {
|
||||
q.where().idEq(id);
|
||||
if (t.isLogSummary()) {
|
||||
t.logSummary("-- DeleteById of " + descriptor.name() + " id[" + id + "] requires fetch of foreign key values");
|
||||
t.logSummary("-- DeleteById of ", descriptor.name(), " id[", String.valueOf(id), "] requires fetch of foreign key values");
|
||||
}
|
||||
EntityBean bean = (EntityBean) server.findOne(q, t);
|
||||
if (bean == null) {
|
||||
@@ -741,7 +741,7 @@ public final class DefaultPersister implements Persister {
|
||||
for (BeanPropertyAssocMany<?> many : manys) {
|
||||
SqlUpdate sqlDelete = many.deleteByParentId(id, idList);
|
||||
if (t.isLogSummary()) {
|
||||
t.logSummary("-- Deleting intersection table entries: " + many.fullName());
|
||||
t.logSummary("-- Deleting intersection table entries: ", many.fullName());
|
||||
}
|
||||
executeSqlUpdate(sqlDelete, t);
|
||||
}
|
||||
@@ -751,9 +751,9 @@ public final class DefaultPersister implements Persister {
|
||||
SqlUpdate deleteById = descriptor.deleteById(id, idList, deleteMode);
|
||||
if (t.isLogSummary()) {
|
||||
if (idList != null) {
|
||||
t.logSummary("-- Deleting " + descriptor.name() + " Ids: " + idList);
|
||||
t.logSummary("-- Deleting ", descriptor.name(), " Ids: ", idList.toString());
|
||||
} else {
|
||||
t.logSummary("-- Deleting " + descriptor.name() + " Id: " + id);
|
||||
t.logSummary("-- Deleting ", descriptor.name(), " Id: ", String.valueOf(id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ final class DeleteUnloadedForeignKeys {
|
||||
|
||||
SpiTransaction t = request.transaction();
|
||||
if (t.isLogSummary()) {
|
||||
t.logSummary("-- Ebean fetching foreign key values for delete of " + descriptor.name() + " id:" + id);
|
||||
t.logSummary("-- Ebean fetching foreign key values for delete of ", descriptor.name(), " id:", String.valueOf(id));
|
||||
}
|
||||
beanWithForeignKeys = (EntityBean) server.findOne(q, t);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
|
||||
} catch (OptimisticLockException e) {
|
||||
// add the SQL and bind values to error message
|
||||
final String m = e.getMessage() + " sql[" + sql + "] bind[" + bindLog + "]";
|
||||
persistRequest.transaction().logSummary("OptimisticLockException:" + m);
|
||||
persistRequest.transaction().logSummary("OptimisticLockException:", m);
|
||||
throw new OptimisticLockException(m, null, e.getEntity());
|
||||
}
|
||||
}
|
||||
@@ -146,15 +146,15 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
|
||||
switch (batchedStatus) {
|
||||
case BATCHED_FIRST: {
|
||||
transaction.logSql(sql);
|
||||
transaction.logSql(Str.add(" -- bind(", bindLog.toString(), ")"));
|
||||
transaction.logSql(" -- bind(", bindLog.toString(), ")");
|
||||
return;
|
||||
}
|
||||
case BATCHED: {
|
||||
transaction.logSql(Str.add(" -- bind(", bindLog.toString(), ")"));
|
||||
transaction.logSql(" -- bind(", bindLog.toString(), ")");
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
transaction.logSql(Str.add(sql, "; -- bind(", bindLog.toString(), ")"));
|
||||
transaction.logSql(sql, "; -- bind(", bindLog.toString(), ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public final class CQueryEngine {
|
||||
try {
|
||||
int rows = query.execute();
|
||||
if (request.logSql()) {
|
||||
request.logSql(Str.add(query.generatedSql(), "; --bind(", query.bindLog(), ") --micros(", query.micros() + ") --rows(", rows + ")"));
|
||||
request.logSql(query.generatedSql(), "; --bind(", query.bindLog(), ") --micros(", query.micros() + ") --rows(", rows + ")");
|
||||
}
|
||||
return rows;
|
||||
} catch (SQLException e) {
|
||||
@@ -129,7 +129,7 @@ public final class CQueryEngine {
|
||||
SpiTransaction t = request.transaction();
|
||||
if (t.isLogSummary()) {
|
||||
// log the error to the transaction log
|
||||
t.logSummary("ERROR executing query, bindLog[" + bindLog + "] error:" + StringHelper.removeNewLines(e.getMessage()));
|
||||
t.logSummary("ERROR executing query, bindLog[", bindLog, "] error:", StringHelper.removeNewLines(e.getMessage()));
|
||||
}
|
||||
// ensure 'rollback' is logged if queryOnly transaction
|
||||
t.connection();
|
||||
@@ -147,7 +147,7 @@ public final class CQueryEngine {
|
||||
}
|
||||
|
||||
private <T> void logGeneratedSql(OrmQueryRequest<T> request, String sql, String bindLog, long micros) {
|
||||
request.logSql(Str.add(sql, "; --bind(", bindLog, ") --micros(", micros + ")"));
|
||||
request.logSql(sql, "; --bind(", bindLog, ") --micros(", micros + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -403,7 +403,7 @@ public final class CQueryEngine {
|
||||
* Log the generated SQL to the transaction log.
|
||||
*/
|
||||
private void logSql(CQuery<?> query) {
|
||||
query.transaction().logSql(Str.add(query.generatedSql(), "; --bind(", query.bindLog(), ") --micros(", query.micros() + ")"));
|
||||
query.transaction().logSql(query.generatedSql(), "; --bind(", query.bindLog(), ") --micros(", String.valueOf(query.micros()), ")");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ public final class DocStoreOnlyTransaction extends JdbcTransaction {
|
||||
/**
|
||||
* Create a new DocStore only Transaction.
|
||||
*/
|
||||
public DocStoreOnlyTransaction(String id, boolean explicit, TransactionManager manager) {
|
||||
super(id, explicit, null, manager);
|
||||
public DocStoreOnlyTransaction(boolean explicit, TransactionManager manager) {
|
||||
super(explicit, null, manager);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -25,11 +25,11 @@ public final class DocStoreTransactionManager extends TransactionManager {
|
||||
|
||||
@Override
|
||||
public SpiTransaction createReadOnlyTransaction(Object tenantId) {
|
||||
return new DocStoreOnlyTransaction("", false, this);
|
||||
return new DocStoreOnlyTransaction(false, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpiTransaction createTransaction(boolean explicit, Connection c) {
|
||||
return new DocStoreOnlyTransaction(nextTxnId(), explicit, this);
|
||||
return new DocStoreOnlyTransaction(explicit, this);
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -25,14 +25,14 @@ public class ExternalJdbcTransaction extends JdbcTransaction {
|
||||
* </p>
|
||||
*/
|
||||
public ExternalJdbcTransaction(Connection connection) {
|
||||
super(null, true, connection, null);
|
||||
super(true, connection, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct will all explicit parameters.
|
||||
*/
|
||||
public ExternalJdbcTransaction(String id, boolean explicit, Connection connection, TransactionManager manager) {
|
||||
super(id, explicit, connection, manager);
|
||||
public ExternalJdbcTransaction(boolean explicit, Connection connection, TransactionManager manager) {
|
||||
super(explicit, connection, manager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+13
-11
@@ -33,6 +33,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
|
||||
private static final String notExpectedMessage = "Not expected on read only transaction";
|
||||
|
||||
private final TransactionManager manager;
|
||||
private final SpiTxnLogger logger;
|
||||
private final boolean logSql;
|
||||
private final boolean logSummary;
|
||||
|
||||
@@ -61,8 +62,9 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
|
||||
*/
|
||||
ImplicitReadOnlyTransaction(TransactionManager manager, Connection connection) {
|
||||
this.manager = manager;
|
||||
this.logSql = manager.isLogSql();
|
||||
this.logSummary = manager.isLogSummary();
|
||||
this.logger = manager.loggerReadOnly();
|
||||
this.logSql = logger.isLogSql();
|
||||
this.logSummary = logger.isLogSummary();
|
||||
this.active = true;
|
||||
this.connection = connection;
|
||||
this.persistenceContext = new DefaultPersistenceContext();
|
||||
@@ -147,11 +149,6 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
|
||||
public void setSkipCache(boolean skipCache) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogPrefix() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBeanChange(BeanChange beanChange) {
|
||||
throw new IllegalStateException(notExpectedMessage);
|
||||
@@ -433,13 +430,18 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSql(String msg) {
|
||||
manager.log().sql().debug(msg);
|
||||
public void logSql(String... msg) {
|
||||
logger.sql(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSummary(String msg) {
|
||||
manager.log().sum().debug(msg);
|
||||
public void logSummary(String... msg) {
|
||||
logger.sum(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTxn(String... args) {
|
||||
// never called
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,7 +11,6 @@ import io.ebeaninternal.server.core.PersistDeferredRelationship;
|
||||
import io.ebeaninternal.server.core.PersistRequestBean;
|
||||
import io.ebeaninternal.server.persist.BatchControl;
|
||||
import io.ebeaninternal.server.persist.BatchedSqlException;
|
||||
import io.ebeaninternal.server.util.Str;
|
||||
import io.ebeanservice.docstore.api.DocStoreTransaction;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
@@ -33,8 +32,8 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
private static final String illegalStateMessage = "Transaction is Inactive";
|
||||
|
||||
final TransactionManager manager;
|
||||
private final SpiTxnLogger logger;
|
||||
private final String id;
|
||||
private final String logPrefix;
|
||||
private final boolean logSql;
|
||||
private final boolean logSummary;
|
||||
private final boolean explicit;
|
||||
@@ -95,17 +94,17 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
private final long startNanos;
|
||||
private boolean autoPersistUpdates;
|
||||
|
||||
JdbcTransaction(String id, boolean explicit, Connection connection, TransactionManager manager) {
|
||||
JdbcTransaction(boolean explicit, Connection connection, TransactionManager manager) {
|
||||
try {
|
||||
this.active = true;
|
||||
this.id = id;
|
||||
this.logPrefix = deriveLogPrefix(id);
|
||||
this.explicit = explicit;
|
||||
this.manager = manager;
|
||||
this.connection = connection;
|
||||
this.persistenceContext = new DefaultPersistenceContext();
|
||||
this.startNanos = System.nanoTime();
|
||||
if (manager == null) {
|
||||
this.logger = null;
|
||||
this.id = "";
|
||||
this.logSql = false;
|
||||
this.logSummary = false;
|
||||
this.skipCacheAfterWrite = true;
|
||||
@@ -113,9 +112,11 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
this.batchOnCascadeMode = false;
|
||||
this.onQueryOnlyCommit = false;
|
||||
} else {
|
||||
this.logger = manager.logger();
|
||||
this.id = logger.id();
|
||||
this.autoPersistUpdates = explicit && manager.isAutoPersistUpdates();
|
||||
this.logSql = manager.isLogSql();
|
||||
this.logSummary = manager.isLogSummary();
|
||||
this.logSql = logger.isLogSql();
|
||||
this.logSummary = logger.isLogSummary();
|
||||
this.skipCacheAfterWrite = manager.isSkipCacheAfterWrite();
|
||||
this.batchMode = manager.isPersistBatch();
|
||||
this.batchOnCascadeMode = manager.isPersistBatchOnCascade();
|
||||
@@ -188,16 +189,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
}
|
||||
}
|
||||
|
||||
private static String deriveLogPrefix(String id) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("txn[");
|
||||
if (id != null) {
|
||||
sb.append(id);
|
||||
}
|
||||
sb.append("] ");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setAutoPersistUpdates(boolean autoPersistUpdates) {
|
||||
@@ -226,17 +218,13 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
this.skipCache = skipCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getLogPrefix() {
|
||||
return logPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (active) {
|
||||
return logPrefix;
|
||||
return id;
|
||||
} else {
|
||||
return logPrefix + "(inactive)";
|
||||
return id + "(inactive)";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -726,13 +714,18 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void logSql(String msg) {
|
||||
manager.log().sql().debug(Str.add(logPrefix, msg));
|
||||
public final void logSql(String... msg) {
|
||||
logger.sql(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void logSummary(String msg) {
|
||||
manager.log().sum().debug(Str.add(logPrefix, msg));
|
||||
public final void logSummary(String... msg) {
|
||||
logger.sum(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTxn(String... args) {
|
||||
logger.txn(args);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -805,9 +798,11 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
final void notifyCommit() {
|
||||
if (manager != null) {
|
||||
if (queryOnly) {
|
||||
logger.notifyQueryOnly();
|
||||
manager.notifyOfQueryOnly(this);
|
||||
} else {
|
||||
manager.notifyOfCommit(this);
|
||||
logger.notifyCommit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -966,6 +961,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
|
||||
manager.notifyOfQueryOnly(this);
|
||||
} else {
|
||||
manager.notifyOfRollback(this, cause);
|
||||
logger.notifyRollback(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ public final class JtaTransaction extends JdbcTransaction {
|
||||
/**
|
||||
* Create the JtaTransaction.
|
||||
*/
|
||||
public JtaTransaction(String id, boolean explicit, UserTransaction utx, DataSource ds, TransactionManager manager) {
|
||||
super(id, explicit, null, manager);
|
||||
public JtaTransaction(boolean explicit, UserTransaction utx, DataSource ds, TransactionManager manager) {
|
||||
super(explicit, null, manager);
|
||||
userTransaction = utx;
|
||||
try {
|
||||
newTransaction = userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION;
|
||||
|
||||
+1
-2
@@ -105,8 +105,7 @@ public final class JtaTransactionManager implements ExternalTransactionManager {
|
||||
// This is a transaction that Ebean has not seen before.
|
||||
|
||||
// "wrap" it in a Ebean specific JtaTransaction
|
||||
String txnId = String.valueOf(System.currentTimeMillis());
|
||||
JtaTransaction newTrans = new JtaTransaction(txnId, true, ut, dataSource(), transactionManager);
|
||||
JtaTransaction newTrans = new JtaTransaction( true, ut, dataSource(), transactionManager);
|
||||
|
||||
// create and register transaction listener
|
||||
JtaTxnListener txnListener = createJtaTxnListener(newTrans);
|
||||
|
||||
@@ -117,10 +117,6 @@ final class NoTransaction implements SpiTransaction {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogPrefix() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogSql() {
|
||||
@@ -133,12 +129,17 @@ final class NoTransaction implements SpiTransaction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSql(String msg) {
|
||||
public void logSql(String... msg) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSummary(String msg) {
|
||||
public void logSummary(String... msg) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logTxn(String... args) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
+5
-10
@@ -21,7 +21,6 @@ final class SavepointTransaction extends SpiTransactionProxy {
|
||||
private final TransactionManager manager;
|
||||
private final Savepoint savepoint;
|
||||
private final Connection connection;
|
||||
private final String logPrefix;
|
||||
private final String spPrefix;
|
||||
|
||||
private boolean rollbackOnly;
|
||||
@@ -33,13 +32,12 @@ final class SavepointTransaction extends SpiTransactionProxy {
|
||||
this.transaction = transaction;
|
||||
this.connection = transaction.getInternalConnection();
|
||||
this.savepoint = connection.setSavepoint();
|
||||
if (manager.isTxnDebug()) {
|
||||
if (transaction.isLogSql()) {
|
||||
int savepointId = manager.isSupportsSavepointId() ? savepoint.getSavepointId() : 0;
|
||||
this.spPrefix = "sp[" + savepointId + "] ";
|
||||
} else {
|
||||
this.spPrefix = "sp[] ";
|
||||
}
|
||||
this.logPrefix = transaction.getLogPrefix() + spPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,17 +49,12 @@ final class SavepointTransaction extends SpiTransactionProxy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogPrefix() {
|
||||
return logPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSql(String msg) {
|
||||
public void logSql(String... msg) {
|
||||
transaction.logSql(Str.add(spPrefix, msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSummary(String msg) {
|
||||
public void logSummary(String... msg) {
|
||||
transaction.logSummary(Str.add(spPrefix, msg));
|
||||
}
|
||||
|
||||
@@ -99,6 +92,7 @@ final class SavepointTransaction extends SpiTransactionProxy {
|
||||
connection.releaseSavepoint(savepoint);
|
||||
state = STATE_COMMITTED;
|
||||
manager.notifyOfCommit(this);
|
||||
transaction.logTxn(spPrefix, "commit");
|
||||
} catch (SQLException e) {
|
||||
throw new PersistenceException("Error trying to commit/release Savepoint", e);
|
||||
}
|
||||
@@ -109,6 +103,7 @@ final class SavepointTransaction extends SpiTransactionProxy {
|
||||
connection.rollback(savepoint);
|
||||
state = STATE_ROLLED_BACK;
|
||||
manager.notifyOfRollback(this, cause);
|
||||
transaction.logTxn(spPrefix, "rollback");//TODO: Pass the cause
|
||||
} catch (SQLException e) {
|
||||
throw new PersistenceException("Error trying to rollback Savepoint", e);
|
||||
}
|
||||
|
||||
+10
-74
@@ -36,7 +36,6 @@ import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static java.lang.System.Logger.Level.DEBUG;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
@@ -63,8 +62,6 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
* Prefix for transaction id's (logging).
|
||||
*/
|
||||
final String prefix;
|
||||
private final String externalTransPrefix;
|
||||
private final AtomicLong counter = new AtomicLong(1000L);
|
||||
|
||||
/**
|
||||
* The dataSource of connections.
|
||||
@@ -96,8 +93,6 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
private final boolean skipCacheAfterWrite;
|
||||
private final TransactionFactory transactionFactory;
|
||||
private final SpiLogManager logManager;
|
||||
private final SpiLogger txnLogger;
|
||||
private final boolean txnDebug;
|
||||
private final DatabasePlatform databasePlatform;
|
||||
private final SpiProfileHandler profileHandler;
|
||||
private final TimedMetric txnMain;
|
||||
@@ -115,8 +110,6 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
public TransactionManager(TransactionManagerOptions options) {
|
||||
this.server = options.server;
|
||||
this.logManager = options.logManager;
|
||||
this.txnLogger = logManager.txn();
|
||||
this.txnDebug = txnLogger.isDebug();
|
||||
this.databasePlatform = options.config.getDatabasePlatform();
|
||||
this.supportsSavepointId = databasePlatform.supportsSavepointId();
|
||||
this.skipCacheAfterWrite = options.config.isSkipCacheAfterWrite();
|
||||
@@ -142,7 +135,6 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
this.profileHandler = options.profileHandler;
|
||||
this.bulkEventListenerMap = new BulkEventListenerMap(options.config.getBulkTableEventListeners());
|
||||
this.prefix = "";
|
||||
this.externalTransPrefix = "e";
|
||||
|
||||
CurrentTenantProvider tenantProvider = options.config.getCurrentTenantProvider();
|
||||
this.transactionFactory = TransactionFactoryBuilder.build(this, dataSourceSupplier, tenantProvider);
|
||||
@@ -273,14 +265,7 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
* Wrap the externally supplied Connection.
|
||||
*/
|
||||
public SpiTransaction wrapExternalConnection(Connection c) {
|
||||
return wrapExternalConnection(externalTransPrefix + c.hashCode(), c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an externally supplied Connection with a known transaction id.
|
||||
*/
|
||||
private SpiTransaction wrapExternalConnection(String id, Connection c) {
|
||||
ExternalJdbcTransaction t = new ExternalJdbcTransaction(id, true, c, this);
|
||||
ExternalJdbcTransaction t = new ExternalJdbcTransaction(true, c, this);
|
||||
// set the default batch mode
|
||||
t.setBatchMode(persistBatch);
|
||||
t.setBatchOnCascade(persistBatchOnCascade);
|
||||
@@ -313,14 +298,7 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
* Create a new transaction.
|
||||
*/
|
||||
SpiTransaction createTransaction(boolean explicit, Connection c) {
|
||||
return new JdbcTransaction(nextTxnId(), explicit, c, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next transaction id.
|
||||
*/
|
||||
String nextTxnId() {
|
||||
return txnDebug ? prefix + counter.incrementAndGet() : prefix;
|
||||
return new JdbcTransaction(explicit, c, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -328,17 +306,7 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
*/
|
||||
@Override
|
||||
public final void notifyOfRollback(SpiTransaction transaction, Throwable cause) {
|
||||
try {
|
||||
if (txnLogger.isDebug()) {
|
||||
String msg = transaction.getLogPrefix() + "Rollback";
|
||||
if (cause != null) {
|
||||
msg += " error: " + formatThrowable(cause);
|
||||
}
|
||||
txnLogger.debug(msg);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.log(ERROR, "Error while notifying TransactionEventListener of rollback event", ex);
|
||||
}
|
||||
// Do nothing now
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -349,38 +317,12 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
// Nothing that interesting here
|
||||
}
|
||||
|
||||
private String formatThrowable(Throwable e) {
|
||||
if (e == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
formatThrowable(e, sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void formatThrowable(Throwable e, StringBuilder sb) {
|
||||
sb.append(e.toString());
|
||||
StackTraceElement[] stackTrace = e.getStackTrace();
|
||||
if (stackTrace.length > 0) {
|
||||
sb.append(" stack0: ");
|
||||
sb.append(stackTrace[0]);
|
||||
}
|
||||
Throwable cause = e.getCause();
|
||||
if (cause != null) {
|
||||
sb.append(" cause: ");
|
||||
formatThrowable(cause, sb);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a local committed transaction.
|
||||
*/
|
||||
@Override
|
||||
public final void notifyOfCommit(SpiTransaction transaction) {
|
||||
try {
|
||||
if (txnLogger.isDebug()) {
|
||||
txnLogger.debug(transaction.getLogPrefix() + "Commit");
|
||||
}
|
||||
PostCommitProcessing postCommit = new PostCommitProcessing(clusterManager, this, transaction);
|
||||
postCommit.notifyLocalCache();
|
||||
backgroundExecutor.execute(postCommit.backgroundNotify());
|
||||
@@ -673,25 +615,18 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if Transaction debug is on.
|
||||
*/
|
||||
public final boolean isTxnDebug() {
|
||||
return txnDebug;
|
||||
public SpiTxnLogger logger() {
|
||||
return logManager.logger();
|
||||
}
|
||||
|
||||
public SpiTxnLogger loggerReadOnly() {
|
||||
return logManager.readOnlyLogger();
|
||||
}
|
||||
|
||||
public final SpiLogManager log() {
|
||||
return logManager;
|
||||
}
|
||||
|
||||
public final boolean isLogSql() {
|
||||
return logManager.sql().isDebug();
|
||||
}
|
||||
|
||||
public final boolean isLogSummary() {
|
||||
return logManager.sum().isDebug();
|
||||
}
|
||||
|
||||
/**
|
||||
* Experimental - find dirty beans in the persistence context and persist them.
|
||||
*/
|
||||
@@ -701,4 +636,5 @@ public class TransactionManager implements SpiTransactionManager {
|
||||
server.updateAll(dirtyBeans, transaction);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,4 +43,24 @@ public final class Str {
|
||||
return sb.append(s0).append(s1).toString();
|
||||
}
|
||||
|
||||
public static String add(String s0, String[] s1) {
|
||||
if (s1 == null || s1.length == 0) {
|
||||
return s0;
|
||||
}
|
||||
int len = s0.length();
|
||||
for (String s : s1) {
|
||||
if (s != null) {
|
||||
len += s.length();
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(len);
|
||||
sb.append(s0);
|
||||
for (String s : s1) {
|
||||
if (s != null) {
|
||||
sb.append(s);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user