| Method from org.apache.openjpa.kernel.BrokerImpl Detail: |
void addDereferencedDependent(StateManagerImpl sm) {
lock();
try {
// if we're in the middle of flush and introducing more derefs
// via instance callbacks, add them to the special additions set
if ((_flags & FLAG_DEREFDELETING) != 0) {
if (_derefAdditions == null)
_derefAdditions = new HashSet();
_derefAdditions.add(sm);
} else {
if (_derefCache == null)
_derefCache = new HashSet();
_derefCache.add(sm);
}
}
finally {
unlock();
}
}
Add a dereferenced dependent object to the persistence manager's cache.
On flush, these objects will be deleted. |
public void addLifecycleListener(Object listener,
Class[] classes) {
beginOperation(false);
try {
_lifeEventManager.addListener(listener, classes);
} finally {
endOperation();
}
}
|
void addToPendingTransaction(StateManagerImpl sm) {
lock();
try {
if (_pending == null)
_pending = new HashSet();
_pending.add(sm);
} finally {
unlock();
}
}
Notify the broker that the given state manager should
be added to the set of instances that will become transactional
on the next transaction |
void addToTransaction(StateManagerImpl sm) {
// we only add clean instances now; dirty instances are added in
// the setDirty callback
if (sm.isDirty())
return;
lock();
try {
if (!hasTransactionalObjects())
_transCache = new TransactionalCache(_orderDirty);
_transCache.addClean(sm);
} finally {
unlock();
}
}
Notify the broker that the given state manager should
be added to the set of instances involved in the current transaction. |
public void addTransactionListener(Object tl) {
beginOperation(false);
try {
_transEventManager.addListener(tl);
if (tl instanceof RemoteCommitEventManager)
_flags |= FLAG_REMOTE_LISTENER;
} finally {
endOperation();
}
}
|
public void afterCompletion(int status) {
beginOperation(false);
try {
assertActiveTransaction();
_flags |= FLAG_TRANS_ENDING;
endTransaction(status);
if (_sync != null)
_sync.afterCompletion(status);
if ((_autoDetach & DETACH_COMMIT) != 0)
detachAllInternal(null);
else if (status == Status.STATUS_ROLLEDBACK
&& (_autoDetach & DETACH_ROLLBACK) != 0) {
detachAllInternal(null);
}
// in an ee context, it's possible that the user tried to close
// us but we didn't actually close because we were waiting on this
// transaction; if that's true, then close now
if ((_flags & FLAG_CLOSE_INVOKED) != 0
&& _compat.getCloseOnManagedCommit())
free();
} catch (OpenJPAException ke) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), ke);
throw translateManagedCompletionException(ke);
} catch (RuntimeException re) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), re);
throw translateManagedCompletionException(new StoreException(re));
} finally {
_flags &= ~FLAG_ACTIVE;
_flags &= ~FLAG_FLUSHED;
_flags &= ~FLAG_TRANS_ENDING;
// event manager nulled if freed broker
if (_transEventManager != null
&& _transEventManager.hasEndListeners()) {
fireTransactionEvent(new TransactionEvent(this,
status == Status.STATUS_COMMITTED
? TransactionEvent.AFTER_COMMIT_COMPLETE
: TransactionEvent.AFTER_ROLLBACK_COMPLETE,
null, null, null, null));
}
endOperation();
}
}
|
public void assertActiveTransaction() {
if ((_flags & FLAG_ACTIVE) == 0)
throw new NoTransactionException(_loc.get("not-active"));
}
|
public void assertNontransactionalRead() {
if ((_flags & FLAG_ACTIVE) == 0 && !_nontransRead)
throw new InvalidStateException(_loc.get("non-trans-read"));
}
|
public void assertOpen() {
if (_closed) {
if (_closedException == null) // TRACE not enabled
throw new InvalidStateException(_loc.get("closed-notrace"))
.setFatal(true);
else {
OpenJPAException e = new InvalidStateException(
_loc.get("closed"), _closedException).setFatal(true);
e.setCause(_closedException);
throw e;
}
}
}
Throw an exception if the context is closed. The exact message and
content of the exception varies whether TRACE is enabled or not. |
protected PersistenceCapable assertPersistenceCapable(Object obj) {
if (obj == null)
return null;
if (ImplHelper.isManageable(obj))
return ImplHelper.toPersistenceCapable(obj, _conf);
// check for different instances of the PersistenceCapable interface
// and throw a better error that mentions the class loaders
Class[] intfs = obj.getClass().getInterfaces();
for (int i = 0; intfs != null && i < intfs.length; i++) {
if (intfs[i].getName().equals(PersistenceCapable.class.getName())) {
throw new UserException(_loc.get("pc-loader-different",
Exceptions.toString(obj),
(ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getClassLoaderAction(
PersistenceCapable.class)),
(ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getClassLoaderAction(intfs[i]))))
.setFailedObject(obj);
}
}
// not enhanced
throw new UserException(_loc.get("pc-cast",
Exceptions.toString(obj))).setFailedObject(obj);
}
Return the given instance as a PersistenceCapable .
If the instance is not manageable throw the proper exception. |
public void assertWriteOperation() {
if ((_flags & FLAG_ACTIVE) == 0 && (!_nontransWrite
|| (_autoDetach & DETACH_NONTXREAD) != 0))
throw new NoTransactionException(_loc.get("write-operation"));
}
|
protected void assignObjectId(Object cache,
Object id,
StateManagerImpl sm) {
((ManagedCache) cache).assignObjectId(id, sm);
}
Assign the object id to the cache. Exception will be
thrown if the id already exists in the cache. |
public Object attach(Object obj,
boolean copyNew,
OpCallbacks call) {
if (obj == null)
return null;
beginOperation(true);
try {
// make sure not to try to set rollback only if this fails
assertWriteOperation();
try {
return new AttachManager(this, copyNew, call).attach(obj);
} catch (OptimisticException oe) {
setRollbackOnly(oe);
throw oe.setFatal(true);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
}
}
finally {
endOperation();
}
}
|
public Object[] attachAll(Collection objs,
boolean copyNew,
OpCallbacks call) {
if (objs == null)
return null;
if (objs.isEmpty())
return EMPTY_OBJECTS;
beginOperation(true);
try {
// make sure not to try to set rollback only if this fails
assertWriteOperation();
try {
return new AttachManager(this, copyNew, call).attachAll(objs);
} catch (OptimisticException oe) {
setRollbackOnly(oe);
throw oe.setFatal(true);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
}
}
finally {
endOperation();
}
}
|
public void beforeCompletion() {
beginOperation(false);
try {
// user-supplied synchronization
if (_sync != null)
_sync.beforeCompletion();
flushSafe(FLUSH_COMMIT);
} catch (OpenJPAException ke) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), ke);
throw translateManagedCompletionException(ke);
} catch (RuntimeException re) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), re);
throw translateManagedCompletionException(new StoreException(re));
} finally {
endOperation();
}
}
|
public void begin() {
beginOperation(true);
try {
if ((_flags & FLAG_ACTIVE) != 0)
throw new InvalidStateException(_loc.get("active"));
_factory.syncWithManagedTransaction(this, true);
beginInternal();
} finally {
endOperation();
}
}
|
public boolean beginOperation(boolean syncTrans) {
lock();
try {
assertOpen();
if (syncTrans && _operationCount == 0 && _syncManaged
&& (_flags & FLAG_ACTIVE) == 0)
syncWithManagedTransaction();
return _operationCount++ == 1;
} catch (OpenJPAException ke) {
unlock();
throw ke;
} catch (RuntimeException re) {
unlock();
throw new GeneralException(re);
}
}
|
public void beginStore() {
beginOperation(true);
try {
assertTransactionOperation();
if ((_flags & FLAG_STORE_ACTIVE) == 0)
beginStoreManagerTransaction(false);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new StoreException(re);
} finally {
endOperation();
}
}
|
public boolean cancelAll() {
// this method does not lock, since we want to allow a different
// thread to be able to cancel on a locked-up persistence manager
assertOpen();
try {
// if we're flushing, have to set rollback only -- do this before we
// attempt to cancel, because otherwise the cancel might case the
// transaction to complete before we have a chance to set the
// rollback only flag
if ((_flags & FLAG_STORE_FLUSHING) != 0)
setRollbackOnlyInternal(new UserException());
return _store.cancelAll();
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new StoreException(re);
}
}
|
protected void checkForDuplicateId(Object id,
Object obj) {
StateManagerImpl other = getStateManagerImplById(id, false);
if (other != null && !other.isDeleted() && !other.isNew())
throw new ObjectExistsException(_loc.get("cache-exists",
obj.getClass().getName(), id)).setFailedObject(obj);
}
This method makes sure we don't already have the instance cached |
public Object clone() throws CloneNotSupportedException {
if (_initializeWasInvoked)
throw new CloneNotSupportedException();
else {
return super.clone();
}
}
|
public void close() {
beginOperation(false);
try {
// throw an exception if closing in an active local trans
if (!_managed && (_flags & FLAG_ACTIVE) != 0)
throw new InvalidStateException(_loc.get("active"));
// only close if not active; if active managed trans wait
// for completion
_flags |= FLAG_CLOSE_INVOKED;
if ((_flags & FLAG_ACTIVE) == 0)
free();
} finally {
endOperation();
}
}
|
public void commit() {
beginOperation(false);
try {
assertTransactionOperation();
javax.transaction.Transaction trans =
_runtime.getTransactionManager().getTransaction();
if (trans == null)
throw new InvalidStateException(_loc.get("null-trans"));
// this commit on the transaction will cause our
// beforeCompletion method to be invoked
trans.commit();
} catch (OpenJPAException ke) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), ke);
throw ke;
} catch (Exception e) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), e);
throw new StoreException(e);
} finally {
endOperation();
}
}
|
public void commitAndResume() {
endAndResume(true);
}
|
OpenJPAStateManager copy(OpenJPAStateManager copy,
PCState state) {
beginOperation(true);
try {
assertOpen();
Object oid = copy.fetchObjectId();
Class type = copy.getManagedInstance().getClass();
if (oid == null)
throw new InternalException();
// cached instance?
StateManagerImpl sm = null;
if (!copy.isEmbedded())
sm = getStateManagerImplById(oid, true);
if (sm == null) {
MetaDataRepository repos = _conf.
getMetaDataRepositoryInstance();
ClassMetaData meta = repos.getMetaData(type, _loader, true);
// construct a new state manager with all info known
sm = new StateManagerImpl(oid, meta, this);
sm.setObjectId(oid);
sm.initialize(sm.getMetaData().getDescribedType(), state);
}
return sm;
} finally {
endOperation();
}
}
If not already cached, create an empty copy of the given state
manager in the given state. |
public void delete(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
assertWriteOperation();
delete(obj, getStateManagerImpl(obj, true), call);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
void delete(Object obj,
StateManagerImpl sm,
OpCallbacks call) {
if (!_operating.add(obj))
return;
int action = processArgument(OpCallbacks.OP_DELETE, obj, sm, call);
if (action == OpCallbacks.ACT_NONE)
return;
// ACT_CASCADE
if ((action & OpCallbacks.ACT_RUN) == 0) {
if (sm != null)
sm.cascadeDelete(call);
else
cascadeTransient(OpCallbacks.OP_DELETE, obj, call, "delete");
return;
}
// ACT_RUN
if (sm != null) {
if (sm.isDetached())
throw newDetachedException(obj, "delete");
if ((action & OpCallbacks.ACT_CASCADE) != 0)
sm.cascadeDelete(call);
sm.delete();
} else if (assertPersistenceCapable(obj).pcIsDetached() == Boolean.TRUE)
throw newDetachedException(obj, "delete");
}
|
public void deleteAll(Collection objs,
OpCallbacks call) {
beginOperation(true);
try {
assertWriteOperation();
List exceps = null;
Object obj;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
obj = itr.next();
if (obj != null)
delete(obj, getStateManagerImpl(obj, true), call);
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
throwNestedExceptions(exceps, false);
} finally {
endOperation();
}
}
|
public Object detach(Object obj,
OpCallbacks call) {
if (obj == null)
return null;
if (call == null)
call = _call;
beginOperation(true);
try {
return new DetachManager(this, false, call).detach(obj);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void detachAll(OpCallbacks call) {
detachAll(call, true);
}
|
public Object[] detachAll(Collection objs,
OpCallbacks call) {
if (objs == null)
return null;
if (objs.isEmpty())
return EMPTY_OBJECTS;
if (call == null)
call = _call;
beginOperation(true);
try {
return new DetachManager(this, false, call).detachAll(objs);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void detachAll(OpCallbacks call,
boolean flush) {
beginOperation(true);
try {
// If a flush is desired (based on input parm), then check if the
// "dirty" flag is set before calling flush().
if ((flush) && ((_flags & FLAG_FLUSH_REQUIRED) != 0))
flush();
detachAllInternal(call);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void dirtyType(Class cls) {
if (cls == null)
return;
beginOperation(false);
try {
if (_updatedClss == null)
_updatedClss = new HashSet();
_updatedClss.add(cls);
} finally {
endOperation();
}
}
|
public OpenJPAStateManager embed(Object obj,
Object id,
OpenJPAStateManager owner,
ValueMetaData ownerMeta) {
beginOperation(true);
try {
StateManagerImpl orig = getStateManagerImpl(obj, true);
if (orig != null) {
// if already embedded, nothing to do
if (orig.getOwner() == owner && orig.getMetaData().
getEmbeddingMetaData() == ownerMeta)
return orig;
// otherwise make sure pc is fully loaded for when we copy its
// data below
orig.load(_fc, StateManagerImpl.LOAD_ALL, null, null, false);
}
// create new state manager with embedded metadata
ClassMetaData meta = ownerMeta.getEmbeddedMetaData();
if (meta == null)
throw new InternalException(_loc.get("bad-embed", ownerMeta));
if (id == null)
id = StateManagerId.newInstance(this);
StateManagerImpl sm = new StateManagerImpl(id, meta, this);
sm.setOwner((StateManagerImpl) owner, ownerMeta);
PersistenceCapable copy;
PCState state;
Class type = meta.getDescribedType();
if (obj != null) {
// give copy and the original instance the same state manager
// so that we can copy fields from one to the other
StateManagerImpl copySM;
PersistenceCapable pc;
if (orig == null) {
copySM = sm;
pc = assertPersistenceCapable(obj);
pc.pcReplaceStateManager(sm);
} else {
copySM = orig;
pc = orig.getPersistenceCapable();
}
try {
// copy the instance. we do this even if it doesn't already
// have a state manager in case it is later assigned to a
// PC field; at that point it's too late to copy
copy = PCRegistry.newInstance(type, copySM, false);
int[] fields = new int[meta.getFields().length];
for (int i = 0; i < fields.length; i++)
fields[i] = i;
copy.pcCopyFields(pc, fields);
state = PCState.ECOPY;
copy.pcReplaceStateManager(null);
} finally {
// if the instance didn't have a state manager to start,
// revert it to being transient
if (orig == null)
pc.pcReplaceStateManager(null);
}
} else {
copy = PCRegistry.newInstance(type, sm, false);
if ((_flags & FLAG_ACTIVE) != 0 && !_optimistic)
state = PCState.ECLEAN;
else
state = PCState.ENONTRANS;
}
sm.initialize(copy, state);
return sm;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public boolean endOperation() {
try {
if (_operationCount == 1 && (_autoDetach & DETACH_NONTXREAD) != 0
&& (_flags & FLAG_ACTIVE) == 0) {
detachAllInternal(null);
}
if (_operationCount < 1)
throw new InternalException(_loc.get("multi-threaded-access"));
return _operationCount == 1;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
_operationCount--;
if (_operationCount == 0)
initializeOperatingSet();
unlock();
}
}
Mark the operation over. If outermost caller of stack, returns true
and will detach managed instances if necessary. |
protected void endTransaction(int status) {
// if a data store transaction was in progress, do the
// appropriate transaction change
boolean rollback = status != Status.STATUS_COMMITTED;
List exceps = null;
try {
exceps = add(exceps, endStoreManagerTransaction(rollback));
} catch (RuntimeException re) {
rollback = true;
exceps = add(exceps, re);
}
// go back to default none lock level
_fc.setReadLockLevel(LOCK_NONE);
_fc.setWriteLockLevel(LOCK_NONE);
_fc.setLockTimeout(-1);
Collection transStates;
if (hasTransactionalObjects())
transStates = _transCache;
else
transStates = Collections.EMPTY_LIST;
// fire after rollback/commit event
Collection mobjs = null;
if (_transEventManager.hasEndListeners()) {
mobjs = new ManagedObjectCollection(transStates);
int eventType = (rollback) ? TransactionEvent.AFTER_ROLLBACK
: TransactionEvent.AFTER_COMMIT;
fireTransactionEvent(new TransactionEvent(this, eventType, mobjs,
_persistedClss, _updatedClss, _deletedClss));
}
// null transactional caches now so that all the removeFromTransaction
// calls as we transition each object don't have to do any work; don't
// clear trans cache object because we still need the transStates
// reference to it below
_transCache = null;
if (_persistedClss != null)
_persistedClss = null;
if (_updatedClss != null)
_updatedClss = null;
if (_deletedClss != null)
_deletedClss = null;
// new cache would get cleared anyway during transitions, but doing so
// immediately saves us some lookups
_cache.clearNew();
// tell all derefed instances they're no longer derefed; we can't
// rely on rollback and commit calls below cause some instances might
// not be transactional
if (_derefCache != null && !_derefCache.isEmpty()) {
for (Iterator itr = _derefCache.iterator(); itr.hasNext();)
((StateManagerImpl) itr.next()).setDereferencedDependent
(false, false);
_derefCache = null;
}
// peform commit or rollback state transitions on each instance
StateManagerImpl sm;
for (Iterator itr = transStates.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
try {
if (rollback) {
// tell objects that may have been derefed then flushed
// (and therefore deleted) to un-deref
sm.setDereferencedDependent(false, false);
sm.rollback();
} else
sm.commit();
} catch (RuntimeException re) {
exceps = add(exceps, re);
}
}
// notify the lock manager to clean up and release remaining locks
_lm.endTransaction();
// clear old savepoints in reverse
OpenJPASavepoint save;
while (_savepoints != null && _savepoints.size() > 0) {
save =
(OpenJPASavepoint) _savepoints.remove(_savepoints.size() - 1);
save.release(false);
}
_savepoints = null;
_savepointCache = null;
// fire after state change event
if (_transEventManager.hasEndListeners())
fireTransactionEvent(new TransactionEvent(this, TransactionEvent.
AFTER_STATE_TRANSITIONS, mobjs, null, null, null));
// now clear trans cache; keep cleared version rather than
// null to avoid having to re-create the set later; more efficient
if (transStates != Collections.EMPTY_LIST) {
_transCache = (TransactionalCache) transStates;
_transCache.clear();
}
throwNestedExceptions(exceps, true);
}
End the current transaction, making appropriate state transitions. |
public void evict(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(false);
try {
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_EVICT, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm == null)
return;
sm.evict();
if (_evictDataCache && sm.getObjectId() != null) {
DataCache cache = sm.getMetaData().getDataCache();
if (cache != null)
cache.remove(sm.getObjectId());
}
}
catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void evictAll(OpCallbacks call) {
beginOperation(false);
try {
// evict all PClean and PNonTrans objects
Collection c = getManagedStates();
StateManagerImpl sm;
for (Iterator itr = c.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
if (sm.isPersistent() && !sm.isDirty())
evict(sm.getManagedInstance(), call);
}
}
finally {
endOperation();
}
}
|
public void evictAll(Collection objs,
OpCallbacks call) {
List exceps = null;
beginOperation(false);
try {
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
evict(itr.next(), call);
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
} finally {
endOperation();
}
throwNestedExceptions(exceps, false);
}
|
public void evictAll(Extent extent,
OpCallbacks call) {
if (extent == null)
return;
beginOperation(false);
try {
// evict all PClean and PNonTrans objects in extent
Collection c = getManagedStates();
StateManagerImpl sm;
Class cls;
for (Iterator itr = c.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
if (sm.isPersistent() && !sm.isDirty()) {
cls = sm.getMetaData().getDescribedType();
if (cls == extent.getElementType()
|| (extent.hasSubclasses()
&& extent.getElementType().isAssignableFrom(cls)))
evict(sm.getManagedInstance(), call);
}
}
} finally {
endOperation();
}
}
|
public Iterator extentIterator(Class type,
boolean subclasses,
FetchConfiguration fetch,
boolean ignoreChanges) {
Extent extent = newExtent(type, subclasses, fetch);
extent.setIgnoreChanges(ignoreChanges);
return extent.iterator();
}
|
public Object find(Object oid,
boolean validate,
FindCallbacks call) {
int flags = OID_COPY | OID_ALLOW_NEW | OID_NODELETED;
if (!validate)
flags |= OID_NOVALIDATE;
return find(oid, _fc, null, null, flags, call);
}
|
public Object find(Object oid,
FetchConfiguration fetch,
BitSet exclude,
Object edata,
int flags) {
return find(oid, fetch, exclude, edata, flags, null);
}
|
protected Object find(Object oid,
FetchConfiguration fetch,
BitSet exclude,
Object edata,
int flags,
FindCallbacks call) {
if (call == null)
call = this;
oid = call.processArgument(oid);
if (oid == null) {
if ((flags & OID_NOVALIDATE) == 0)
throw new ObjectNotFoundException(_loc.get("null-oid"));
return call.processReturn(oid, null);
}
if (fetch == null)
fetch = _fc;
beginOperation(true);
try {
assertNontransactionalRead();
// cached instance?
StateManagerImpl sm = getStateManagerImplById(oid,
(flags & OID_ALLOW_NEW) != 0 || hasFlushed());
if (sm != null) {
if (!requiresLoad(sm, true, fetch, edata, flags))
return call.processReturn(oid, sm);
if (!sm.isLoading()) {
// make sure all the configured fields are loaded; do this
// after making instance transactional for locking
if (!sm.isTransactional() && useTransactionalState(fetch))
sm.transactional();
boolean loaded;
try {
loaded = sm.load(fetch, StateManagerImpl.LOAD_FGS,
exclude, edata, false);
} catch (ObjectNotFoundException onfe) {
if ((flags & OID_NODELETED) != 0
|| (flags & OID_NOVALIDATE) != 0)
throw onfe;
return call.processReturn(oid, null);
}
// if no data needed to be loaded and the user wants to
// validate, just make sure the object exists
if (!loaded && (flags & OID_NOVALIDATE) == 0
&& _compat.getValidateTrueChecksStore()
&& !sm.isTransactional()
&& !_store.exists(sm, edata)) {
if ((flags & OID_NODELETED) == 0)
return call.processReturn(oid, null);
throw new ObjectNotFoundException(_loc.get
("del-instance", sm.getManagedInstance(), oid)).
setFailedObject(sm.getManagedInstance());
}
}
// since the object was cached, we may need to upgrade lock
// if current level is higher than level of initial load
if ((_flags & FLAG_ACTIVE) != 0) {
int level = fetch.getReadLockLevel();
_lm.lock(sm, level, fetch.getLockTimeout(), edata);
sm.readLocked(level, fetch.getWriteLockLevel());
}
return call.processReturn(oid, sm);
}
// if there's no cached sm for a new/transient id type, we
// it definitely doesn't exist
if (oid instanceof StateManagerId)
return call.processReturn(oid, null);
// initialize a new state manager for the datastore instance
sm = newStateManagerImpl(oid, (flags & OID_COPY) != 0);
boolean load = requiresLoad(sm, false, fetch, edata, flags);
sm = initialize(sm, load, fetch, edata);
if (sm == null) {
if ((flags & OID_NOVALIDATE) != 0)
throw new ObjectNotFoundException(oid);
return call.processReturn(oid, null);
}
// make sure all configured fields were loaded
if (load) {
try {
sm.load(fetch, StateManagerImpl.LOAD_FGS, exclude,
edata, false);
} catch (ObjectNotFoundException onfe) {
if ((flags & OID_NODELETED) != 0
|| (flags & OID_NOVALIDATE) != 0)
throw onfe;
return call.processReturn(oid, null);
}
}
return call.processReturn(oid, sm);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public Object[] findAll(Collection oids,
boolean validate,
FindCallbacks call) {
int flags = OID_COPY | OID_ALLOW_NEW | OID_NODELETED;
if (!validate)
flags |= OID_NOVALIDATE;
return findAll(oids, _fc, null, null, flags, call);
}
|
public Object[] findAll(Collection oids,
FetchConfiguration fetch,
BitSet exclude,
Object edata,
int flags) {
return findAll(oids, fetch, exclude, edata, flags, null);
}
|
protected Object[] findAll(Collection oids,
FetchConfiguration fetch,
BitSet exclude,
Object edata,
int flags,
FindCallbacks call) {
findAllDepth ++;
// throw any exceptions for null oids up immediately
if (oids == null)
throw new NullPointerException("oids == null");
if ((flags & OID_NOVALIDATE) != 0 && oids.contains(null))
throw new UserException(_loc.get("null-oids"));
// we have to use a map of oid- >sm rather than a simple
// array, so that we make sure not to create multiple sms for equivalent
// oids if the user has duplicates in the given array
if (_loading == null)
_loading = new HashMap((int) (oids.size() * 1.33 + 1));
if (call == null)
call = this;
if (fetch == null)
fetch = _fc;
beginOperation(true);
try {
assertNontransactionalRead();
// collection of state managers to pass to store manager
List load = null;
StateManagerImpl sm;
boolean initialized;
boolean transState = useTransactionalState(fetch);
Object obj, oid;
int idx = 0;
for (Iterator itr = oids.iterator(); itr.hasNext(); idx++) {
// if we've already seen this oid, skip repeats
obj = itr.next();
oid = call.processArgument(obj);
if (oid == null || _loading.containsKey(obj))
continue;
// if we don't have a cached instance or it is not transactional
// and is hollow or we need to validate, load it
sm = getStateManagerImplById(oid, (flags & OID_ALLOW_NEW) != 0
|| hasFlushed());
initialized = sm != null;
if (!initialized)
sm = newStateManagerImpl(oid, (flags & OID_COPY) != 0);
_loading.put(obj, sm);
if (requiresLoad(sm, initialized, fetch, edata, flags)) {
transState = transState || useTransactionalState(fetch);
if (initialized && !sm.isTransactional() && transState)
sm.transactional();
if (load == null)
load = new ArrayList(oids.size() - idx);
load.add(sm);
} else if (!initialized)
sm.initialize(sm.getMetaData().getDescribedType(),
PCState.HOLLOW);
}
// pass all state managers in need of loading or validation to the
// store manager
if (load != null) {
PCState state = (transState) ? PCState.PCLEAN
: PCState.PNONTRANS;
Collection failed = _store.loadAll(load, state,
StoreManager.FORCE_LOAD_NONE, fetch, edata);
// set failed instances to null
if (failed != null && !failed.isEmpty()) {
if ((flags & OID_NOVALIDATE) != 0)
throw newObjectNotFoundException(failed);
for (Iterator itr = failed.iterator(); itr.hasNext();)
_loading.put(itr.next(), null);
}
}
// create results array; make sure all configured fields are
// loaded in each instance
Object[] results = new Object[oids.size()];
boolean active = (_flags & FLAG_ACTIVE) != 0;
int level = fetch.getReadLockLevel();
idx = 0;
for (Iterator itr = oids.iterator(); itr.hasNext(); idx++) {
oid = itr.next();
sm = (StateManagerImpl) _loading.get(oid);
if (sm != null && requiresLoad(sm, true, fetch, edata, flags)) {
try {
sm.load(fetch, StateManagerImpl.LOAD_FGS,
exclude, edata, false);
if (active) {
_lm.lock(sm, level, fetch.getLockTimeout(), edata);
sm.readLocked(level, fetch.getWriteLockLevel());
}
}
catch (ObjectNotFoundException onfe) {
if ((flags & OID_NODELETED) != 0
|| (flags & OID_NOVALIDATE) != 0)
throw onfe;
sm = null;
}
}
results[idx] = call.processReturn(oid, sm);
}
return results;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
findAllDepth--;
if (findAllDepth == 0)
_loading = null;
endOperation();
}
}
|
public Object findCached(Object oid,
FindCallbacks call) {
if (call == null)
call = this;
oid = call.processArgument(oid);
if (oid == null)
return call.processReturn(oid, null);
beginOperation(true);
try {
StateManagerImpl sm = getStateManagerImplById(oid, true);
return call.processReturn(oid, sm);
} finally {
endOperation();
}
}
|
boolean fireLifecycleEvent(Object src,
Object related,
ClassMetaData meta,
int eventType) {
if (_lifeEventManager == null)
return false;
handleCallbackExceptions(_lifeEventManager.fireEvent(src, related,
meta, eventType), _lifeCallbackMode);
return true;
}
Fire given lifecycle event, handling any exceptions appropriately. |
public void flush() {
beginOperation(true);
try {
// return silently if no trans is active, or if this is a reentrant
// call, which can happen if the store manager tries to get an
// auto-inc oid during flush
if ((_flags & FLAG_ACTIVE) == 0
|| (_flags & FLAG_STORE_FLUSHING) != 0)
return;
// make sure the runtime supports it
if (!_conf.supportedOptions().contains(_conf.OPTION_INC_FLUSH))
throw new UnsupportedException(_loc.get
("incremental-flush-not-supported"));
if (_savepoints != null && !_savepoints.isEmpty()
&& !_spm.supportsIncrementalFlush())
throw new UnsupportedException(_loc.get
("savepoint-flush-not-supported"));
try {
flushSafe(FLUSH_INC);
_flags |= FLAG_FLUSHED;
} catch (OpenJPAException ke) {
// rollback on flush error; objects may be in inconsistent state
setRollbackOnly(ke);
throw ke.setFatal(true);
} catch (RuntimeException re) {
// rollback on flush error; objects may be in inconsistent state
setRollbackOnly(re);
throw new StoreException(re).setFatal(true);
}
}
finally {
endOperation();
}
}
|
protected void flush(int reason) {
// this will enlist proxied states as necessary so we know whether we
// have anything to flush
Collection transactional = getTransactionalStates();
// do we actually have to flush? only if our flags say so, or if
// we have transaction listeners that need to be invoked for commit
// (no need to invoke them on inc flush if nothing is dirty). we
// special case the remote commit listener used by the datacache cause
// we know it doesn't require the commit event when nothing changes
boolean flush = (_flags & FLAG_FLUSH_REQUIRED) != 0;
boolean listeners = (_transEventManager.hasFlushListeners()
|| _transEventManager.hasEndListeners())
&& ((_flags & FLAG_REMOTE_LISTENER) == 0
|| _transEventManager.getListeners().size() > 1);
if (!flush && (reason != FLUSH_COMMIT || !listeners))
return;
Collection mobjs = null;
_flags |= FLAG_PRESTORING;
try {
if (flush) {
// call pre store on all currently transactional objs
for (Iterator itr = transactional.iterator(); itr.hasNext();)
((StateManagerImpl) itr.next()).beforeFlush(reason, _call);
flushAdditions(transactional, reason);
}
// hopefully now all dependent instances that are going to end
// up referenced have been marked as such; delete unrefed
// dependents
_flags |= FLAG_DEREFDELETING;
if (flush && _derefCache != null && !_derefCache.isEmpty()) {
for (Iterator itr = _derefCache.iterator(); itr.hasNext();)
deleteDeref((StateManagerImpl) itr.next());
flushAdditions(transactional, reason);
}
if (reason != FLUSH_LOGICAL) {
// if no datastore transaction, start one; even if we don't
// think we'll need to flush at this point, our transaction
// listeners might introduce some dirty objects or interact
// directly with the database
if ((_flags & FLAG_STORE_ACTIVE) == 0)
beginStoreManagerTransaction(false);
if ((_transEventManager.hasFlushListeners()
|| _transEventManager.hasEndListeners())
&& (flush || reason == FLUSH_COMMIT)) {
// fire events
mobjs = new ManagedObjectCollection(transactional);
if (reason == FLUSH_COMMIT
&& _transEventManager.hasEndListeners()) {
fireTransactionEvent(new TransactionEvent(this,
TransactionEvent.BEFORE_COMMIT, mobjs,
_persistedClss, _updatedClss, _deletedClss));
flushAdditions(transactional, reason);
flush = (_flags & FLAG_FLUSH_REQUIRED) != 0;
}
if (flush && _transEventManager.hasFlushListeners()) {
fireTransactionEvent(new TransactionEvent(this,
TransactionEvent.BEFORE_FLUSH, mobjs,
_persistedClss, _updatedClss, _deletedClss));
flushAdditions(transactional, reason);
}
}
}
}
finally {
_flags &= ~FLAG_PRESTORING;
_flags &= ~FLAG_DEREFDELETING;
_transAdditions = null;
_derefAdditions = null;
// also clear derefed set; the deletes have been recorded
if (_derefCache != null)
_derefCache = null;
}
// flush to store manager
List exceps = null;
try {
if (flush && reason != FLUSH_LOGICAL) {
_flags |= FLAG_STORE_FLUSHING;
exceps = add(exceps,
newFlushException(_store.flush(transactional)));
}
} finally {
_flags &= ~FLAG_STORE_FLUSHING;
if (reason == FLUSH_ROLLBACK)
exceps = add(exceps, endStoreManagerTransaction(true));
else if (reason != FLUSH_LOGICAL)
_flags &= ~FLAG_FLUSH_REQUIRED;
// mark states as flushed
if (flush) {
StateManagerImpl sm;
for (Iterator itr = transactional.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
try {
// the state may have become transient, such as if
// it is embedded and the owner has been deleted during
// this flush process; bug #1100
if (sm.getPCState() == PCState.TRANSIENT)
continue;
sm.afterFlush(reason);
if (reason == FLUSH_INC) {
// if not about to clear trans cache for commit
// anyway, re-cache dirty objects with default soft
// refs; we don't need hard refs now that the
// changes have been flushed
sm.proxyFields(true, false);
_transCache.flushed(sm);
}
} catch (Exception e) {
exceps = add(exceps, e);
}
}
}
}
// throw any exceptions to shortcut listeners on fail
throwNestedExceptions(exceps, true);
if (flush && reason != FLUSH_ROLLBACK && reason != FLUSH_LOGICAL
&& _transEventManager.hasFlushListeners()) {
fireTransactionEvent(new TransactionEvent(this,
TransactionEvent.AFTER_FLUSH, mobjs, _persistedClss,
_updatedClss, _deletedClss));
}
}
Flush the transactional state to the data store. Subclasses that
customize commit behavior should override this method. The method
assumes that the persistence manager is locked, is not closed,
and has an active transaction. |
protected void free() {
RuntimeException err = null;
if ((_autoDetach & DETACH_CLOSE) != 0) {
try {
detachAllInternal(_call);
} catch (RuntimeException re) {
err = re;
}
}
_sync = null;
_userObjects = null;
_cache.clear();
_transCache = null;
_persistedClss = null;
_updatedClss = null;
_deletedClss = null;
_derefCache = null;
_pending = null;
_loader = null;
_transEventManager = null;
_lifeEventManager = null;
OpenJPASavepoint save;
while (_savepoints != null && !_savepoints.isEmpty()) {
save =
(OpenJPASavepoint) _savepoints.remove(_savepoints.size() - 1);
save.release(false);
}
_savepoints = null;
_savepointCache = null;
if (_queries != null) {
for (Iterator itr = _queries.iterator(); itr.hasNext();) {
try {
((Query) itr.next()).closeResources();
} catch (RuntimeException re) {
}
}
_queries = null;
}
if (_extents != null) {
Extent e;
for (Iterator itr = _extents.iterator(); itr.hasNext();) {
e = (Extent) itr.next();
try {
e.closeAll();
} catch (RuntimeException re) {
}
}
_extents = null;
}
try { releaseConnection(); } catch (RuntimeException re) {}
_lm.close();
_store.close();
_flags = 0;
_closed = true;
if (_log.isTraceEnabled())
_closedException = new IllegalStateException();
_factory.releaseBroker(this);
if (err != null)
throw err;
}
Free the resources used by this persistence manager. |
void gatherCascadeRefresh(Object obj,
OpCallbacks call) {
if (obj == null)
return;
if (!_operating.add(obj))
return;
StateManagerImpl sm = getStateManagerImpl(obj, false);
int action = processArgument(OpCallbacks.OP_REFRESH, obj, sm, call);
if ((action & OpCallbacks.ACT_CASCADE) == 0)
return;
if (sm != null)
sm.gatherCascadeRefresh(call);
else
cascadeTransient(OpCallbacks.OP_REFRESH, obj, call, "refresh");
}
Gathers all objects reachable through cascade-refresh relations
into the operating set. |
public int getAutoClear() {
return _autoClear;
}
|
public int getAutoDetach() {
return _autoDetach;
}
|
public Broker getBroker() {
return this;
}
|
public BrokerFactory getBrokerFactory() {
return _factory;
}
|
public ClassLoader getClassLoader() {
return _loader;
}
|
public OpenJPAConfiguration getConfiguration() {
return _conf;
}
|
public Object getConnection() {
assertOpen();
if (!_conf.supportedOptions().contains
(_conf.OPTION_DATASTORE_CONNECTION))
throw new UnsupportedException(_loc.get("conn-not-supported"));
return _store.getClientConnection();
}
|
public String getConnectionPassword() {
return _pass;
}
|
public int getConnectionRetainMode() {
return _connRetainMode;
}
|
public String getConnectionUserName() {
return _user;
}
|
public Collection getDeletedTypes() {
if (_deletedClss == null || _deletedClss.isEmpty())
return Collections.EMPTY_LIST;
return Collections.unmodifiableCollection(_deletedClss);
}
|
public int getDetachState() {
return _detachState;
}
|
public Collection getDirtyObjects() {
beginOperation(false);
try {
return new ManagedObjectCollection(getDirtyStates());
} finally {
endOperation();
}
}
|
protected Collection getDirtyStates() {
if (!hasTransactionalObjects())
return Collections.EMPTY_LIST;
return _transCache.copyDirty();
}
Return a copy of all dirty state managers. |
public boolean getEvictFromDataCache() {
return _evictDataCache;
}
|
public FetchConfiguration getFetchConfiguration() {
return _fc;
}
|
public Seq getIdentitySequence(ClassMetaData meta) {
if (meta == null)
return null;
return getSequence(meta, null);
}
|
public boolean getIgnoreChanges() {
return _ignoreChanges;
}
|
RuntimeExceptionTranslator getInstanceExceptionTranslator() {
return (_operationCount == 0) ? _extrans : null;
}
|
public InverseManager getInverseManager() {
return _im;
}
|
public LifecycleEventManager getLifecycleEventManager() {
return _lifeEventManager;
}
Give state managers access to the lifecycle event manager. |
public int getLifecycleListenerCallbackMode() {
return _lifeCallbackMode;
}
|
public int getLockLevel(Object o) {
assertOpen();
if (o == null)
return LockLevels.LOCK_NONE;
OpenJPAStateManager sm = getStateManager(o);
if (sm == null)
return LockLevels.LOCK_NONE;
return getLockManager().getLockLevel(sm);
}
|
public LockManager getLockManager() {
return _lm;
}
|
public Collection getManagedObjects() {
beginOperation(false);
try {
return new ManagedObjectCollection(getManagedStates());
} finally {
endOperation();
}
}
|
public ManagedRuntime getManagedRuntime() {
return _runtime;
}
|
protected Collection getManagedStates() {
return _cache.copy();
}
Return a copy of all managed state managers. |
public boolean getMultithreaded() {
return _multithreaded;
}
|
public boolean getNontransactionalRead() {
return _nontransRead;
}
|
public boolean getNontransactionalWrite() {
return _nontransWrite;
}
|
public Object getObjectId(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf))
.pcFetchObjectId();
return null;
}
|
public Class getObjectIdType(Class cls) {
if (cls == null)
return null;
beginOperation(false);
try {
ClassMetaData meta = _conf.getMetaDataRepositoryInstance().
getMetaData(cls, _loader, false);
if (meta == null
|| meta.getIdentityType() == ClassMetaData.ID_UNKNOWN)
return null;
if (meta.getIdentityType() == ClassMetaData.ID_APPLICATION)
return meta.getObjectIdType();
return _store.getDataStoreIdType(meta);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
protected Set getOperatingSet() {
return Collections.unmodifiableSet(_operating);
}
Gets the unmodifiable set of instances being operated. |
public boolean getOptimistic() {
return _optimistic;
}
|
public boolean getOrderDirtyObjects() {
return _orderDirty;
}
|
public Collection getPendingTransactionalObjects() {
beginOperation(false);
try {
return new ManagedObjectCollection
(getPendingTransactionalStates());
} finally {
endOperation();
}
}
|
protected Collection getPendingTransactionalStates() {
if (_pending == null)
return Collections.EMPTY_LIST;
return new ArrayList(_pending);
}
Return a copy of all state managers which will become
transactional upon the next transaction. |
public Collection getPersistedTypes() {
if (_persistedClss == null || _persistedClss.isEmpty())
return Collections.EMPTY_LIST;
return Collections.unmodifiableCollection(_persistedClss);
}
|
public boolean getPopulateDataCache() {
return _populateDataCache;
}
|
public int getRestoreState() {
return _restoreState;
}
|
public boolean getRetainState() {
return _retainState;
}
|
public Throwable getRollbackCause() {
beginOperation(true);
try {
if ((_flags & FLAG_ACTIVE) == 0)
return null;
javax.transaction.Transaction trans =
_runtime.getTransactionManager().getTransaction();
if (trans == null)
return null;
if (trans.getStatus() == Status.STATUS_MARKED_ROLLBACK)
return _runtime.getRollbackCause();
return null;
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
} finally {
endOperation();
}
}
|
public boolean getRollbackOnly() {
beginOperation(true);
try {
if ((_flags & FLAG_ACTIVE) == 0)
return false;
javax.transaction.Transaction trans =
_runtime.getTransactionManager().getTransaction();
if (trans == null)
return false;
return trans.getStatus() == Status.STATUS_MARKED_ROLLBACK;
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
} finally {
endOperation();
}
}
|
public OpenJPAStateManager getStateManager(Object obj) {
assertOpen();
return getStateManagerImpl(obj, false);
}
|
protected StateManagerImpl getStateManagerImpl(Object obj,
boolean assertThisContext) {
if (ImplHelper.isManageable(obj)) {
PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf);
if (pc.pcGetGenericContext() == this)
return (StateManagerImpl) pc.pcGetStateManager();
if (assertThisContext && pc.pcGetGenericContext() != null)
throw new UserException(_loc.get("not-managed",
Exceptions.toString(obj))).setFailedObject(obj);
}
return null;
}
Return the state manager for the given instance, or null. |
protected StateManagerImpl getStateManagerImplById(Object oid,
boolean allowNew) {
return _cache.getById(oid, allowNew);
}
Return the state manager for the given oid. |
public DelegatingStoreManager getStoreManager() {
return _store;
}
|
public boolean getSyncWithManagedTransactions() {
return _syncManaged;
}
|
public Synchronization getSynchronization() {
return _sync;
}
|
public int getTransactionListenerCallbackMode() {
return _transCallbackMode;
}
|
public Collection getTransactionalObjects() {
beginOperation(false);
try {
return new ManagedObjectCollection(getTransactionalStates());
} finally {
endOperation();
}
}
|
protected Collection getTransactionalStates() {
if (!hasTransactionalObjects())
return Collections.EMPTY_LIST;
return _transCache.copy();
}
Return a copy of all transactional state managers. |
public Collection getUpdatedTypes() {
if (_updatedClss == null || _updatedClss.isEmpty())
return Collections.EMPTY_LIST;
return Collections.unmodifiableCollection(_updatedClss);
}
|
public Object getUserObject(Object key) {
beginOperation(false);
try {
return (_userObjects == null) ? null : _userObjects.get(key);
} finally {
endOperation();
}
}
|
public Seq getValueSequence(FieldMetaData fmd) {
if (fmd == null)
return null;
return getSequence(fmd.getDefiningMetaData(), fmd);
}
|
public Object getVersion(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf)).pcGetVersion();
return null;
}
|
public boolean hasConnection() {
assertOpen();
return (_flags & FLAG_RETAINED_CONN) != 0;
}
|
protected StateManagerImpl initialize(StateManagerImpl sm,
boolean load,
FetchConfiguration fetch,
Object edata) {
if (!load) {
sm.initialize(sm.getMetaData().getDescribedType(),
PCState.HOLLOW);
} else {
PCState state = (useTransactionalState(fetch))
? PCState.PCLEAN : PCState.PNONTRANS;
sm.setLoading(true);
try {
if (!_store.initialize(sm, state, fetch, edata))
return null;
} finally {
sm.setLoading(false);
}
}
return sm;
}
Initialize a newly-constructed state manager. |
public void initialize(AbstractBrokerFactory factory,
DelegatingStoreManager sm,
boolean managed,
int connMode,
boolean fromDeserialization) {
_initializeWasInvoked = true;
_loader = (ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getContextClassLoaderAction());
if (!fromDeserialization)
_conf = factory.getConfiguration();
_compat = _conf.getCompatibilityInstance();
_factory = factory;
_log = _conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
if (!fromDeserialization)
_cache = new ManagedCache(this);
initializeOperatingSet();
_connRetainMode = connMode;
_managed = managed;
if (managed)
_runtime = _conf.getManagedRuntimeInstance();
else
_runtime = new LocalManagedRuntime(this);
if (!fromDeserialization) {
_lifeEventManager = new LifecycleEventManager();
_transEventManager = new TransactionEventManager();
int cmode = _conf.getMetaDataRepositoryInstance().
getMetaDataFactory().getDefaults().getCallbackMode();
setLifecycleListenerCallbackMode(cmode);
setTransactionListenerCallbackMode(cmode);
// setup default options
_factory.configureBroker(this);
}
// make sure to do this after configuring broker so that store manager
// can look to broker configuration; we set both store and lock managers
// before initializing them because they may each try to access the
// other in thier initialization
_store = sm;
_lm = _conf.newLockManagerInstance();
_im = _conf.newInverseManagerInstance();
_spm = _conf.getSavepointManagerInstance();
_store.setContext(this);
_lm.setContext(this);
if (_connRetainMode == CONN_RETAIN_ALWAYS)
retainConnection();
if (!fromDeserialization) {
_fc = _store.newFetchConfiguration();
_fc.setContext(this);
}
// synch with the global transaction in progress, if any
if (_factory.syncWithManagedTransaction(this, false))
beginInternal();
}
Initialize the persistence manager. This method is called
automatically by the factory before use. |
public boolean isActive() {
beginOperation(true);
try {
return (_flags & FLAG_ACTIVE) != 0;
} finally {
endOperation();
}
}
|
public boolean isCloseInvoked() {
return _closed || (_flags & FLAG_CLOSE_INVOKED) != 0;
}
|
public boolean isClosed() {
return _closed;
}
|
public boolean isDeleted(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf)).pcIsDeleted();
return false;
}
|
public boolean isDetached(Object obj) {
if (!(ImplHelper.isManageable(obj)))
return false;
PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf);
Boolean detached = pc.pcIsDetached();
if (detached != null)
return detached.booleanValue();
// last resort: instance is detached if it has a store record
ClassMetaData meta = _conf.getMetaDataRepositoryInstance().
getMetaData(ImplHelper.getManagedInstance(pc).getClass(),
_loader, true);
Object oid = ApplicationIds.create(pc, meta);
if (oid == null)
return false;
return find(oid, null, EXCLUDE_ALL, null, 0) != null;
}
|
public boolean isDetachedNew() {
return _detachedNew;
}
|
public boolean isDirty(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj)) {
PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf);
return pc.pcIsDirty();
}
return false;
}
|
public boolean isManaged() {
return _managed;
}
|
public boolean isNew(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf)).pcIsNew();
return false;
}
|
public boolean isPersistent(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf)).pcIsPersistent();
return false;
}
|
boolean isSerializing() {
return _isSerializing;
}
Whether or not this broker is in the midst of being serialized. |
public boolean isStoreActive() {
// we need to lock here, because we might be in the middle of an
// atomic transaction process (e.g., commitAndResume)
beginOperation(true);
try {
return (_flags & FLAG_STORE_ACTIVE) != 0;
} finally {
endOperation();
}
}
|
public boolean isTrackChangesByType() {
return _largeTransaction;
}
|
boolean isTransactionEnding() {
return (_flags & FLAG_TRANS_ENDING) != 0;
}
Return whether the current transaction is ending, i.e. in the 2nd phase
of a commit or rollback |
public boolean isTransactional(Object obj) {
assertOpen();
if (ImplHelper.isManageable(obj))
return (ImplHelper.toPersistenceCapable(obj, _conf))
.pcIsTransactional();
return false;
}
|
public void lock() {
if (_lock != null)
_lock.lock();
}
|
public void lock(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true); // have to sync or lock level always NONE
try {
lock(obj, _fc.getWriteLockLevel(), _fc.getLockTimeout(), call);
} finally {
endOperation();
}
}
|
public void lock(Object obj,
int level,
int timeout,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
assertActiveTransaction();
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_LOCK, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm == null || !sm.isPersistent())
return;
_lm.lock(sm, level, timeout, null);
sm.readLocked(level, level); // use same level for future write
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void lockAll(Collection objs,
OpCallbacks call) {
if (objs.isEmpty())
return;
beginOperation(true); // have to sync or lock level always NONE
try {
lockAll(objs, _fc.getWriteLockLevel(), _fc.getLockTimeout(),
call);
} finally {
endOperation();
}
}
|
public void lockAll(Collection objs,
int level,
int timeout,
OpCallbacks call) {
if (objs.isEmpty())
return;
if (objs.size() == 1) {
lock(objs.iterator().next(), level, timeout, call);
return;
}
beginOperation(true);
try {
assertActiveTransaction();
Collection sms = new ArrayList(objs.size());
Object obj;
StateManagerImpl sm;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
obj = itr.next();
if (obj == null)
continue;
sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_LOCK, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
continue;
if (sm != null && sm.isPersistent())
sms.add(sm);
}
_lm.lockAll(sms, level, timeout, null);
for (Iterator itr = sms.iterator(); itr.hasNext();)
((StateManagerImpl) itr.next()).readLocked(level, level);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public Extent newExtent(Class type,
boolean subclasses) {
return newExtent(type, subclasses, null);
}
|
public Object newInstance(Class cls) {
assertOpen();
if (!cls.isInterface() && Modifier.isAbstract(cls.getModifiers()))
throw new UnsupportedOperationException(_loc.get
("new-abstract", cls).getMessage());
// 1.5 doesn't initialize classes without a true Class.forName
if (!PCRegistry.isRegistered(cls)) {
try {
Class.forName(cls.getName(), true,
(ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getClassLoaderAction(cls)));
} catch (Throwable t) {
}
}
if (_conf.getMetaDataRepositoryInstance().getMetaData(cls,
getClassLoader(), false) == null)
throw new IllegalArgumentException(
_loc.get("no-interface-metadata", cls.getName()).getMessage());
try {
return PCRegistry.newInstance(cls, null, false);
} catch (IllegalStateException ise) {
IllegalArgumentException iae =
new IllegalArgumentException(ise.getMessage());
iae.setStackTrace(ise.getStackTrace());
throw iae;
}
}
|
protected Map newManagedObjectCache() {
return new ReferenceHashMap(ReferenceMap.HARD, ReferenceMap.SOFT);
}
Create a Map to be used for the primary managed object cache.
Maps oids to state managers. By default, this creates a
ReferenceMap with soft values. |
public Object newObjectId(Class cls,
Object val) {
if (val == null)
return null;
beginOperation(false);
try {
ClassMetaData meta = _conf.getMetaDataRepositoryInstance().
getMetaData(cls, _loader, true);
switch (meta.getIdentityType()) {
case ClassMetaData.ID_DATASTORE:
// delegate to store manager for datastore ids
if (val instanceof String
&& ((String) val).startsWith(StateManagerId.STRING_PREFIX))
return new StateManagerId((String) val);
return _store.newDataStoreId(val, meta);
case ClassMetaData.ID_APPLICATION:
if (ImplHelper.isAssignable(meta.getObjectIdType(),
val.getClass())) {
if (!meta.isOpenJPAIdentity()
&& meta.isObjectIdTypeShared())
return new ObjectId(cls, val);
return val;
}
// stringified app id?
if (val instanceof String
&& !_conf.getCompatibilityInstance().
getStrictIdentityValues()
&& !Modifier.isAbstract(cls.getModifiers()))
return PCRegistry.newObjectId(cls, (String) val);
Object[] arr = (val instanceof Object[]) ? (Object[]) val
: new Object[]{ val };
return ApplicationIds.fromPKValues(arr, meta);
default:
throw new UserException(_loc.get("meta-unknownid", cls));
}
} catch (OpenJPAException ke) {
throw ke;
} catch (ClassCastException cce) {
throw new UserException(_loc.get("bad-id-value", val,
val.getClass().getName(), cls)).setCause(cce);
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public Query newQuery(String lang,
Object query) {
// common mistakes
if (query instanceof Extent || query instanceof Class)
throw new UserException(_loc.get("bad-new-query"));
beginOperation(false);
try {
StoreQuery sq = _store.newQuery(lang);
if (sq == null) {
ExpressionParser ep = QueryLanguages.parserForLanguage(lang);
if (ep != null)
sq = new ExpressionStoreQuery(ep);
else if (QueryLanguages.LANG_METHODQL.equals(lang))
sq = new MethodStoreQuery();
else
throw new UnsupportedException(lang);
}
Query q = newQueryImpl(lang, sq);
q.setIgnoreChanges(_ignoreChanges);
if (query != null)
q.setQuery(query);
// track queries
if (_queries == null)
_queries = new ReferenceHashSet(ReferenceHashSet.WEAK);
_queries.add(q);
return q;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public Query newQuery(String lang,
Class cls,
Object query) {
Query q = newQuery(lang, query);
q.setCandidateType(cls, true);
return q;
}
|
protected QueryImpl newQueryImpl(String lang,
StoreQuery sq) {
return new QueryImpl(this, lang, sq);
}
|
protected StateManagerImpl newStateManagerImpl(Object oid,
ClassMetaData meta) {
return new StateManagerImpl(oid, meta, this);
}
Create a state manager for the given oid and metadata. |
public void nontransactional(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_NONTRANSACTIONAL, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm != null)
sm.nontransactional();
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void nontransactionalAll(Collection objs,
OpCallbacks call) {
beginOperation(true);
try {
List exceps = null;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
nontransactional(itr.next(), call);
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
throwNestedExceptions(exceps, false);
} finally {
endOperation();
}
}
|
public void persist(Object obj,
OpCallbacks call) {
persist(obj, null, true, call);
}
|
public OpenJPAStateManager persist(Object obj,
Object id,
OpCallbacks call) {
return persist(obj, id, true, call);
}
|
public void persist(Object obj,
boolean explicit,
OpCallbacks call) {
persist(obj, null, explicit, call);
}
Persist the given object. Indicate whether this was an explicit persist
(PNEW) or a provisonal persist (PNEWPROVISIONAL) |
public OpenJPAStateManager persist(Object obj,
Object id,
boolean explicit,
OpCallbacks call) {
if (obj == null)
return null;
beginOperation(true);
try {
assertWriteOperation();
StateManagerImpl sm = getStateManagerImpl(obj, true);
if (!_operating.add(obj))
return sm;
int action = processArgument(OpCallbacks.OP_PERSIST, obj, sm, call);
if (action == OpCallbacks.ACT_NONE)
return sm;
// ACT_CASCADE
if ((action & OpCallbacks.ACT_RUN) == 0) {
if (sm != null)
sm.cascadePersist(call);
else
cascadeTransient(OpCallbacks.OP_PERSIST, obj, call,
"persist");
return sm;
}
// ACT_RUN
PersistenceCapable pc;
if (sm != null) {
if (sm.isDetached())
throw new ObjectExistsException(_loc.get
("persist-detached", Exceptions.toString(obj))).
setFailedObject(obj);
if (!sm.isEmbedded()) {
sm.persist();
_cache.persist(sm);
if ((action & OpCallbacks.ACT_CASCADE) != 0)
sm.cascadePersist(call);
return sm;
}
// an embedded field; notify the owner that the value has
// changed by becoming independently persistent
sm.getOwner().dirty(sm.getOwnerIndex());
_cache.persist(sm);
pc = sm.getPersistenceCapable();
} else {
pc = assertPersistenceCapable(obj);
if (pc.pcIsDetached() == Boolean.TRUE)
throw new ObjectExistsException(_loc.get
("persist-detached", Exceptions.toString(obj))).
setFailedObject(obj);
}
ClassMetaData meta = _conf.getMetaDataRepositoryInstance().
getMetaData(obj.getClass(), _loader, true);
fireLifecycleEvent(obj, null, meta, LifecycleEvent.BEFORE_PERSIST);
// create id for instance
if (id == null) {
if (meta.getIdentityType() == ClassMetaData.ID_APPLICATION)
id = ApplicationIds.create(pc, meta);
else if (meta.getIdentityType() == ClassMetaData.ID_UNKNOWN)
throw new UserException(_loc.get("meta-unknownid", meta));
else
id = StateManagerId.newInstance(this);
}
// make sure we don't already have the instance cached
checkForDuplicateId(id, obj);
// if had embedded sm, null it
if (sm != null)
pc.pcReplaceStateManager(null);
// create new sm
sm = new StateManagerImpl(id, meta, this);
if ((_flags & FLAG_ACTIVE) != 0) {
if (explicit)
sm.initialize(pc, PCState.PNEW);
else
sm.initialize(pc, PCState.PNEWPROVISIONAL);
} else
sm.initialize(pc, PCState.PNONTRANSNEW);
if ((action & OpCallbacks.ACT_CASCADE) != 0)
sm.cascadePersist(call);
return sm;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
Persist the given object. Indicate whether this was an explicit persist
(PNEW) or a provisonal persist (PNEWPROVISIONAL).
See Broker for details on this method. |
public void persistAll(Collection objs,
OpCallbacks call) {
persistAll(objs, true, call);
}
|
public void persistAll(Collection objs,
boolean explicit,
OpCallbacks call) {
if (objs.isEmpty())
return;
beginOperation(true);
List exceps = null;
try {
assertWriteOperation();
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
persist(itr.next(), explicit, call);
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
} finally {
endOperation();
}
throwNestedExceptions(exceps, false);
}
Persist the given objects. Indicate whether this was an explicit persist
(PNEW) or a provisonal persist (PNEWPROVISIONAL). |
public void popFetchConfiguration() {
if (_fcs == null || _fcs.isEmpty())
throw new UserException(_loc.get("fetch-configuration-stack-empty"));
_fc = (FetchConfiguration) _fcs.removeLast();
}
|
public void preFlush() {
beginOperation(true);
try {
if ((_flags & FLAG_ACTIVE) != 0)
flushSafe(FLUSH_LOGICAL);
} finally {
endOperation();
}
}
|
public Object processArgument(Object oid) {
return oid;
}
|
public Object processReturn(Object oid,
OpenJPAStateManager sm) {
return (sm == null) ? null : sm.getManagedInstance();
}
|
public FetchConfiguration pushFetchConfiguration() {
if (_fcs == null)
_fcs = new LinkedList();
_fcs.add(_fc);
_fc = (FetchConfiguration) _fc.clone();
return _fc;
}
|
public Object putUserObject(Object key,
Object val) {
beginOperation(false);
try {
if (val == null)
return (_userObjects == null) ? null : _userObjects.remove(key);
if (_userObjects == null)
_userObjects = new HashMap();
return _userObjects.put(key, val);
} finally {
endOperation();
}
}
|
public void refresh(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
assertNontransactionalRead();
gatherCascadeRefresh(obj, call);
if (_operating.isEmpty())
return;
if (_operating.size() == 1)
refreshInternal(_operating.iterator().next(), call);
else
refreshInternal(_operating, call);
} finally {
endOperation();
}
}
|
public void refreshAll(Collection objs,
OpCallbacks call) {
if (objs.isEmpty())
return;
beginOperation(true);
try {
assertNontransactionalRead();
for (Iterator itr = objs.iterator(); itr.hasNext();)
gatherCascadeRefresh(itr.next(), call);
if (_operating.isEmpty())
return;
if (_operating.size() == 1)
refreshInternal(_operating.iterator().next(), call);
else
refreshInternal(_operating, call);
} finally {
endOperation();
}
}
|
protected void refreshInternal(Collection objs,
OpCallbacks call) {
List exceps = null;
try {
// collect instances that need a refresh
Collection load = null;
StateManagerImpl sm;
Object obj;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
obj = itr.next();
if (obj == null)
continue;
try {
sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_REFRESH, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
continue;
if (sm != null) {
if (sm.isDetached())
throw newDetachedException(obj, "refresh");
else if (sm.beforeRefresh(true)) {
if (load == null)
load = new ArrayList(objs.size());
load.add(sm);
}
} else if (assertPersistenceCapable(obj).pcIsDetached()
== Boolean.TRUE)
throw newDetachedException(obj, "refresh");
} catch (OpenJPAException ke) {
exceps = add(exceps, ke);
}
}
// refresh all
if (load != null) {
Collection failed = _store.loadAll(load, null,
StoreManager.FORCE_LOAD_REFRESH, _fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps, newObjectNotFoundException(failed));
// perform post-refresh transitions and make sure all fetch
// group fields are loaded
for (Iterator itr = load.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
if (failed != null && failed.contains(sm.getId()))
continue;
try {
sm.afterRefresh();
sm.load(_fc, StateManagerImpl.LOAD_FGS, null, null,
false);
} catch (OpenJPAException ke) {
exceps = add(exceps, ke);
}
}
}
// now invoke postRefresh on all the instances
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
sm = getStateManagerImpl(itr.next(), true);
if (sm != null && !sm.isDetached())
fireLifecycleEvent(sm.getManagedInstance(), null,
sm.getMetaData(), LifecycleEvent.AFTER_REFRESH);
} catch (OpenJPAException ke) {
exceps = add(exceps, ke);
}
}
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
}
throwNestedExceptions(exceps, false);
}
This method is called with the full set of objects reachable via
cascade-refresh relations from the user-given instances. |
protected void refreshInternal(Object obj,
OpCallbacks call) {
try {
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_REFRESH, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm != null) {
if (sm.isDetached())
throw newDetachedException(obj, "refresh");
else if (sm.beforeRefresh(false)) {
sm.load(_fc, StateManagerImpl.LOAD_FGS, null, null, false);
sm.afterRefresh();
}
fireLifecycleEvent(sm.getManagedInstance(), null,
sm.getMetaData(), LifecycleEvent.AFTER_REFRESH);
} else if (assertPersistenceCapable(obj).pcIsDetached()
== Boolean.TRUE)
throw newDetachedException(obj, "refresh");
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
}
}
Optimization for single-object refresh. |
public void release(Object obj,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(false);
try {
StateManagerImpl sm = getStateManagerImpl(obj, true);
int action = processArgument(OpCallbacks.OP_RELEASE, obj, sm, call);
if (sm == null)
return;
if ((action & OpCallbacks.ACT_RUN) != 0 && sm.isPersistent()) {
boolean pending = sm.isPendingTransactional();
sm.release(true);
if (pending)
removeFromPendingTransaction(sm);
}
}
catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void releaseAll(Collection objs,
OpCallbacks call) {
beginOperation(false);
try {
List exceps = null;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
try {
release(itr.next(), call);
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
throwNestedExceptions(exceps, false);
} finally {
endOperation();
}
}
|
public void releaseSavepoint() {
beginOperation(false);
try {
if (_savepoints == null || _savepoints.isEmpty())
throw new UserException(_loc.get("no-lastsavepoint"));
releaseSavepoint((String) _savepoints.get
(_savepoints.size() - 1));
} finally {
endOperation();
}
}
|
public void releaseSavepoint(String savepoint) {
beginOperation(false);
try {
assertActiveTransaction();
int index = (_savepoints == null) ? -1
: _savepoints.indexOf(savepoint);
if (index < 0)
throw new UserException(_loc.get("no-savepoint", savepoint));
// clear old in reverse
OpenJPASavepoint save;
while (_savepoints.size() > index + 1) {
save = (OpenJPASavepoint) _savepoints.remove
(_savepoints.size() - 1);
save.release(false);
}
save = (OpenJPASavepoint) _savepoints.remove(index);
save.release(true);
if (_savepointCache != null)
_savepointCache.clear();
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
} finally {
endOperation();
}
}
|
void removeDereferencedDependent(StateManagerImpl sm) {
lock();
try {
boolean removed = false;
if (_derefAdditions != null)
removed = _derefAdditions.remove(sm);
if (!removed && (_derefCache == null || !_derefCache.remove(sm)))
throw new InvalidStateException(_loc.get("not-derefed",
Exceptions.toString(sm.getManagedInstance()))).
setFailedObject(sm.getManagedInstance()).
setFatal(true);
} finally {
unlock();
}
}
Remove the given previously dereferenced dependent object from the
cache. It is now referenced. |
void removeFromPendingTransaction(StateManagerImpl sm) {
lock();
try {
if (_pending != null)
_pending.remove(sm);
if (_derefCache != null && !sm.isPersistent())
_derefCache.remove(sm);
} finally {
unlock();
}
}
Notify the persistence manager that the given state manager should
be removed from the set of instances involved in the next transaction. |
void removeFromTransaction(StateManagerImpl sm) {
lock();
try {
if (_transCache != null)
// intentional direct access; we don't want to recompute
// dirtiness while removing instances from the transaction
_transCache.remove(sm);
if (_derefCache != null && !sm.isPersistent())
_derefCache.remove(sm);
} finally {
unlock();
}
}
Notify the persistence manager that the given state manager should
be removed from the set of instances involved in the current transaction. |
public void removeLifecycleListener(Object listener) {
beginOperation(false);
try {
_lifeEventManager.removeListener(listener);
} finally {
endOperation();
}
}
|
public void removeTransactionListener(Object tl) {
beginOperation(false);
try {
if (_transEventManager.removeListener(tl)
&& (tl instanceof RemoteCommitEventManager))
_flags &= ~FLAG_REMOTE_LISTENER;
} finally {
endOperation();
}
}
|
public void retrieve(Object obj,
boolean dfgOnly,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
assertOpen();
assertNontransactionalRead();
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_RETRIEVE, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm != null) {
if (sm.isDetached())
throw newDetachedException(obj, "retrieve");
if (sm.isPersistent()) {
int mode = (dfgOnly) ? StateManagerImpl.LOAD_FGS
: StateManagerImpl.LOAD_ALL;
sm.beforeRead(-1);
sm.load(_fc, mode, null, null, false);
}
} else if (assertPersistenceCapable(obj).pcIsDetached()
== Boolean.TRUE)
throw newDetachedException(obj, "retrieve");
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
|
public void retrieveAll(Collection objs,
boolean dfgOnly,
OpCallbacks call) {
if (objs.isEmpty())
return;
if (objs.size() == 1) {
retrieve(objs.iterator().next(), dfgOnly, call);
return;
}
List exceps = null;
beginOperation(true);
try {
assertOpen();
assertNontransactionalRead();
// collect all hollow instances for load
Object obj;
Collection load = null;
StateManagerImpl sm;
Collection sms = new ArrayList(objs.size());
for (Iterator itr = objs.iterator(); itr.hasNext();) {
obj = itr.next();
if (obj == null)
continue;
try {
sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_RETRIEVE, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
continue;
if (sm != null) {
if (sm.isDetached())
throw newDetachedException(obj, "retrieve");
if (sm.isPersistent()) {
sms.add(sm);
if (sm.getPCState() == PCState.HOLLOW) {
if (load == null)
load = new ArrayList();
load.add(sm);
}
}
} else if (assertPersistenceCapable(obj).pcIsDetached()
== Boolean.TRUE)
throw newDetachedException(obj, "retrieve");
} catch (UserException ue) {
exceps = add(exceps, ue);
}
}
// load all hollow instances
Collection failed = null;
if (load != null) {
int mode = (dfgOnly) ? _store.FORCE_LOAD_DFG
: _store.FORCE_LOAD_ALL;
failed = _store.loadAll(load, null, mode, _fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps, newObjectNotFoundException(failed));
}
// retrieve all non-failed instances
for (Iterator itr = sms.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
if (failed != null && failed.contains(sm.getId()))
continue;
int mode = (dfgOnly) ? StateManagerImpl.LOAD_FGS
: StateManagerImpl.LOAD_ALL;
try {
sm.beforeRead(-1);
sm.load(_fc, mode, null, null, false);
} catch (OpenJPAException ke) {
exceps = add(exceps, ke);
}
}
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
throwNestedExceptions(exceps, false);
}
|
public void rollback() {
beginOperation(false);
try {
assertTransactionOperation();
javax.transaction.Transaction trans =
_runtime.getTransactionManager().getTransaction();
if (trans != null)
trans.rollback();
} catch (OpenJPAException ke) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), ke);
throw ke;
} catch (Exception e) {
if (_log.isTraceEnabled())
_log.trace(_loc.get("end-trans-error"), e);
throw new StoreException(e);
} finally {
endOperation();
}
}
|
public void rollbackAndResume() {
endAndResume(false);
}
|
public void rollbackToSavepoint() {
beginOperation(false);
try {
if (_savepoints == null || _savepoints.isEmpty())
throw new UserException(_loc.get("no-lastsavepoint"));
rollbackToSavepoint((String) _savepoints.get
(_savepoints.size() - 1));
} finally {
endOperation();
}
}
|
public void rollbackToSavepoint(String savepoint) {
beginOperation(false);
try {
assertActiveTransaction();
int index = (_savepoints == null) ? -1
: _savepoints.indexOf(savepoint);
if (index < 0)
throw new UserException(_loc.get("no-savepoint", savepoint));
// clear old in reverse
OpenJPASavepoint save;
while (_savepoints.size() > index + 1) {
save = (OpenJPASavepoint) _savepoints.remove
(_savepoints.size() - 1);
save.release(false);
}
save = (OpenJPASavepoint) _savepoints.remove(index);
Collection saved = save.rollback(_savepoints.values());
if (_savepointCache != null)
_savepointCache.clear();
if (hasTransactionalObjects()) {
// build up a new collection of states
TransactionalCache oldTransCache = _transCache;
TransactionalCache newTransCache = new TransactionalCache
(_orderDirty);
_transCache = null;
// currently there is the assumption that incremental
// flush is either a) not allowed, or b) required
// pre-savepoint. this solves a number of issues including
// storing flushed states as well as OID handling.
// if future plugins do not follow this, we need to cache
// more info per state
SavepointFieldManager fm;
StateManagerImpl sm;
for (Iterator itr = saved.iterator(); itr.hasNext();) {
fm = (SavepointFieldManager) itr.next();
sm = fm.getStateManager();
sm.rollbackToSavepoint(fm);
oldTransCache.remove(sm);
if (sm.isDirty())
newTransCache.addDirty(sm);
else
newTransCache.addClean(sm);
}
for (Iterator itr = oldTransCache.iterator(); itr.hasNext();) {
sm = (StateManagerImpl) itr.next();
sm.rollback();
removeFromTransaction(sm);
}
_transCache = newTransCache;
}
}
catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
} finally {
endOperation();
}
}
|
public void setAuthentication(String user,
String pass) {
_user = user;
_pass = pass;
}
Set the persistence manager's authentication. This is the first
method called after construction. |
public void setAutoClear(int val) {
assertOpen();
_autoClear = val;
}
|
public void setAutoDetach(int detachFlags) {
assertOpen();
_autoDetach = detachFlags;
}
|
public void setAutoDetach(int detachFlag,
boolean on) {
assertOpen();
if (on)
_autoDetach |= detachFlag;
else
_autoDetach &= ~detachFlag;
}
|
public void setDetachState(int mode) {
assertOpen();
_detachState = mode;
}
|
public void setDetachedNew(boolean isNew) {
assertOpen();
_detachedNew = isNew;
}
|
void setDirty(StateManagerImpl sm,
boolean firstDirty) {
if (sm.isPersistent())
_flags |= FLAG_FLUSH_REQUIRED;
if (_savepoints != null && !_savepoints.isEmpty()) {
if (_savepointCache == null)
_savepointCache = new HashSet();
_savepointCache.add(sm);
}
if (firstDirty && sm.isTransactional()) {
lock();
try {
// cache dirty instance
if (!hasTransactionalObjects())
_transCache = new TransactionalCache(_orderDirty);
_transCache.addDirty(sm);
// also record that the class is dirty
if (sm.isNew()) {
if (_persistedClss == null)
_persistedClss = new HashSet();
_persistedClss.add(sm.getMetaData().getDescribedType());
} else if (sm.isDeleted()) {
if (_deletedClss == null)
_deletedClss = new HashSet();
_deletedClss.add(sm.getMetaData().getDescribedType());
} else {
if (_updatedClss == null)
_updatedClss = new HashSet();
_updatedClss.add(sm.getMetaData().getDescribedType());
}
// if tracking changes and this instance wasn't already dirty,
// add to changed set; we use this for detecting instances that
// enter the transaction during pre store
if ((_flags & FLAG_PRESTORING) != 0) {
if (_transAdditions == null)
_transAdditions = new HashSet();
_transAdditions.add(sm);
}
} finally {
unlock();
}
}
}
Notification that the given instance has been dirtied. This
notification is given when an object first transitions to a dirty state,
and every time the object is modified by the user thereafter. |
public void setEvictFromDataCache(boolean evict) {
assertOpen();
_evictDataCache = evict;
}
|
public void setIgnoreChanges(boolean val) {
assertOpen();
_ignoreChanges = val;
}
|
public void setImplicitBehavior(OpCallbacks call,
RuntimeExceptionTranslator ex) {
if (_call == null)
_call = call;
if (_extrans == null)
_extrans = ex;
}
|
public void setLifecycleListenerCallbackMode(int mode) {
beginOperation(false);
try {
_lifeCallbackMode = mode;
_lifeEventManager.setFailFast((mode & CALLBACK_FAIL_FAST) != 0);
} finally {
endOperation();
}
}
|
public void setMultithreaded(boolean multithreaded) {
assertOpen();
_multithreaded = multithreaded;
if (multithreaded && _lock == null)
_lock = new ReentrantLock();
else if (!multithreaded)
_lock = null;
}
|
public void setNontransactionalRead(boolean val) {
assertOpen();
if ((_flags & FLAG_PRESTORING) != 0)
throw new UserException(_loc.get("illegal-op-in-prestore"));
// make sure the runtime supports it
if (val && !_conf.supportedOptions().contains
(_conf.OPTION_NONTRANS_READ))
throw new UnsupportedException(_loc.get
("nontrans-read-not-supported"));
_nontransRead = val;
}
|
public void setNontransactionalWrite(boolean val) {
assertOpen();
if ((_flags & FLAG_PRESTORING) != 0)
throw new UserException(_loc.get("illegal-op-in-prestore"));
_nontransWrite = val;
}
|
public void setOptimistic(boolean val) {
assertOpen();
if ((_flags & FLAG_ACTIVE) != 0)
throw new InvalidStateException(_loc.get("trans-active",
"Optimistic"));
// make sure the runtime supports it
if (val && !_conf.supportedOptions().contains(_conf.OPTION_OPTIMISTIC))
throw new UnsupportedException(_loc.get
("optimistic-not-supported"));
_optimistic = val;
}
|
public void setOrderDirtyObjects(boolean order) {
_orderDirty = order;
}
|
public void setPopulateDataCache(boolean cache) {
assertOpen();
_populateDataCache = cache;
}
|
public void setRestoreState(int val) {
assertOpen();
if ((_flags & FLAG_ACTIVE) != 0)
throw new InvalidStateException(_loc.get("trans-active",
"Restore"));
_restoreState = val;
}
|
public void setRetainState(boolean val) {
assertOpen();
if ((_flags & FLAG_PRESTORING) != 0)
throw new UserException(_loc.get("illegal-op-in-prestore"));
_retainState = val;
}
|
public void setRollbackOnly() {
setRollbackOnly(new UserException());
}
|
public void setRollbackOnly(Throwable cause) {
beginOperation(true);
try {
assertTransactionOperation();
setRollbackOnlyInternal(cause);
} finally {
endOperation();
}
}
|
public void setSavepoint(String name) {
beginOperation(true);
try {
assertActiveTransaction();
if (_savepoints != null && _savepoints.containsKey(name))
throw new UserException(_loc.get("savepoint-exists", name));
if (hasFlushed() && !_spm.supportsIncrementalFlush())
throw new UnsupportedException(_loc.get
("savepoint-flush-not-supported"));
OpenJPASavepoint save = _spm.newSavepoint(name, this);
if (_savepoints == null || _savepoints.isEmpty()) {
save.save(getTransactionalStates());
_savepoints = new LinkedMap();
} else {
if (_savepointCache == null)
save.save(Collections.EMPTY_LIST);
else {
save.save(_savepointCache);
_savepointCache.clear();
}
}
_savepoints.put(name, save);
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
} finally {
endOperation();
}
}
|
void setStateManager(Object id,
StateManagerImpl sm,
int status) {
lock();
try {
switch (status) {
case STATUS_INIT:
_cache.add(sm);
break;
case STATUS_TRANSIENT:
_cache.remove(id, sm);
break;
case STATUS_OID_ASSIGN:
assignObjectId(_cache, id, sm);
break;
case STATUS_COMMIT_NEW:
_cache.commitNew(id, sm);
break;
default:
throw new InternalException();
}
}
finally {
unlock();
}
}
Set the cached StateManager for the instance that had the given oid.
This method must not be called multiple times for new instances. |
public void setSyncWithManagedTransactions(boolean sync) {
assertOpen();
_syncManaged = sync;
}
|
public void setSynchronization(Synchronization sync) {
assertOpen();
_sync = sync;
}
|
public void setTrackChangesByType(boolean largeTransaction) {
assertOpen();
_largeTransaction = largeTransaction;
}
|
public void setTransactionListenerCallbackMode(int mode) {
beginOperation(false);
try {
_transCallbackMode = mode;
_transEventManager.setFailFast((mode & CALLBACK_FAIL_FAST) != 0);
} finally {
endOperation();
}
}
|
public boolean syncWithManagedTransaction() {
assertOpen();
lock();
try {
if ((_flags & FLAG_ACTIVE) != 0)
return true;
if (!_managed)
throw new InvalidStateException(_loc.get("trans-not-managed"));
if (_factory.syncWithManagedTransaction(this, false)) {
beginInternal();
return true;
}
return false;
} finally {
unlock();
}
}
|
public void transactional(Object obj,
boolean updateVersion,
OpCallbacks call) {
if (obj == null)
return;
beginOperation(true);
try {
StateManagerImpl sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_TRANSACTIONAL, obj, sm, call)
& OpCallbacks.ACT_RUN) == 0)
return;
if (sm != null && sm.isPersistent()) {
assertActiveTransaction();
sm.transactional();
sm.load(_fc, StateManagerImpl.LOAD_FGS, null, null, false);
sm.setCheckVersion(true);
if (updateVersion)
sm.setUpdateVersion(true);
_flags |= FLAG_FLUSH_REQUIRED; // version check/up
} else if (sm == null) {
// manage transient instance
ClassMetaData meta = _conf.getMetaDataRepositoryInstance().
getMetaData(obj.getClass(), _loader, true);
Object id = StateManagerId.newInstance(this);
sm = new StateManagerImpl(id, meta, this);
sm.initialize(assertPersistenceCapable(obj),
PCState.TCLEAN);
}
}
catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
Make the given instances transactional. |
public void transactionalAll(Collection objs,
boolean updateVersion,
OpCallbacks call) {
if (objs.isEmpty())
return;
if (objs.size() == 1) {
transactional(objs.iterator().next(), updateVersion, call);
return;
}
beginOperation(true);
try {
// collect all hollow instances for load, and make unmananged
// instances transient-transactional
Collection load = null;
Object obj;
StateManagerImpl sm;
ClassMetaData meta;
Collection sms = new ArrayList(objs.size());
List exceps = null;
for (Iterator itr = objs.iterator(); itr.hasNext();) {
obj = itr.next();
if (obj == null)
continue;
try {
sm = getStateManagerImpl(obj, true);
if ((processArgument(OpCallbacks.OP_TRANSACTIONAL, obj, sm,
call) & OpCallbacks.ACT_RUN) == 0)
continue;
if (sm == null) {
// manage transient instance
meta = _conf.getMetaDataRepositoryInstance().
getMetaData(obj.getClass(), _loader, true);
sm = new StateManagerImpl
(StateManagerId.newInstance(this), meta, this);
sm.initialize(assertPersistenceCapable(obj),
PCState.TCLEAN);
} else if (sm.isPersistent()) {
assertActiveTransaction();
sms.add(sm);
if (sm.getPCState() == PCState.HOLLOW) {
if (load == null)
load = new ArrayList();
load.add(sm);
}
sm.setCheckVersion(true);
if (updateVersion)
sm.setUpdateVersion(true);
_flags |= FLAG_FLUSH_REQUIRED; // version check/up
}
}
catch (UserException ue) {
exceps = add(exceps, ue);
}
}
// load all hollow instances
Collection failed = null;
if (load != null) {
failed = _store.loadAll(load, null, _store.FORCE_LOAD_NONE,
_fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps,
newObjectNotFoundException(failed));
}
transactionalStatesAll(sms, failed, exceps);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
} finally {
endOperation();
}
}
Make the given instances transactional. |
public void unlock() {
if (_lock != null)
_lock.unlock();
}
|
public void validateChanges() {
beginOperation(true);
try {
// if no trans, just return; if active datastore trans, flush
if ((_flags & FLAG_ACTIVE) == 0)
return;
if ((_flags & FLAG_STORE_ACTIVE) != 0) {
flush();
return;
}
// make sure the runtime supports inc flush
if (!_conf.supportedOptions().contains(_conf.OPTION_INC_FLUSH))
throw new UnsupportedException(_loc.get
("incremental-flush-not-supported"));
try {
flushSafe(FLUSH_ROLLBACK);
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new StoreException(re);
}
}
finally {
endOperation();
}
}
|