JPA says an EM should act closed when close() has been invoked even if the

persistence context remains open while the current managed transaction 
completes.



git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@487339 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2006-12-14 20:27:16 +00:00
parent d8be93089c
commit 597930f15a
7 changed files with 156 additions and 4 deletions

View File

@ -650,6 +650,12 @@ public interface Broker
*/ */
public boolean isClosed(); public boolean isClosed();
/**
* Whether {@link #close} has been invoked, though the broker might
* remain open until the current managed transaction completes.
*/
public boolean isCloseInvoked();
/** /**
* Close the broker. * Close the broker.
*/ */

View File

@ -3955,6 +3955,10 @@ public class BrokerImpl
return _closed != null; return _closed != null;
} }
public boolean isCloseInvoked() {
return _closed != null || (_flags & FLAG_CLOSE_INVOKED) != 0;
}
public void close() { public void close() {
beginOperation(false); beginOperation(false);
try { try {

View File

@ -1271,6 +1271,14 @@ public class DelegatingBroker
} }
} }
public boolean isCloseInvoked() {
try {
return _broker.isCloseInvoked();
} catch (RuntimeException re) {
throw translate(re);
}
}
public void assertOpen() { public void assertOpen() {
try { try {
_broker.assertOpen(); _broker.assertOpen();

View File

@ -95,6 +95,7 @@ public class EntityManagerImpl
public Interaction createInteraction() public Interaction createInteraction()
throws ResourceException { throws ResourceException {
assertOpen();
return _broker.createInteraction(); return _broker.createInteraction();
} }
@ -117,6 +118,7 @@ public class EntityManagerImpl
} }
public FetchPlan getFetchPlan() { public FetchPlan getFetchPlan() {
assertOpen();
_broker.lock(); _broker.lock();
try { try {
if (_fetch == null) if (_fetch == null)
@ -145,6 +147,7 @@ public class EntityManagerImpl
} }
public void setSyncWithManagedTransactions(boolean sync) { public void setSyncWithManagedTransactions(boolean sync) {
assertOpen();
_broker.setSyncWithManagedTransactions(sync); _broker.setSyncWithManagedTransactions(sync);
} }
@ -165,6 +168,7 @@ public class EntityManagerImpl
} }
public void setMultithreaded(boolean multithreaded) { public void setMultithreaded(boolean multithreaded) {
assertOpen();
_broker.setMultithreaded(multithreaded); _broker.setMultithreaded(multithreaded);
} }
@ -173,6 +177,7 @@ public class EntityManagerImpl
} }
public void setIgnoreChanges(boolean val) { public void setIgnoreChanges(boolean val) {
assertOpen();
_broker.setIgnoreChanges(val); _broker.setIgnoreChanges(val);
} }
@ -181,6 +186,7 @@ public class EntityManagerImpl
} }
public void setNontransactionalRead(boolean val) { public void setNontransactionalRead(boolean val) {
assertOpen();
_broker.setNontransactionalRead(val); _broker.setNontransactionalRead(val);
} }
@ -189,6 +195,7 @@ public class EntityManagerImpl
} }
public void setNontransactionalWrite(boolean val) { public void setNontransactionalWrite(boolean val) {
assertOpen();
_broker.setNontransactionalWrite(val); _broker.setNontransactionalWrite(val);
} }
@ -197,6 +204,7 @@ public class EntityManagerImpl
} }
public void setOptimistic(boolean val) { public void setOptimistic(boolean val) {
assertOpen();
_broker.setOptimistic(val); _broker.setOptimistic(val);
} }
@ -205,6 +213,7 @@ public class EntityManagerImpl
} }
public void setRestoreState(int val) { public void setRestoreState(int val) {
assertOpen();
_broker.setRestoreState(val); _broker.setRestoreState(val);
} }
@ -213,6 +222,7 @@ public class EntityManagerImpl
} }
public void setRetainState(boolean val) { public void setRetainState(boolean val) {
assertOpen();
_broker.setRetainState(val); _broker.setRetainState(val);
} }
@ -221,6 +231,7 @@ public class EntityManagerImpl
} }
public void setAutoClear(int val) { public void setAutoClear(int val) {
assertOpen();
_broker.setAutoClear(val); _broker.setAutoClear(val);
} }
@ -229,6 +240,7 @@ public class EntityManagerImpl
} }
public void setDetachState(int mode) { public void setDetachState(int mode) {
assertOpen();
_broker.setDetachState(mode); _broker.setDetachState(mode);
} }
@ -237,10 +249,12 @@ public class EntityManagerImpl
} }
public void setAutoDetach(int flags) { public void setAutoDetach(int flags) {
assertOpen();
_broker.setAutoDetach(flags); _broker.setAutoDetach(flags);
} }
public void setAutoDetach(int flag, boolean on) { public void setAutoDetach(int flag, boolean on) {
assertOpen();
_broker.setAutoDetach(flag, on); _broker.setAutoDetach(flag, on);
} }
@ -249,6 +263,7 @@ public class EntityManagerImpl
} }
public void setEvictFromStoreCache(boolean evict) { public void setEvictFromStoreCache(boolean evict) {
assertOpen();
_broker.setEvictFromDataCache(evict); _broker.setEvictFromDataCache(evict);
} }
@ -257,6 +272,7 @@ public class EntityManagerImpl
} }
public void setPopulateStoreCache(boolean cache) { public void setPopulateStoreCache(boolean cache) {
assertOpen();
_broker.setPopulateDataCache(cache); _broker.setPopulateDataCache(cache);
} }
@ -265,6 +281,7 @@ public class EntityManagerImpl
} }
public void setLargeTransaction(boolean largeTransaction) { public void setLargeTransaction(boolean largeTransaction) {
assertOpen();
_broker.setLargeTransaction(largeTransaction); _broker.setLargeTransaction(largeTransaction);
} }
@ -273,14 +290,17 @@ public class EntityManagerImpl
} }
public Object putUserObject(Object key, Object val) { public Object putUserObject(Object key, Object val) {
assertOpen();
return _broker.putUserObject(key, val); return _broker.putUserObject(key, val);
} }
public void addTransactionListener(Object listener) { public void addTransactionListener(Object listener) {
assertOpen();
_broker.addTransactionListener(listener); _broker.addTransactionListener(listener);
} }
public void removeTransactionListener(Object listener) { public void removeTransactionListener(Object listener) {
assertOpen();
_broker.removeTransactionListener(listener); _broker.removeTransactionListener(listener);
} }
@ -289,14 +309,17 @@ public class EntityManagerImpl
} }
public void setTransactionListenerCallbackMode(int mode) { public void setTransactionListenerCallbackMode(int mode) {
assertOpen();
_broker.setTransactionListenerCallbackMode(mode); _broker.setTransactionListenerCallbackMode(mode);
} }
public void addLifecycleListener(Object listener, Class... classes) { public void addLifecycleListener(Object listener, Class... classes) {
assertOpen();
_broker.addLifecycleListener(listener, classes); _broker.addLifecycleListener(listener, classes);
} }
public void removeLifecycleListener(Object listener) { public void removeLifecycleListener(Object listener) {
assertOpen();
_broker.removeLifecycleListener(listener); _broker.removeLifecycleListener(listener);
} }
@ -305,17 +328,20 @@ public class EntityManagerImpl
} }
public void setLifecycleListenerCallbackMode(int mode) { public void setLifecycleListenerCallbackMode(int mode) {
assertOpen();
_broker.setLifecycleListenerCallbackMode(mode); _broker.setLifecycleListenerCallbackMode(mode);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getReference(Class<T> cls, Object oid) { public <T> T getReference(Class<T> cls, Object oid) {
assertOpen();
oid = _broker.newObjectId(cls, oid); oid = _broker.newObjectId(cls, oid);
return (T) _broker.find(oid, false, this); return (T) _broker.find(oid, false, this);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T find(Class<T> cls, Object oid) { public <T> T find(Class<T> cls, Object oid) {
assertOpen();
oid = _broker.newObjectId(cls, oid); oid = _broker.newObjectId(cls, oid);
return (T) _broker.find(oid, true, this); return (T) _broker.find(oid, true, this);
} }
@ -330,6 +356,7 @@ public class EntityManagerImpl
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Collection<T> findAll(final Class<T> cls, Collection oids) { public <T> Collection<T> findAll(final Class<T> cls, Collection oids) {
assertOpen();
Object[] objs = _broker.findAll(oids, true, new FindCallbacks() { Object[] objs = _broker.findAll(oids, true, new FindCallbacks() {
public Object processArgument(Object oid) { public Object processArgument(Object oid) {
return _broker.newObjectId(cls, oid); return _broker.newObjectId(cls, oid);
@ -344,15 +371,16 @@ public class EntityManagerImpl
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T findCached(Class<T> cls, Object oid) { public <T> T findCached(Class<T> cls, Object oid) {
assertOpen();
return (T) _broker.findCached(_broker.newObjectId(cls, oid), this); return (T) _broker.findCached(_broker.newObjectId(cls, oid), this);
} }
public Class getObjectIdClass(Class cls) { public Class getObjectIdClass(Class cls) {
assertOpen();
if (cls == null) if (cls == null)
return null; return null;
return OpenJPAPersistence return OpenJPAPersistence.fromOpenJPAObjectIdClass
.fromOpenJPAObjectIdClass(_broker.getObjectIdType (_broker.getObjectIdType(cls));
(cls));
} }
public EntityTransaction getTransaction() { public EntityTransaction getTransaction() {
@ -363,6 +391,7 @@ public class EntityManagerImpl
} }
public void joinTransaction() { public void joinTransaction() {
assertOpen();
if (!_broker.syncWithManagedTransaction()) if (!_broker.syncWithManagedTransaction())
throw new TransactionRequiredException(_loc.get throw new TransactionRequiredException(_loc.get
("no-managed-trans"), null, null, false); ("no-managed-trans"), null, null, false);
@ -414,35 +443,43 @@ public class EntityManagerImpl
} }
public void setSavepoint(String name) { public void setSavepoint(String name) {
assertOpen();
_broker.setSavepoint(name); _broker.setSavepoint(name);
} }
public void rollbackToSavepoint() { public void rollbackToSavepoint() {
assertOpen();
_broker.rollbackToSavepoint(); _broker.rollbackToSavepoint();
} }
public void rollbackToSavepoint(String name) { public void rollbackToSavepoint(String name) {
assertOpen();
_broker.rollbackToSavepoint(name); _broker.rollbackToSavepoint(name);
} }
public void releaseSavepoint() { public void releaseSavepoint() {
assertOpen();
_broker.releaseSavepoint(); _broker.releaseSavepoint();
} }
public void releaseSavepoint(String name) { public void releaseSavepoint(String name) {
assertOpen();
_broker.releaseSavepoint(name); _broker.releaseSavepoint(name);
} }
public void flush() { public void flush() {
assertOpen();
_broker.assertActiveTransaction(); _broker.assertActiveTransaction();
_broker.flush(); _broker.flush();
} }
public void preFlush() { public void preFlush() {
assertOpen();
_broker.preFlush(); _broker.preFlush();
} }
public void validateChanges() { public void validateChanges() {
assertOpen();
_broker.validateChanges(); _broker.validateChanges();
} }
@ -459,6 +496,7 @@ public class EntityManagerImpl
} }
public boolean contains(Object entity) { public boolean contains(Object entity) {
assertOpen();
if (entity == null) if (entity == null)
return false; return false;
OpenJPAStateManager sm = _broker.getStateManager(entity); OpenJPAStateManager sm = _broker.getStateManager(entity);
@ -483,6 +521,7 @@ public class EntityManagerImpl
} }
public void persist(Object entity) { public void persist(Object entity) {
assertOpen();
_broker.persist(entity, this); _broker.persist(entity, this);
} }
@ -491,10 +530,12 @@ public class EntityManagerImpl
} }
public void persistAll(Collection entities) { public void persistAll(Collection entities) {
assertOpen();
_broker.persistAll(entities, this); _broker.persistAll(entities, this);
} }
public void remove(Object entity) { public void remove(Object entity) {
assertOpen();
_broker.delete(entity, this); _broker.delete(entity, this);
} }
@ -503,14 +544,17 @@ public class EntityManagerImpl
} }
public void removeAll(Collection entities) { public void removeAll(Collection entities) {
assertOpen();
_broker.deleteAll(entities, this); _broker.deleteAll(entities, this);
} }
public void release(Object entity) { public void release(Object entity) {
assertOpen();
_broker.release(entity, this); _broker.release(entity, this);
} }
public void releaseAll(Collection entities) { public void releaseAll(Collection entities) {
assertOpen();
_broker.releaseAll(entities, this); _broker.releaseAll(entities, this);
} }
@ -519,16 +563,19 @@ public class EntityManagerImpl
} }
public void refresh(Object entity) { public void refresh(Object entity) {
assertOpen();
_broker.assertWriteOperation(); _broker.assertWriteOperation();
_broker.refresh(entity, this); _broker.refresh(entity, this);
} }
public void refreshAll() { public void refreshAll() {
assertOpen();
_broker.assertWriteOperation(); _broker.assertWriteOperation();
_broker.refreshAll(_broker.getTransactionalObjects(), this); _broker.refreshAll(_broker.getTransactionalObjects(), this);
} }
public void refreshAll(Collection entities) { public void refreshAll(Collection entities) {
assertOpen();
_broker.assertWriteOperation(); _broker.assertWriteOperation();
_broker.refreshAll(entities, this); _broker.refreshAll(entities, this);
} }
@ -538,10 +585,12 @@ public class EntityManagerImpl
} }
public void retrieve(Object entity) { public void retrieve(Object entity) {
assertOpen();
_broker.retrieve(entity, true, this); _broker.retrieve(entity, true, this);
} }
public void retrieveAll(Collection entities) { public void retrieveAll(Collection entities) {
assertOpen();
_broker.retrieveAll(entities, true, this); _broker.retrieveAll(entities, true, this);
} }
@ -550,10 +599,12 @@ public class EntityManagerImpl
} }
public void evict(Object entity) { public void evict(Object entity) {
assertOpen();
_broker.evict(entity, this); _broker.evict(entity, this);
} }
public void evictAll(Collection entities) { public void evictAll(Collection entities) {
assertOpen();
_broker.evictAll(entities, this); _broker.evictAll(entities, this);
} }
@ -562,32 +613,39 @@ public class EntityManagerImpl
} }
public void evictAll() { public void evictAll() {
assertOpen();
_broker.evictAll(this); _broker.evictAll(this);
} }
public void evictAll(Class cls) { public void evictAll(Class cls) {
assertOpen();
_broker.evictAll(_broker.newExtent(cls, true), this); _broker.evictAll(_broker.newExtent(cls, true), this);
} }
public void evictAll(Extent extent) { public void evictAll(Extent extent) {
assertOpen();
_broker.evictAll(extent.getDelegate(), this); _broker.evictAll(extent.getDelegate(), this);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T detach(T entity) { public <T> T detach(T entity) {
assertOpen();
return (T) _broker.detach(entity, this); return (T) _broker.detach(entity, this);
} }
public Object[] detachAll(Object... entities) { public Object[] detachAll(Object... entities) {
assertOpen();
return _broker.detachAll(Arrays.asList(entities), this); return _broker.detachAll(Arrays.asList(entities), this);
} }
public Collection detachAll(Collection entities) { public Collection detachAll(Collection entities) {
assertOpen();
return Arrays.asList(_broker.detachAll(entities, this)); return Arrays.asList(_broker.detachAll(entities, this));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T merge(T entity) { public <T> T merge(T entity) {
assertOpen();
return (T) _broker.attach(entity, true, this); return (T) _broker.attach(entity, true, this);
} }
@ -598,34 +656,42 @@ public class EntityManagerImpl
} }
public Collection mergeAll(Collection entities) { public Collection mergeAll(Collection entities) {
assertOpen();
return Arrays.asList(_broker.attachAll(entities, true, this)); return Arrays.asList(_broker.attachAll(entities, true, this));
} }
public void transactional(Object entity, boolean updateVersion) { public void transactional(Object entity, boolean updateVersion) {
assertOpen();
_broker.transactional(entity, updateVersion, this); _broker.transactional(entity, updateVersion, this);
} }
public void transactionalAll(Collection objs, boolean updateVersion) { public void transactionalAll(Collection objs, boolean updateVersion) {
assertOpen();
_broker.transactionalAll(objs, updateVersion, this); _broker.transactionalAll(objs, updateVersion, this);
} }
public void transactionalAll(Object[] objs, boolean updateVersion) { public void transactionalAll(Object[] objs, boolean updateVersion) {
assertOpen();
_broker.transactionalAll(Arrays.asList(objs), updateVersion, this); _broker.transactionalAll(Arrays.asList(objs), updateVersion, this);
} }
public void nontransactional(Object entity) { public void nontransactional(Object entity) {
assertOpen();
_broker.nontransactional(entity, this); _broker.nontransactional(entity, this);
} }
public void nontransactionalAll(Collection objs) { public void nontransactionalAll(Collection objs) {
assertOpen();
_broker.nontransactionalAll(objs, this); _broker.nontransactionalAll(objs, this);
} }
public void nontransactionalAll(Object[] objs) { public void nontransactionalAll(Object[] objs) {
assertOpen();
_broker.nontransactionalAll(Arrays.asList(objs), this); _broker.nontransactionalAll(Arrays.asList(objs), this);
} }
public Generator getNamedGenerator(String name) { public Generator getNamedGenerator(String name) {
assertOpen();
try { try {
SequenceMetaData meta = _broker.getConfiguration(). SequenceMetaData meta = _broker.getConfiguration().
getMetaDataRepositoryInstance().getSequenceMetaData(name, getMetaDataRepositoryInstance().getSequenceMetaData(name,
@ -638,6 +704,7 @@ public class EntityManagerImpl
} }
public Generator getIdGenerator(Class forClass) { public Generator getIdGenerator(Class forClass) {
assertOpen();
try { try {
ClassMetaData meta = _broker.getConfiguration(). ClassMetaData meta = _broker.getConfiguration().
getMetaDataRepositoryInstance().getMetaData(forClass, getMetaDataRepositoryInstance().getMetaData(forClass,
@ -651,6 +718,7 @@ public class EntityManagerImpl
} }
public Generator getFieldGenerator(Class forClass, String fieldName) { public Generator getFieldGenerator(Class forClass, String fieldName) {
assertOpen();
try { try {
ClassMetaData meta = _broker.getConfiguration(). ClassMetaData meta = _broker.getConfiguration().
getMetaDataRepositoryInstance().getMetaData(forClass, getMetaDataRepositoryInstance().getMetaData(forClass,
@ -669,6 +737,7 @@ public class EntityManagerImpl
} }
public <T> Extent<T> createExtent(Class<T> cls, boolean subclasses) { public <T> Extent<T> createExtent(Class<T> cls, boolean subclasses) {
assertOpen();
return new ExtentImpl<T>(this, _broker.newExtent(cls, subclasses)); return new ExtentImpl<T>(this, _broker.newExtent(cls, subclasses));
} }
@ -677,18 +746,21 @@ public class EntityManagerImpl
} }
public OpenJPAQuery createQuery(String language, String query) { public OpenJPAQuery createQuery(String language, String query) {
assertOpen();
return new QueryImpl(this, _broker.newQuery(language, query)); return new QueryImpl(this, _broker.newQuery(language, query));
} }
public OpenJPAQuery createQuery(Query query) { public OpenJPAQuery createQuery(Query query) {
if (query == null) if (query == null)
return createQuery((String) null); return createQuery((String) null);
assertOpen();
org.apache.openjpa.kernel.Query q = ((QueryImpl) query).getDelegate(); org.apache.openjpa.kernel.Query q = ((QueryImpl) query).getDelegate();
return new QueryImpl(this, _broker.newQuery(q.getLanguage(), return new QueryImpl(this, _broker.newQuery(q.getLanguage(),
q)); q));
} }
public OpenJPAQuery createNamedQuery(String name) { public OpenJPAQuery createNamedQuery(String name) {
assertOpen();
try { try {
QueryMetaData meta = _broker.getConfiguration(). QueryMetaData meta = _broker.getConfiguration().
getMetaDataRepositoryInstance().getQueryMetaData(null, name, getMetaDataRepositoryInstance().getQueryMetaData(null, name,
@ -719,6 +791,7 @@ public class EntityManagerImpl
} }
public OpenJPAQuery createNativeQuery(String query, String mappingName) { public OpenJPAQuery createNativeQuery(String query, String mappingName) {
assertOpen();
validateSQL(query); validateSQL(query);
org.apache.openjpa.kernel.Query kernelQuery = _broker.newQuery( org.apache.openjpa.kernel.Query kernelQuery = _broker.newQuery(
QueryLanguages.LANG_SQL, query); QueryLanguages.LANG_SQL, query);
@ -735,11 +808,13 @@ public class EntityManagerImpl
} }
public void setFlushMode(FlushModeType flushMode) { public void setFlushMode(FlushModeType flushMode) {
assertOpen();
_broker.getFetchConfiguration().setFlushBeforeQueries _broker.getFetchConfiguration().setFlushBeforeQueries
(toFlushBeforeQueries(flushMode)); (toFlushBeforeQueries(flushMode));
} }
public FlushModeType getFlushMode() { public FlushModeType getFlushMode() {
assertOpen();
return fromFlushBeforeQueries(_broker.getFetchConfiguration(). return fromFlushBeforeQueries(_broker.getFetchConfiguration().
getFlushBeforeQueries()); getFlushBeforeQueries());
} }
@ -773,6 +848,7 @@ public class EntityManagerImpl
} }
public void clear() { public void clear() {
assertOpen();
_broker.detachAll(this); _broker.detachAll(this);
} }
@ -781,26 +857,32 @@ public class EntityManagerImpl
} }
public LockModeType getLockMode(Object entity) { public LockModeType getLockMode(Object entity) {
assertOpen();
return fromLockLevel(_broker.getLockLevel(entity)); return fromLockLevel(_broker.getLockLevel(entity));
} }
public void lock(Object entity, LockModeType mode) { public void lock(Object entity, LockModeType mode) {
assertOpen();
_broker.lock(entity, toLockLevel(mode), -1, this); _broker.lock(entity, toLockLevel(mode), -1, this);
} }
public void lock(Object entity) { public void lock(Object entity) {
assertOpen();
_broker.lock(entity, this); _broker.lock(entity, this);
} }
public void lock(Object entity, LockModeType mode, int timeout) { public void lock(Object entity, LockModeType mode, int timeout) {
assertOpen();
_broker.lock(entity, toLockLevel(mode), timeout, this); _broker.lock(entity, toLockLevel(mode), timeout, this);
} }
public void lockAll(Collection entities) { public void lockAll(Collection entities) {
assertOpen();
_broker.lockAll(entities, this); _broker.lockAll(entities, this);
} }
public void lockAll(Collection entities, LockModeType mode, int timeout) { public void lockAll(Collection entities, LockModeType mode, int timeout) {
assertOpen();
_broker.lockAll(entities, toLockLevel(mode), timeout, this); _broker.lockAll(entities, toLockLevel(mode), timeout, this);
} }
@ -865,10 +947,12 @@ public class EntityManagerImpl
} }
public void setOrderDirtyObjects(boolean order) { public void setOrderDirtyObjects(boolean order) {
assertOpen();
_broker.setOrderDirtyObjects(order); _broker.setOrderDirtyObjects(order);
} }
public void dirtyClass(Class cls) { public void dirtyClass(Class cls) {
assertOpen();
_broker.dirtyType(cls); _broker.dirtyType(cls);
} }
@ -888,6 +972,7 @@ public class EntityManagerImpl
} }
public <T> T createInstance(Class<T> cls) { public <T> T createInstance(Class<T> cls) {
assertOpen();
return (T) _broker.newInstance(cls); return (T) _broker.newInstance(cls);
} }
@ -896,10 +981,12 @@ public class EntityManagerImpl
} }
public boolean isOpen() { public boolean isOpen() {
return !_broker.isClosed(); assertOpen();
return !_broker.isCloseInvoked();
} }
public void dirty(Object o, String field) { public void dirty(Object o, String field) {
assertOpen();
OpenJPAStateManager sm = _broker.getStateManager(o); OpenJPAStateManager sm = _broker.getStateManager(o);
try { try {
if (sm != null) if (sm != null)
@ -910,37 +997,56 @@ public class EntityManagerImpl
} }
public Object getObjectId(Object o) { public Object getObjectId(Object o) {
assertOpen();
return OpenJPAPersistence.fromOpenJPAObjectId(_broker.getObjectId(o)); return OpenJPAPersistence.fromOpenJPAObjectId(_broker.getObjectId(o));
} }
public boolean isDirty(Object o) { public boolean isDirty(Object o) {
assertOpen();
return _broker.isDirty(o); return _broker.isDirty(o);
} }
public boolean isTransactional(Object o) { public boolean isTransactional(Object o) {
assertOpen();
return _broker.isTransactional(o); return _broker.isTransactional(o);
} }
public boolean isPersistent(Object o) { public boolean isPersistent(Object o) {
assertOpen();
return _broker.isPersistent(o); return _broker.isPersistent(o);
} }
public boolean isNewlyPersistent(Object o) { public boolean isNewlyPersistent(Object o) {
assertOpen();
return _broker.isNew(o); return _broker.isNew(o);
} }
public boolean isRemoved(Object o) { public boolean isRemoved(Object o) {
assertOpen();
return _broker.isDeleted(o); return _broker.isDeleted(o);
} }
public boolean isDetached(Object entity) { public boolean isDetached(Object entity) {
assertOpen();
return _broker.isDetached(entity); return _broker.isDetached(entity);
} }
public Object getVersion(Object o) { public Object getVersion(Object o) {
assertOpen();
return _broker.getVersion(o); return _broker.getVersion(o);
} }
/**
* Throw appropriate exception if entity manager is closed.
*/
void assertOpen() {
// only throw when broker wouldn't throw anyway, because we prefer
// the broker's more detailed exception when available
if (!_broker.isClosed() && _broker.isCloseInvoked())
throw new InvalidStateException(_loc.get("close-invoked"), null,
null, true);
}
//////////////////////////////// ////////////////////////////////
// FindCallbacks implementation // FindCallbacks implementation
//////////////////////////////// ////////////////////////////////

View File

@ -61,6 +61,7 @@ public class ExtentImpl<T>
} }
public FetchPlan getFetchPlan() { public FetchPlan getFetchPlan() {
_em.assertOpen();
_extent.lock(); _extent.lock();
try { try {
if (_fetch == null) if (_fetch == null)
@ -78,14 +79,17 @@ public class ExtentImpl<T>
} }
public void setIgnoreChanges(boolean ignoreChanges) { public void setIgnoreChanges(boolean ignoreChanges) {
_em.assertOpen();
_extent.setIgnoreChanges(ignoreChanges); _extent.setIgnoreChanges(ignoreChanges);
} }
public List<T> list() { public List<T> list() {
_em.assertOpen();
return _extent.list(); return _extent.list();
} }
public Iterator<T> iterator() { public Iterator<T> iterator() {
_em.assertOpen();
return _extent.iterator(); return _extent.iterator();
} }

View File

@ -91,6 +91,7 @@ public class QueryImpl
} }
public FetchPlan getFetchPlan() { public FetchPlan getFetchPlan() {
_em.assertOpen();
_query.assertNotSerialized(); _query.assertNotSerialized();
_query.lock(); _query.lock();
try { try {
@ -113,26 +114,31 @@ public class QueryImpl
} }
public OpenJPAQuery setIgnoreChanges(boolean ignore) { public OpenJPAQuery setIgnoreChanges(boolean ignore) {
_em.assertOpen();
_query.setIgnoreChanges(ignore); _query.setIgnoreChanges(ignore);
return this; return this;
} }
public OpenJPAQuery addFilterListener(FilterListener listener) { public OpenJPAQuery addFilterListener(FilterListener listener) {
_em.assertOpen();
_query.addFilterListener(listener); _query.addFilterListener(listener);
return this; return this;
} }
public OpenJPAQuery removeFilterListener(FilterListener listener) { public OpenJPAQuery removeFilterListener(FilterListener listener) {
_em.assertOpen();
_query.removeFilterListener(listener); _query.removeFilterListener(listener);
return this; return this;
} }
public OpenJPAQuery addAggregateListener(AggregateListener listener) { public OpenJPAQuery addAggregateListener(AggregateListener listener) {
_em.assertOpen();
_query.addAggregateListener(listener); _query.addAggregateListener(listener);
return this; return this;
} }
public OpenJPAQuery removeAggregateListener(AggregateListener listener) { public OpenJPAQuery removeAggregateListener(AggregateListener listener) {
_em.assertOpen();
_query.removeAggregateListener(listener); _query.removeAggregateListener(listener);
return this; return this;
} }
@ -142,6 +148,7 @@ public class QueryImpl
} }
public OpenJPAQuery setCandidateCollection(Collection coll) { public OpenJPAQuery setCandidateCollection(Collection coll) {
_em.assertOpen();
_query.setCandidateCollection(coll); _query.setCandidateCollection(coll);
return this; return this;
} }
@ -154,6 +161,7 @@ public class QueryImpl
} }
public OpenJPAQuery setResultClass(Class cls) { public OpenJPAQuery setResultClass(Class cls) {
_em.assertOpen();
if (OpenJPAPersistence.isManagedType(_em, cls)) if (OpenJPAPersistence.isManagedType(_em, cls))
_query.setCandidateType(cls, true); _query.setCandidateType(cls, true);
else else
@ -166,6 +174,7 @@ public class QueryImpl
} }
public OpenJPAQuery setSubclasses(boolean subs) { public OpenJPAQuery setSubclasses(boolean subs) {
_em.assertOpen();
Class cls = _query.getCandidateType(); Class cls = _query.getCandidateType();
_query.setCandidateExtent(_query.getBroker().newExtent(cls, subs)); _query.setCandidateExtent(_query.getBroker().newExtent(cls, subs));
return this; return this;
@ -176,6 +185,7 @@ public class QueryImpl
} }
public OpenJPAQuery setFirstResult(int startPosition) { public OpenJPAQuery setFirstResult(int startPosition) {
_em.assertOpen();
_query.setRange(startPosition, _query.getEndRange()); _query.setRange(startPosition, _query.getEndRange());
return this; return this;
} }
@ -185,6 +195,7 @@ public class QueryImpl
} }
public OpenJPAQuery setMaxResults(int max) { public OpenJPAQuery setMaxResults(int max) {
_em.assertOpen();
long start = _query.getStartRange(); long start = _query.getStartRange();
if (max == Integer.MAX_VALUE) if (max == Integer.MAX_VALUE)
_query.setRange(start, Long.MAX_VALUE); _query.setRange(start, Long.MAX_VALUE);
@ -194,6 +205,7 @@ public class QueryImpl
} }
public OpenJPAQuery compile() { public OpenJPAQuery compile() {
_em.assertOpen();
_query.compile(); _query.compile();
return this; return this;
} }
@ -248,6 +260,7 @@ public class QueryImpl
} }
public List getResultList() { public List getResultList() {
_em.assertOpen();
Object ob = execute(); Object ob = execute();
if (ob instanceof List) { if (ob instanceof List) {
List ret = (List) ob; List ret = (List) ob;
@ -265,6 +278,7 @@ public class QueryImpl
* Execute a query that returns a single result. * Execute a query that returns a single result.
*/ */
public Object getSingleResult() { public Object getSingleResult() {
_em.assertOpen();
Object ob = execute(); Object ob = execute();
if (!(ob instanceof List)) if (!(ob instanceof List))
return ob; return ob;
@ -287,6 +301,7 @@ public class QueryImpl
} }
public int executeUpdate() { public int executeUpdate() {
_em.assertOpen();
if (_query.getOperation() == OP_DELETE) { if (_query.getOperation() == OP_DELETE) {
// handle which types of parameters we are using, if any // handle which types of parameters we are using, if any
if (_positional != null) if (_positional != null)
@ -324,12 +339,14 @@ public class QueryImpl
} }
public OpenJPAQuery setFlushMode(FlushModeType flushMode) { public OpenJPAQuery setFlushMode(FlushModeType flushMode) {
_em.assertOpen();
_query.getFetchConfiguration().setFlushBeforeQueries _query.getFetchConfiguration().setFlushBeforeQueries
(EntityManagerImpl.toFlushBeforeQueries(flushMode)); (EntityManagerImpl.toFlushBeforeQueries(flushMode));
return this; return this;
} }
public OpenJPAQuery setHint(String key, Object value) { public OpenJPAQuery setHint(String key, Object value) {
_em.assertOpen();
if (key == null || !key.startsWith("openjpa.")) if (key == null || !key.startsWith("openjpa."))
return this; return this;
String k = key.substring("openjpa.".length()); String k = key.substring("openjpa.".length());
@ -380,6 +397,7 @@ public class QueryImpl
} }
public OpenJPAQuery setParameter(int position, Object value) { public OpenJPAQuery setParameter(int position, Object value) {
_em.assertOpen();
_query.lock(); _query.lock();
try { try {
// not allowed to mix positional and named parameters (EDR2 3.6.4) // not allowed to mix positional and named parameters (EDR2 3.6.4)
@ -419,6 +437,7 @@ public class QueryImpl
} }
public OpenJPAQuery setParameter(String name, Object value) { public OpenJPAQuery setParameter(String name, Object value) {
_em.assertOpen();
_query.lock(); _query.lock();
try { try {
// not allowed to mix positional and named parameters (EDR2 3.6.4) // not allowed to mix positional and named parameters (EDR2 3.6.4)
@ -441,6 +460,7 @@ public class QueryImpl
} }
public Object[] getPositionalParameters() { public Object[] getPositionalParameters() {
_em.assertOpen();
_query.lock(); _query.lock();
try { try {
return (_positional == null) ? EMPTY_ARRAY : _positional.toArray(); return (_positional == null) ? EMPTY_ARRAY : _positional.toArray();
@ -450,6 +470,7 @@ public class QueryImpl
} }
public OpenJPAQuery setParameters(Object... params) { public OpenJPAQuery setParameters(Object... params) {
_em.assertOpen();
_query.lock(); _query.lock();
try { try {
_positional = null; _positional = null;
@ -474,6 +495,7 @@ public class QueryImpl
} }
public OpenJPAQuery setParameters(Map params) { public OpenJPAQuery setParameters(Map params) {
_em.assertOpen();
_query.lock(); _query.lock();
try { try {
_positional = null; _positional = null;

View File

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
close-invoked: You have closed the EntityManager, though the persistence \
context will remain active until the current transaction commits.
no-managed-trans: There is no managed transaction in progress to sync this \ no-managed-trans: There is no managed transaction in progress to sync this \
EntityManager with. EntityManager with.
get-managed-trans: You cannot access the EntityTransaction when using managed \ get-managed-trans: You cannot access the EntityTransaction when using managed \