#390 - ENH: Add Change log mechanism for easy fine grained logging of insert, update, delete activity

This commit is contained in:
Robin Bygrave committed 2015-08-20 20:46:28 +12:00
1 parent 46b158a0a0
commit 84190f3c2a
34 files changed
+1642 -84

No files matched your search

@@ -3,6 +3,9 @@ package com.avaje.ebeaninternal.server.transaction;
import com.avaje.ebean.TransactionCallback;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebean.config.PersistBatch;
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
import com.avaje.ebean.event.changelog.BeanChange;
import com.avaje.ebean.event.changelog.ChangeSet;
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.api.TransactionEvent;
@@ -10,7 +13,6 @@ import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
import com.avaje.ebeaninternal.server.lib.util.Str;
import com.avaje.ebeaninternal.server.persist.BatchControl;
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -19,7 +21,12 @@ import javax.persistence.RollbackException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
/**
* JDBC Connection based transaction.
@@ -133,6 +140,8 @@ public class JdbcTransaction implements SpiTransaction {
protected boolean batchOnCascadeSet;
protected TChangeLogHolder changeLogHolder;
/**
* Create a new JdbcTransaction.
*/
@@ -180,6 +189,19 @@ public class JdbcTransaction implements SpiTransaction {
return logPrefix;
}
@Override
public void addBeanChange(BeanChange beanChange) {
if (changeLogHolder == null) {
changeLogHolder = new TChangeLogHolder(this, 100);
}
changeLogHolder.addBeanChange(beanChange);
}
@Override
public void sendChangeLog(ChangeSet changesRequest) {
manager.sendChangeLog(changesRequest);
}
@Override
public void register(TransactionCallback callback) {
if (callbackList == null) {
@@ -210,6 +232,9 @@ public class JdbcTransaction implements SpiTransaction {
}
}
}
if (changeLogHolder != null) {
changeLogHolder.postRollback();
}
}
protected void firePreCommit() {
@@ -234,6 +259,9 @@ public class JdbcTransaction implements SpiTransaction {
}
}
}
if (changeLogHolder != null) {
changeLogHolder.postCommit();
}
}
@Override
@@ -0,0 +1,88 @@
package com.avaje.ebeaninternal.server.transaction;
import com.avaje.ebean.event.changelog.BeanChange;
import com.avaje.ebean.event.changelog.ChangeSet;
import com.avaje.ebean.event.changelog.TxnState;
import com.avaje.ebeaninternal.api.SpiTransaction;
import java.util.UUID;
/**
* Holder of the changes handling the case when we send the changes
* prior to commit or rollback as we hit the allowed 'batch size'.
*/
public class TChangeLogHolder {
/**
* The owning transaction.
*/
private final SpiTransaction owner;
/**
* A transaction id that can be used to join many changeSets when
* we send multiple for a large transaction.
*/
private final String transactionId;
/**
* When we hit batch size then send the changeSet even when the tranaction
* has not yet completed.
*/
private final int batchSize;
/**
* The changes we collect to send to the listener.
*/
private ChangeSet changes;
private long batchId;
/**
* Counter to check when we hit the batch size.
*/
private int count;
/**
* Construct with the owning transaction and batch size to use.
*/
public TChangeLogHolder(SpiTransaction owner, int batchSize) {
this.owner = owner;
this.transactionId = UUID.randomUUID().toString();
this.batchSize = batchSize;
this.changes = new ChangeSet(transactionId, 0);
}
/**
* Add a bean change to the change set.
*/
public void addBeanChange(BeanChange change) {
changes.addBeanChange(change);
if (++count >= batchSize) {
// we hit the batch size so send what we have knowing
// that the transaction has not completed yet and
// reset the changes and count
owner.sendChangeLog(changes);
changes = new ChangeSet(transactionId, ++batchId);
count = 0;
}
}
/**
* On post commit send the changes we have collected.
*/
public void postCommit() {
changes.setTxnState(TxnState.COMMITTED);
owner.sendChangeLog(changes);
}
/**
* On post rollback send the changes we have collected and
* leave it up to the listener to decide what to do.
*/
public void postRollback() {
changes.setTxnState(TxnState.ROLLBACK);
owner.sendChangeLog(changes);
}
}
@@ -6,6 +6,9 @@ import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
import com.avaje.ebean.dbmigration.DbOffline;
import com.avaje.ebean.event.TransactionEventListener;
import com.avaje.ebean.event.changelog.ChangeLogListener;
import com.avaje.ebean.event.changelog.ChangeLogPrepare;
import com.avaje.ebean.event.changelog.ChangeSet;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.api.TransactionEvent;
import com.avaje.ebeaninternal.api.TransactionEventTable;
@@ -80,6 +83,17 @@ public class TransactionManager {
protected final TransactionEventListener[] transactionEventListeners;
/**
* Used to prepare the change set setting user context information in the
* foreground thread before logging.
*/
private final ChangeLogPrepare changeLogPrepare;
/**
* Performs the actual logging of the change set in background.
*/
private final ChangeLogListener changeLogListener;
/**
* Create the TransactionManager
*/
@@ -89,6 +103,8 @@ public class TransactionManager {
this.persistBatch = config.getPersistBatch();
this.persistBatchOnCascade = config.appliedPersistBatchOnCascade();
this.beanDescriptorManager = descMgr;
this.changeLogPrepare = descMgr.getChangeLogPrepare();
this.changeLogListener = descMgr.getChangeLogListener();
this.clusterManager = clusterManager;
this.serverName = config.getName();
this.backgroundExecutor = backgroundExecutor;
@@ -439,4 +455,21 @@ public class TransactionManager {
}
}
/**
* Prepare and then send/log the changeSet.
*/
public void sendChangeLog(final ChangeSet changeSet) {
// can set userId, userIpAddress & userContext if desired
if (changeLogPrepare.prepare(changeSet)) {
// call the log method in background
backgroundExecutor.execute(new Runnable() {
@Override
public void run() {
changeLogListener.log(changeSet);
}
});
}
}
}