Refactor transaction logging to simple SLF4J use

This commit is contained in:
Robin Bygrave committed 2013-04-15 23:16:08 +12:00
1 parent b5ef07bab9
commit cbd7b5e912
60 files changed
+2976 -4529

No files matched your search

@@ -237,7 +237,7 @@ public final class BatchControl {
Arrays.sort(bsArray, depthComparator);
if (transaction.isLogSummary()) {
transaction.logInternal("BatchControl flush " + Arrays.toString(bsArray));
transaction.logSummary("BatchControl flush " + Arrays.toString(bsArray));
}
for (int i = 0; i < bsArray.length; i++) {
BatchedBeanHolder bs = bsArray[i];
@@ -498,7 +498,7 @@ public final class DefaultPersister implements Persister {
if (idList != null) {
q.where().idIn(idList);
if (t.isLogSummary()) {
t.logInternal("-- DeleteById of " + descriptor.getName() + " ids[" + idList + "] requires fetch of foreign key values");
t.logSummary("-- DeleteById of " + descriptor.getName() + " ids[" + idList + "] requires fetch of foreign key values");
}
List<?> beanList = server.findList(q, t);
deleteList(beanList, t);
@@ -507,7 +507,7 @@ public final class DefaultPersister implements Persister {
} else {
q.where().idEq(id);
if (t.isLogSummary()) {
t.logInternal("-- DeleteById of " + descriptor.getName() + " id[" + id + "] requires fetch of foreign key values");
t.logSummary("-- DeleteById of " + descriptor.getName() + " id[" + id + "] requires fetch of foreign key values");
}
Object bean = server.findUnique(q, t);
if (bean == null) {
@@ -555,7 +555,7 @@ public final class DefaultPersister implements Persister {
for (int i = 0; i < manys.length; i++) {
SqlUpdate sqlDelete = manys[i].deleteByParentId(id, idList);
if (t.isLogSummary()) {
t.logInternal("-- Deleting intersection table entries: " + manys[i].getFullBeanName());
t.logSummary("-- Deleting intersection table entries: " + manys[i].getFullBeanName());
}
executeSqlUpdate(sqlDelete, t);
}
@@ -563,7 +563,7 @@ public final class DefaultPersister implements Persister {
// delete the bean(s)
SqlUpdate deleteById = descriptor.deleteById(id, idList);
if (t.isLogSummary()) {
t.logInternal("-- Deleting " + descriptor.getName() + " Ids" + idList);
t.logSummary("-- Deleting " + descriptor.getName() + " Ids" + idList);
}
// use Id's to update L2 cache rather than Bulk table event
@@ -1020,7 +1020,7 @@ public final class DefaultPersister implements Persister {
if (deletions != null && deletions.remove(otherBean)) {
String m = "Inserting and Deleting same object? " + otherBean;
if (t.isLogSummary()) {
t.logInternal(m);
t.logSummary(m);
}
logger.warn(m);
@@ -68,7 +68,7 @@ public class DeleteUnloadedForeignKeys {
SpiTransaction t = request.getTransaction();
if (t.isLogSummary()) {
t.logInternal("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
t.logSummary("-- Ebean fetching foreign key values for delete of " + descriptor.getName() + " id:" + id);
}
beanWithForeignKeys = server.findUnique(q, t);
}
@@ -95,7 +95,7 @@ public class ExeCallableSql {
} else {
if (logSql){
t.logInternal(sql);
t.logSql(sql);
}
cstmt = pstmtFactory.getCstmt(t, sql);
}
@@ -123,7 +123,7 @@ public class ExeOrmUpdate {
} else {
if (logSql){
t.logInternal(sql);
t.logSql(sql);
}
pstmt = pstmtFactory.getPstmt(t, sql);
}
@@ -115,7 +115,7 @@ public class ExeUpdateSql {
} else {
if (logSql){
t.logInternal(sql);
t.logSql(sql);
}
pstmt = pstmtFactory.getPstmt(t, sql);
}
@@ -54,7 +54,7 @@ public class PstmtFactory {
}
if (logSql){
t.logInternal(sql);
t.logSql(sql);
}
Connection conn = t.getInternalConnection();
@@ -83,7 +83,7 @@ public class PstmtFactory {
}
if (logSql){
t.logInternal(sql);
t.logSql(sql);
}
Connection conn = t.getInternalConnection();
@@ -36,23 +36,14 @@ public class DeleteHandler extends DmlHandler {
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Delete [");
bindLogAppend(meta.getTableName());
bindLogAppend("] where[");
meta.bind(persistRequest, this);
bindLogAppend("]");
// log the binding to transaction log if requested
logBinding();
logSql(sql);
}
/**
@@ -91,7 +91,7 @@ public final class DmlBeanPersister implements BeanPersister {
String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r","\n"}, "\\n ");
String msg = "ERROR executing DML bindLog["+handler.getBindLog()+"] error["+errMsg+"]";
if (request.getTransaction().isLogSummary()) {
request.getTransaction().logInternal(msg);
request.getTransaction().logSummary(msg);
}
throw new PersistenceException(msg, e);
@@ -16,47 +16,43 @@ import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.persist.BatchedPstmt;
import com.avaje.ebeaninternal.server.persist.BatchedPstmtHolder;
import com.avaje.ebeaninternal.server.persist.dmlbind.BindableRequest;
import com.avaje.ebeaninternal.server.transaction.TransactionManager;
import com.avaje.ebeaninternal.server.type.DataBind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class for Handler implementations.
*/
public abstract class DmlHandler implements PersistHandler, BindableRequest {
private static final Logger logger = LoggerFactory.getLogger(DmlHandler.class);
/**
* The originating request.
*/
protected final PersistRequestBean<?> persistRequest;
protected final StringBuilder bindLog;
/**
* The originating request.
*/
protected final PersistRequestBean<?> persistRequest;
protected final Set<String> loadedProps;
protected final StringBuilder bindLog;
protected final SpiTransaction transaction;
protected final boolean emptyStringToNull;
protected final boolean logLevelSql;
protected final Set<String> loadedProps;
/**
* The PreparedStatement used for the dml.
*/
protected DataBind dataBind;
protected String sql;
protected ArrayList<UpdateGenValue> updateGenValues;
private Set<String> additionalProps;
protected final SpiTransaction transaction;
// private boolean checkDelta;
//
// private BeanDelta deltaBean;
protected final boolean emptyStringToNull;
protected final boolean logLevelSql;
/**
* The PreparedStatement used for the dml.
*/
protected DataBind dataBind;
protected String sql;
protected ArrayList<UpdateGenValue> updateGenValues;
private Set<String> additionalProps;
protected DmlHandler(PersistRequestBean<?> persistRequest, boolean emptyStringToNull) {
this.persistRequest = persistRequest;
@@ -71,329 +67,308 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
}
}
// protected void setCheckDelta(boolean checkDelta) {
// this.checkDelta = checkDelta;
// }
public PersistRequestBean<?> getPersistRequest() {
return persistRequest;
}
/**
* Get the sql and bind the statement.
*/
public abstract void bind() throws SQLException;
/**
* Execute now for non-batch execution.
*/
public abstract void execute() throws SQLException;
/**
* Get the sql and bind the statement.
*/
public abstract void bind() throws SQLException;
/**
* Check the rowCount.
*/
protected void checkRowCount(int rowCount) throws SQLException, OptimisticLockException {
try {
persistRequest.checkRowCount(rowCount);
persistRequest.postExecute();
} catch (OptimisticLockException e){
// add the SQL and bind values to error message
String m = e.getMessage()+" sql["+sql+"] bind["+bindLog+"]";
persistRequest.getTransaction().log("OptimisticLockException:"+m);
throw new OptimisticLockException(m, null, e.getEntity());
}
}
/**
* Add this for batch execution.
*/
public void addBatch() throws SQLException {
PstmtBatch pstmtBatch = persistRequest.getPstmtBatch();
if (pstmtBatch != null){
pstmtBatch.addBatch(dataBind.getPstmt());
} else {
dataBind.getPstmt().addBatch();
}
}
/**
* Execute now for non-batch execution.
*/
public abstract void execute() throws SQLException;
/**
* Close the underlying statement.
*/
public void close() {
try {
if (dataBind != null){
dataBind.close();
}
} catch (SQLException ex) {
logger.error(null, ex);
}
}
/**
* Return the bind log.
*/
public String getBindLog() {
return bindLog == null ? "" : bindLog.toString();
}
/**
* Set the Id value that was bound. This value is used for logging summary
* level information.
*/
public void setIdValue(Object idValue) {
persistRequest.setBoundId(idValue);
}
/**
* Log the bind information to the transaction log.
*/
protected void logBinding() {
if (logLevelSql) {
transaction.logInternal(bindLog.toString());
}
}
/**
* Log the sql to the transaction log.
*/
protected void logSql(String sql) {
if (logLevelSql) {
transaction.logInternal(sql);
}
}
public boolean isIncluded(BeanProperty prop) {
return (loadedProps == null || loadedProps.contains(prop.getName()));
}
public boolean isIncludedWhere(BeanProperty prop) {
if (prop.isDbEncrypted()){
// update without a version property ...
// for encrypted properties only include if it was
// also an updated/modified property
return isIncluded(prop);
}
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
/**
* Check the rowCount.
*/
protected void checkRowCount(int rowCount) throws SQLException, OptimisticLockException {
try {
persistRequest.checkRowCount(rowCount);
persistRequest.postExecute();
} catch (OptimisticLockException e) {
// add the SQL and bind values to error message
String m = e.getMessage() + " sql[" + sql + "] bind[" + bindLog + "]";
persistRequest.getTransaction().logSummary("OptimisticLockException:" + m);
throw new OptimisticLockException(m, null, e.getEntity());
}
/**
* Bind a raw value. Used to bind the discriminator column.
*/
public Object bind(String propName, Object value, int sqlType) throws SQLException {
if (logLevelSql) {
bindLog.append(propName).append("=");
bindLog.append(value).append(", ");
}
dataBind.setObject(value, sqlType);
return value;
}
}
public Object bindNoLog(Object value, int sqlType, String logPlaceHolder) throws SQLException {
if (logLevelSql) {
bindLog.append(logPlaceHolder).append(" ");
}
dataBind.setObject(value, sqlType);
return value;
/**
* Add this for batch execution.
*/
public void addBatch() throws SQLException {
PstmtBatch pstmtBatch = persistRequest.getPstmtBatch();
if (pstmtBatch != null) {
pstmtBatch.addBatch(dataBind.getPstmt());
} else {
dataBind.getPstmt().addBatch();
}
}
/**
* Bind the value to the preparedStatement.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
return bindInternal(logLevelSql, value, prop, propName, bindNull);
/**
* Close the underlying statement.
*/
public void close() {
try {
if (dataBind != null) {
dataBind.close();
}
} catch (SQLException ex) {
logger.error(null, ex);
}
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
return bindInternal(false, value, prop, propName, bindNull);
}
/**
* Return the bind log.
*/
public String getBindLog() {
return bindLog == null ? "" : bindLog.toString();
}
/**
* Set the Id value that was bound. This value is used for logging summary
* level information.
*/
public void setIdValue(Object idValue) {
persistRequest.setBoundId(idValue);
}
/**
* Log the sql to the transaction log.
*/
protected void logSql(String sql) {
if (logLevelSql) {
if (TransactionManager.SQL_LOGGER.isTraceEnabled()) {
sql += "; --bind(" + bindLog + ")";
}
transaction.logSql(sql);
}
private Object bindInternal(boolean log, Object value, BeanProperty prop, String propName, boolean bindNull) throws SQLException {
if (!bindNull){
if (emptyStringToNull && (value instanceof String) && ((String)value).length() == 0){
// support Oracle conversion of empty string to null
//value = prop.getDbNullValue(value);
value = null;
}
}
}
if (!bindNull && value == null) {
// where will have IS NULL clause so don't actually bind
if (log) {
bindLog.append(propName).append("=");
bindLog.append("null, ");
}
} else {
if (log) {
bindLog.append(propName).append("=");
if (prop.isLob()){
bindLog.append("[LOB]");
} else {
String sv = String.valueOf(value);
if (sv.length() > 50){
sv = sv.substring(0,47)+"...";
}
bindLog.append(sv);
}
bindLog.append(", ");
}
// do the actual binding to PreparedStatement
prop.bind(dataBind, value);
// if (checkDelta) {
// if (!prop.isId() && prop.isDeltaRequired()){
// if (deltaBean == null){
// deltaBean = persistRequest.createDeltaBean();
// transaction.getEvent().addBeanDelta(deltaBean);
// }
// deltaBean.add(prop, value);
// }
// }
}
return value;
}
/**
* Add the comment to the bind information log.
*/
protected void bindLogAppend(String comment) {
if (logLevelSql) {
bindLog.append(comment);
}
}
public boolean isIncluded(BeanProperty prop) {
return (loadedProps == null || loadedProps.contains(prop.getName()));
}
/**
* For generated properties set on insert register as additional
* loaded properties if required.
*/
public final void registerAdditionalProperty(String propertyName) {
if (loadedProps != null && !loadedProps.contains(propertyName)){
if (additionalProps == null){
additionalProps = new HashSet<String>();
}
additionalProps.add(propertyName);
}
public boolean isIncludedWhere(BeanProperty prop) {
if (prop.isDbEncrypted()) {
// update without a version property ...
// for encrypted properties only include if it was
// also an updated/modified property
return isIncluded(prop);
}
return prop.isDbUpdatable() && (loadedProps == null || loadedProps.contains(prop.getName()));
}
/**
* Set any additional (generated) properties to the set of loaded properties
* if required.
*/
protected void setAdditionalProperties() {
if (additionalProps != null){
// additional generated properties set on insert
// added to the set of loaded properties
additionalProps.addAll(loadedProps);
persistRequest.setLoadedProps(additionalProps);
}
}
/**
* Register a generated value on a update. This can not be set to the bean
* until after the where clause has been bound for concurrency checking.
* <p>
* GeneratedProperty values are likely going to be used for optimistic
* concurrency checking. This includes 'counter' and 'update timestamp'
* generation.
* </p>
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value) {
if (updateGenValues == null) {
updateGenValues = new ArrayList<UpdateGenValue>();
}
updateGenValues.add(new UpdateGenValue(prop, bean, value));
registerAdditionalProperty(prop.getName());
}
/**
* Set any update generated values to the bean. Must be called after where
* clause has been bound.
*/
public void setUpdateGenValues() {
if (updateGenValues != null) {
for (int i = 0; i < updateGenValues.size(); i++) {
UpdateGenValue updGenVal = updateGenValues.get(i);
updGenVal.setValue();
}
}
}
/**
* Check with useGeneratedKeys to get appropriate PreparedStatement.
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, boolean genKeys) throws SQLException {
Connection conn = t.getInternalConnection();
if (genKeys) {
// the Id generated is always the first column
// Required to stop Oracle10 giving us Oracle rowId??
// Other jdbc drivers seem fine without this hint.
int[] columns = {1};
return conn.prepareStatement(sql, columns);
/**
* Bind a raw value. Used to bind the discriminator column.
*/
public Object bind(String propName, Object value, int sqlType) throws SQLException {
if (logLevelSql) {
if (value == null) {
bindLog.append("null");
} else {
String sval = value.toString();
if (sval.length() > 50) {
bindLog.append(sval.substring(0, 47)).append("...");
} else {
return conn.prepareStatement(sql);
bindLog.append(sval);
}
}
}
bindLog.append(",");
}
dataBind.setObject(value, sqlType);
return value;
}
/**
* Return a prepared statement taking into account batch requirements.
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, PersistRequestBean<?> request, boolean genKeys)
throws SQLException {
public Object bindNoLog(Object value, int sqlType, String logPlaceHolder) throws SQLException {
if (logLevelSql) {
bindLog.append(logPlaceHolder).append(" ");
}
dataBind.setObject(value, sqlType);
return value;
}
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, request);
/**
* Bind the value to the preparedStatement.
*/
public Object bind(Object value, BeanProperty prop, String propName, boolean bindNull)
throws SQLException {
return bindInternal(logLevelSql, value, prop, propName, bindNull);
}
if (stmt != null) {
return stmt;
}
/**
* Bind the value to the preparedStatement without logging.
*/
public Object bindNoLog(Object value, BeanProperty prop, String propName, boolean bindNull)
throws SQLException {
return bindInternal(false, value, prop, propName, bindNull);
}
if (logLevelSql){
t.logInternal(sql);
}
stmt = getPstmt(t, sql, genKeys);
private Object bindInternal(boolean log, Object value, BeanProperty prop, String propName,
boolean bindNull) throws SQLException {
PstmtBatch pstmtBatch = request.getPstmtBatch();
if (pstmtBatch != null){
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
if (!bindNull) {
if (emptyStringToNull && (value instanceof String) && ((String) value).length() == 0) {
// support Oracle conversion of empty string to null
// value = prop.getDbNullValue(value);
value = null;
}
}
if (!bindNull && value == null) {
// where will have IS NULL clause so don't actually bind
if (log) {
bindLog.append("null, ");
}
} else {
if (log) {
if (prop.isLob()) {
bindLog.append("[LOB]");
} else {
String sv = String.valueOf(value);
if (sv.length() > 50) {
sv = sv.substring(0, 47) + "...";
}
bindLog.append(sv);
}
BatchedPstmt bs = new BatchedPstmt(stmt, genKeys, sql, request.getPstmtBatch(), true);
batch.addStmt(bs, request);
return stmt;
}
/**
* Hold the values from GeneratedValue that need to be set to the bean
* property after the where clause has been built.
*/
private static final class UpdateGenValue {
bindLog.append(",");
}
// do the actual binding to PreparedStatement
prop.bind(dataBind, value);
}
return value;
}
private final BeanProperty property;
/**
* For generated properties set on insert register as additional loaded
* properties if required.
*/
public final void registerAdditionalProperty(String propertyName) {
if (loadedProps != null && !loadedProps.contains(propertyName)) {
if (additionalProps == null) {
additionalProps = new HashSet<String>();
}
additionalProps.add(propertyName);
}
}
private final Object bean;
/**
* Set any additional (generated) properties to the set of loaded properties
* if required.
*/
protected void setAdditionalProperties() {
if (additionalProps != null) {
// additional generated properties set on insert
// added to the set of loaded properties
additionalProps.addAll(loadedProps);
persistRequest.setLoadedProps(additionalProps);
}
}
private final Object value;
/**
* Register a generated value on a update. This can not be set to the bean
* until after the where clause has been bound for concurrency checking.
* <p>
* GeneratedProperty values are likely going to be used for optimistic
* concurrency checking. This includes 'counter' and 'update timestamp'
* generation.
* </p>
*/
public void registerUpdateGenValue(BeanProperty prop, Object bean, Object value) {
if (updateGenValues == null) {
updateGenValues = new ArrayList<UpdateGenValue>();
}
updateGenValues.add(new UpdateGenValue(prop, bean, value));
registerAdditionalProperty(prop.getName());
}
private UpdateGenValue(BeanProperty property, Object bean, Object value) {
this.property = property;
this.bean = bean;
this.value = value;
}
/**
* Set any update generated values to the bean. Must be called after where
* clause has been bound.
*/
public void setUpdateGenValues() {
if (updateGenValues != null) {
for (int i = 0; i < updateGenValues.size(); i++) {
UpdateGenValue updGenVal = updateGenValues.get(i);
updGenVal.setValue();
}
}
}
/**
* Set the value to the bean property.
*/
private void setValue() {
// support PropertyChangeSupport
property.setValueIntercept(bean, value);
}
}
/**
* Check with useGeneratedKeys to get appropriate PreparedStatement.
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, boolean genKeys)
throws SQLException {
Connection conn = t.getInternalConnection();
if (genKeys) {
// the Id generated is always the first column
// Required to stop Oracle10 giving us Oracle rowId??
// Other jdbc drivers seem fine without this hint.
int[] columns = { 1 };
return conn.prepareStatement(sql, columns);
} else {
return conn.prepareStatement(sql);
}
}
/**
* Return a prepared statement taking into account batch requirements.
*/
protected PreparedStatement getPstmt(SpiTransaction t, String sql, PersistRequestBean<?> request,
boolean genKeys) throws SQLException {
BatchedPstmtHolder batch = t.getBatchControl().getPstmtHolder();
PreparedStatement stmt = batch.getStmt(sql, request);
if (stmt != null) {
return stmt;
}
if (logLevelSql) {
t.logSql(sql);
}
stmt = getPstmt(t, sql, genKeys);
PstmtBatch pstmtBatch = request.getPstmtBatch();
if (pstmtBatch != null) {
pstmtBatch.setBatchSize(stmt, t.getBatchControl().getBatchSize());
}
BatchedPstmt bs = new BatchedPstmt(stmt, genKeys, sql, request.getPstmtBatch(), true);
batch.addStmt(bs, request);
return stmt;
}
/**
* Hold the values from GeneratedValue that need to be set to the bean
* property after the where clause has been built.
*/
private static final class UpdateGenValue {
private final BeanProperty property;
private final Object bean;
private final Object value;
private UpdateGenValue(BeanProperty property, Object bean, Object value) {
this.property = property;
this.bean = bean;
this.value = value;
}
/**
* Set the value to the bean property.
*/
private void setValue() {
// support PropertyChangeSupport
property.setValueIntercept(bean, value);
}
}
}
@@ -101,22 +101,15 @@ public class InsertHandler extends DmlHandler {
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, useGeneratedKeys);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, useGeneratedKeys);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Insert [");
bindLogAppend(desc.getBaseTable());
bindLogAppend("] set[");
// bind the bean property values
meta.bind(this, bean, withId);
bindLogAppend("]");
logBinding();
logSql(sql);
}
/**
@@ -52,23 +52,16 @@ public class UpdateHandler extends DmlHandler {
PreparedStatement pstmt;
if (isBatch) {
pstmt = getPstmt(t, sql, persistRequest, false);
} else {
logSql(sql);
pstmt = getPstmt(t, sql, false);
}
dataBind = new DataBind(pstmt);
bindLogAppend("Binding Update [");
bindLogAppend(meta.getTableName());
bindLogAppend("] ");
meta.bind(persistRequest, this, updatePlan);
setUpdateGenValues();
bindLogAppend("]");
logBinding();
logSql(sql);
}
@Override
@@ -74,12 +74,8 @@ public final class UpdateMeta {
Object bean = persist.getBean();
bind.bindLogAppend(" set[");
// bind.setCheckDelta(true);
updatePlan.bindSet(bind, bean);
// bind.setCheckDelta(false);
bind.bindLogAppend("] where[");
id.dmlBind(bind, false, bean);
switch (persist.getConcurrencyMode()) {