mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Fix for issue 46 - Persistence Context Cache not cleared when deleting a collection of entities in a running transaction
This commit is contained in:
1 parent
1a9a4c957c
commit
c6019bf3c5
5 files changed
+172
-10
No files matched your search
@@ -50,6 +50,7 @@ import com.avaje.ebean.bean.CallStack;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.bean.PersistenceContext.WithOption;
|
||||
import com.avaje.ebean.cache.ServerCacheManager;
|
||||
import com.avaje.ebean.config.EncryptKeyManager;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
@@ -1136,9 +1137,14 @@ public final class DefaultServer implements SpiEbeanServer {
|
||||
// first look in the persistence context
|
||||
context = t.getPersistenceContext();
|
||||
if (context != null) {
|
||||
Object o = context.get(beanDescriptor.getBeanType(), query.getId());
|
||||
WithOption o = context.getWithOption(beanDescriptor.getBeanType(), query.getId());
|
||||
if (o != null) {
|
||||
return (T) o;
|
||||
if (o.isDeleted()) {
|
||||
// Bean was previously deleted in the same transaction / persistence context
|
||||
return null;
|
||||
}
|
||||
// Return the entity bean instance from the persistence context
|
||||
return (T) o.getBean();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1148,6 +1154,7 @@ public final class DefaultServer implements SpiEbeanServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Hit the L2 bean cache
|
||||
Object cachedBean = beanDescriptor.cacheGetBean(query.getId(), query.isReadOnly());
|
||||
if (cachedBean != null) {
|
||||
if (context == null) {
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.bean.BeanCollection.ModifyListenMode;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.ebeaninternal.api.SpiUpdate;
|
||||
@@ -38,6 +39,7 @@ import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import com.avaje.ebeaninternal.server.deploy.IntersectionRow;
|
||||
import com.avaje.ebeaninternal.server.deploy.ManyType;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -538,7 +540,18 @@ public final class DefaultPersister implements Persister {
|
||||
} else {
|
||||
t.getEvent().addDeleteById(descriptor, id);
|
||||
}
|
||||
return executeSqlUpdate(deleteById, t);
|
||||
int rows = executeSqlUpdate(deleteById, t);
|
||||
|
||||
// Delete from the persistence context so that it can't be fetched again later
|
||||
PersistenceContext persistenceContext = ((SpiTransaction)t).getPersistenceContext();
|
||||
if (idList != null) {
|
||||
for (Object idValue : idList) {
|
||||
persistenceContext.deleted(descriptor.getBeanType(), idValue);
|
||||
}
|
||||
} else {
|
||||
persistenceContext.deleted(descriptor.getBeanType(), id);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+39
-7
@@ -1,9 +1,11 @@
|
||||
package com.avaje.ebeaninternal.server.transaction;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebeaninternal.api.Monitor;
|
||||
@@ -64,6 +66,12 @@ public final class DefaultPersistenceContext implements PersistenceContext {
|
||||
return getClassContext(beanType).get(id);
|
||||
}
|
||||
}
|
||||
|
||||
public WithOption getWithOption(Class<?> beanType, Object id) {
|
||||
synchronized (monitor) {
|
||||
return getClassContext(beanType).getWithOption(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of beans of the given type in the persistence context.
|
||||
@@ -93,11 +101,19 @@ public final class DefaultPersistenceContext implements PersistenceContext {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleted(Class<?> beanType, Object id) {
|
||||
synchronized (monitor) {
|
||||
ClassContext classMap = typeCache.get(beanType.getName());
|
||||
if (classMap != null && id != null) {
|
||||
classMap.deleted(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear(Class<?> beanType, Object id) {
|
||||
synchronized (monitor) {
|
||||
ClassContext classMap = typeCache.get(beanType.getName());
|
||||
if (classMap != null && id != null) {
|
||||
//id = getUid(beanType, id);
|
||||
classMap.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -130,16 +146,24 @@ public final class DefaultPersistenceContext implements PersistenceContext {
|
||||
|
||||
private static class ClassContext {
|
||||
|
||||
private final WeakValueMap<Object, Object> map = new WeakValueMap<Object, Object>();
|
||||
|
||||
private final WeakValueMap<Object, Object> map = new WeakValueMap<Object, Object>();
|
||||
private Set<Object> deleteSet;
|
||||
|
||||
private WithOption getWithOption(Object id){
|
||||
if (deleteSet != null && deleteSet.contains(id)) {
|
||||
return WithOption.DELETED;
|
||||
}
|
||||
Object bean = map.get(id);
|
||||
return (bean == null) ? null : new WithOption(bean);
|
||||
}
|
||||
|
||||
private Object get(Object id){
|
||||
return map.get(id);
|
||||
}
|
||||
|
||||
private Object putIfAbsent(Object id, Object bean){
|
||||
|
||||
return map.putIfAbsent(id, bean);
|
||||
}
|
||||
private Object putIfAbsent(Object id, Object bean){
|
||||
return map.putIfAbsent(id, bean);
|
||||
}
|
||||
|
||||
private void put(Object id, Object b){
|
||||
map.put(id, b);
|
||||
@@ -156,6 +180,14 @@ public final class DefaultPersistenceContext implements PersistenceContext {
|
||||
private Object remove(Object id){
|
||||
return map.remove(id);
|
||||
}
|
||||
|
||||
private void deleted(Object id){
|
||||
if (deleteSet == null) {
|
||||
deleteSet = new HashSet<Object>();
|
||||
}
|
||||
deleteSet.add(id);
|
||||
map.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user