#535 - Bean lazy loading can execute an extra query when one of the lazy loaded beans has been deleted

This commit is contained in:
Robin Bygrave committed 2016-01-21 20:51:32 +13:00
1 parent 64618da6a7
commit 6ab974a330
5 files changed
+55 -24

No files matched your search

@@ -4,6 +4,8 @@ import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
@@ -13,10 +15,14 @@ import java.util.List;
*/
public class LoadBeanRequest extends LoadRequest {
private static final Logger logger = LoggerFactory.getLogger(LoadBeanRequest.class);
private final List<EntityBeanIntercept> batch;
private final LoadBeanBuffer loadBuffer;
private final int lazyLoadPropertyIndex;
private final String lazyLoadProperty;
private final boolean loadCache;
@@ -24,21 +30,24 @@ public class LoadBeanRequest extends LoadRequest {
/**
* Construct for lazy load request.
*/
public LoadBeanRequest(LoadBeanBuffer LoadBuffer, String lazyLoadProperty, boolean loadCache) {
this(LoadBuffer, null, true, lazyLoadProperty, loadCache);
public LoadBeanRequest(LoadBeanBuffer LoadBuffer, int lazyLoadPropertyIndex, String lazyLoadProperty, boolean loadCache) {
this(LoadBuffer, null, true, lazyLoadPropertyIndex, lazyLoadProperty, loadCache);
}
/**
* Construct for secondary query.
*/
public LoadBeanRequest(LoadBeanBuffer LoadBuffer, OrmQueryRequest<?> parentRequest) {
this(LoadBuffer, parentRequest, false, null, false);
this(LoadBuffer, parentRequest, false, -1, null, false);
}
private LoadBeanRequest(LoadBeanBuffer loadBuffer, OrmQueryRequest<?> parentRequest, boolean lazy, String lazyLoadProperty, boolean loadCache) {
private LoadBeanRequest(LoadBeanBuffer loadBuffer, OrmQueryRequest<?> parentRequest, boolean lazy,
int lazyLoadPropertyIndex, String lazyLoadProperty, boolean loadCache) {
super(parentRequest, lazy);
this.loadBuffer = loadBuffer;
this.batch = loadBuffer.getBatch();
this.lazyLoadPropertyIndex = lazyLoadPropertyIndex;
this.lazyLoadProperty = lazyLoadProperty;
this.loadCache = loadCache;
}
@@ -143,10 +152,20 @@ public class LoadBeanRequest extends LoadRequest {
}
}
for (int i = 0; i < batch.size(); i++) {
// Check if the underlying row in DB was deleted. Mark this bean as 'failed' if
// necessary but allow processing to continue until it is accessed by client code
batch.get(i).checkLazyLoadFailure();
if (lazyLoadPropertyIndex > -1) {
// this is a lazy loading query so check for lazy loading failure (due to deleted rows)
for (int i = 0; i < batch.size(); i++) {
// check if the underlying row in DB was deleted. Mark the bean as 'failed' if
// necessary but allow processing to continue until it is accessed by client code
EntityBeanIntercept ebi = batch.get(i);
// all beans in the batch should have this property loaded now
if (ebi.isLazyLoadFailure(lazyLoadPropertyIndex)) {
BeanDescriptor<?> desc = loadBuffer.getBeanDescriptor();
Object beanId = desc.getId(ebi.getOwner());
ebi.setOwnerId(beanId);
logger.info("Lazy loading unsuccessful for type:" + desc.getName() + " id:" + beanId + " - expecting when bean has been deleted");
}
}
}
}
}
@@ -183,7 +183,7 @@ public class LoadManyRequest extends LoadRequest {
if (logger.isDebugEnabled()) {
EntityBean ownerBean = bc.getOwnerBean();
Object parentId = desc.getId(ownerBean);
logger.debug("BeanCollection after lazy load was empty. type[" + ownerBean.getClass() + "] id[" + parentId + "] owner[" + ownerBean + "]");
logger.debug("BeanCollection after lazy load was empty. type:" + ownerBean.getClass().getName() + " id:" + parentId + " owner:" + ownerBean);
}
} else if (isLoadCache()) {
Object parentId = desc.getId(bc.getOwnerBean());
@@ -85,7 +85,7 @@ public class DefaultBeanLoader {
SpiQuery<?> query = loadRequest.createQuery(server, batchSize);
executeLazyLoadQuery(loadRequest, query);
executeQuery(loadRequest, query);
loadRequest.postLoad();
@@ -214,7 +214,7 @@ public class DefaultBeanLoader {
query.where().idIn(idList);
}
List<?> list = executeLazyLoadQuery(loadRequest, query);
List<?> list = executeQuery(loadRequest, query);
loadRequest.postLoad(list);
@@ -225,7 +225,7 @@ public class DefaultBeanLoader {
/**
* Execute the lazy load query taking into account MySql transaction oddness.
*/
private List<?> executeLazyLoadQuery(LoadRequest loadRequest, SpiQuery<?> query) {
private List<?> executeQuery(LoadRequest loadRequest, SpiQuery<?> query) {
if (onIterateUseExtraTxn && loadRequest.isParentFindIterate()) {
// MySql - we need a different transaction to execute the secondary query
SpiTransaction extraTxn = server.createQueryTransaction();
@@ -190,7 +190,7 @@ public class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContex
}
}
LoadBeanRequest req = new LoadBeanRequest(this, ebi.getLazyLoadProperty(), context.hitCache);
LoadBeanRequest req = new LoadBeanRequest(this, ebi.getLazyLoadPropertyIndex(), ebi.getLazyLoadProperty(), context.hitCache);
context.desc.getEbeanServer().loadBean(req);
}