Package org.exolab.castor.persist

Examples of org.exolab.castor.persist.LockEngine


     * Lyfe-cycle methods to close JDOManager instance and initiate resource cleanup.
     */
    public void close () {
        try {
            ConnectionFactory factory = getConnectionFactory();
            LockEngine engine = ((AbstractConnectionFactory) factory).getEngine();
            engine.closeCaches();
        } catch (MappingException e) {
            LOG.fatal ("Problem closing down caches", e);
        }
    }
View Full Code Here


            if (internalValue != null) {
                Class valueClass = internalValue.getClass();

                if (paramClass.isAssignableFrom(valueClass)) {
                    LockEngine lockEngine = ((AbstractDatabaseImpl) _database).getLockEngine();
                    ClassMolder molder = lockEngine.getClassMolder(valueClass);

                    if (molder != null) {
                        Identity temp = molder.getActualIdentity(
                                _database.getClassLoader(), internalValue);
                        if (temp == null) {
View Full Code Here

        if (identity == null) {
            throw new PersistenceException("Identities can't be null!");
        }

        LockEngine engine = molder.getLockEngine();
       
        oid = new OID(molder, identity);
        accessMode = molder.getAccessMode(suggestedAccessMode);
        if (accessMode == AccessMode.ReadOnly) {
            objectInTx = _tracker.getObjectForOID(engine, oid, true);
View Full Code Here

        Object objectInTx;
        OID oid;
        AccessMode accessMode;

        ClassMolder molder = proposedObject.getActualClassMolder();
        LockEngine engine = molder.getLockEngine();

        if (identity == null) {
            throw new PersistenceException("Identities can't be null!");
        }

        // Test that the object to be loaded (which we will fill in) is of an
        // appropriate type for our molder.
        if (proposedObject.getEntity() != null
                && !molder.getJavaClass(_db.getClassLoader()).isAssignableFrom(
                        proposedObject.getProposedEntityClass())) {
            throw new PersistenceException(Messages.format("persist.typeMismatch",
                    molder.getName(), proposedObject.getProposedEntityClass()));
        }
       
        oid = new OID(molder, identity);
        accessMode = molder.getAccessMode(suggestedAccessMode);
        if (accessMode == AccessMode.ReadOnly) {
            objectInTx = _tracker.getObjectForOID(engine, oid, true);
        } else {
            objectInTx = _tracker.getObjectForOID(engine, oid, false);
        }

        if (objectInTx != null) {
            // Object exists in this transaction.

            // If the object has been loaded, but the instance sugguested to
            // be loaded into is not the same as the loaded instance,
            // error is reported.
           
            // TODO [WG]: could read && propsedObject != objectInTransaction
            if (proposedObject.getEntity() != null
                    && proposedObject.getEntity() != objectInTx) {
                throw new PersistenceException(Messages.format(
                        "persist.multipleLoad", molder.getName(), identity));
            }

            // If the object has been loaded in this transaction from a
            // different engine this is an error. If the object has been
            // deleted in this transaction, it cannot be re-loaded. If the
            // object has been created in this transaction, it cannot be
            // re-loaded but no error is reported.
            if (engine != _tracker.getMolderForObject(objectInTx).getLockEngine()) {
                throw new PersistenceException(Messages.format(
                        "persist.multipleLoad", molder.getName(), identity));
            }

            // Objects marked deleted in the transaction therefore we
            // throw a ObjectNotFoundException to signal that object isn't
            // available any more.
            if (_tracker.isDeleted(objectInTx)) {
                throw new ObjectNotFoundException(Messages.format(
                        "persist.objectNotFound", molder.getName(), identity));
            }

            // ssa, multi classloader feature (note - this code appears to be
            // duplicated, yet different, in both cases. Why?)
            // ssa, FIXME : Are the two following statements equivalent ? (bug
            // 998)
            // if ( ! molder.getJavaClass().isAssignableFrom(
            // entry.object.getClass() ) )
            // if ( ! molder.getJavaClass( _db.getClassLoader()
            // ).isAssignableFrom( entry.object.getClass() ) )
            if (!molder.getJavaClass(_db.getClassLoader()).isAssignableFrom(
                    objectInTx.getClass())) {
                throw new PersistenceException(Messages.format(
                        "persist.typeMismatch", molder.getName(),
                        objectInTx.getClass()));
            }

            // If the object has been created in this transaction, don't bother
            // testing access mode.
            if (_tracker.isCreated(objectInTx)) {
                return objectInTx;
            }

            if ((accessMode == AccessMode.Exclusive || accessMode == AccessMode.DbLocked)
                    && !_tracker.getOIDForObject(objectInTx)
                            .isDbLock()) {
                // If we are in exclusive mode and object has not been
                // loaded in exclusive mode before, then we have a
                // problem. We cannot return an object that is not
                // synchronized with the database, but we cannot
                // synchronize a live object.
                throw new PersistenceException(Messages.format(
                        "persist.lockConflict", molder.getName(), identity));
            }

            return objectInTx;
        }

        // Load (or reload, in case the object is stored in a acache) the object
        // through the persistence engine with the requested lock. This might report
        // failure (object no longer exists), hold until a suitable lock is granted
        // (or fail to grant), or report error with the persistence engine.
        try {
            if (proposedObject.getEntity() != null) {
                objectInTx = proposedObject.getEntity();
            } else {
                // ssa, multi classloader feature
                // ssa, FIXME : No better way to do that ?
                // object = molder.newInstance();
                if (_instanceFactory != null) {
                    objectInTx = _instanceFactory.newInstance(molder
                            .getName(), _db.getClassLoader());
                } else {
                    objectInTx = molder.newInstance(_db
                            .getClassLoader());
                }
               
                proposedObject.setProposedEntityClass(objectInTx.getClass());
                proposedObject.setActualEntityClass(objectInTx.getClass());
                proposedObject.setEntity(objectInTx);
            }

            molder.setIdentity(this, objectInTx, identity);
            _tracker.trackObject(molder, oid, objectInTx);
            OID newoid = engine.load(this, oid, proposedObject,
                    suggestedAccessMode, _lockTimeout, results);
                   
            if (proposedObject.isExpanded()) {
                // Remove old OID from ObjectTracker
                _tracker.untrackObject(objectInTx);
               
                // Create new OID
                ClassMolder actualClassMolder = engine.getClassMolder(
                        proposedObject.getActualEntityClass());
                OID actualOID = new OID(actualClassMolder, identity);
                actualClassMolder.setIdentity(this, proposedObject.getEntity(), identity);

                // Create instance of 'expanded object'
                Object expandedObject = null;
                try {
                    expandedObject = actualClassMolder.newInstance(getClassLoader());
                } catch (Exception e) {
                    LOG.error("Cannot create instance of " + molder.getName());
                    throw new PersistenceException(
                            "Cannot craete instance of " + molder.getName());
                }

                // Add new OID to ObjectTracker
                _tracker.trackObject(molder, actualOID, expandedObject);
               
                ProposedEntity proposedExpanded = new ProposedEntity(proposedObject);
                proposedExpanded.setEntity(expandedObject);
                proposedExpanded.setObjectLockObjectToBeIgnored(true);

                // reload 'expanded object' using correct ClassMolder
                engine.load(this, actualOID, proposedExpanded,
                        suggestedAccessMode, _lockTimeout, results);

                objectInTx = proposedExpanded.getEntity();
            } else {
                // rehash the object entry, because oid might have changed!
                _tracker.trackOIDChange(objectInTx, engine, oid, newoid);
            }           
           
        } catch (ClassCastException except) {
            _tracker.untrackObject(objectInTx);
            throw except;
        } catch (ObjectNotFoundException except) {
            _tracker.untrackObject(objectInTx);
            throw except;
        } catch (ConnectionFailedException except) {
            _tracker.untrackObject(objectInTx);
            throw except;
        } catch (LockNotGrantedException except) {
            _tracker.untrackObject(objectInTx);
            throw except;
        } catch (ClassNotPersistenceCapableException except) {
            _tracker.untrackObject(objectInTx);
            throw new PersistenceException(Messages.format("persist.nested",
                    except));
        } catch (InstantiationException e) {
            _tracker.untrackObject(objectInTx);
            throw new PersistenceException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            _tracker.untrackObject(objectInTx);
            throw new PersistenceException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            _tracker.untrackObject(objectInTx);
            throw new PersistenceException(e.getMessage(), e);
        }

        // Need to copy the contents of this object from the cached
        // copy and deal with it based on the transaction semantics.
        // If the mode is read-only we release the lock and forget about
        // it in the contents of this transaction. Otherwise we record
        // the object in this transaction.
        try {
            if (_callback != null) {
                _callback.using(objectInTx, _db);
                _callback.loaded(objectInTx, accessMode);
            } else if (molder.getCallback() != null) {
                molder.getCallback().using(objectInTx, _db);
                molder.getCallback().loaded(objectInTx, accessMode);
            }
        } catch (Exception except) {
            release(objectInTx);
            throw new PersistenceException(Messages.format("persist.nested",
                    except));
        }

        if (accessMode == AccessMode.ReadOnly) {
            // Mark it read-only.
            _tracker.markReadOnly(objectInTx);

            // Release the lock on this object.
            engine.releaseLock(this, oid);

        }
        return objectInTx;
    }
View Full Code Here

        if (object == null) {
            throw new PersistenceException(
                    "Attempted to mark a null object as created.");
        }

        LockEngine engine = molder.getLockEngine();
       
        // Make sure the object has not beed persisted in this transaction.
        Identity identity = molder.getIdentity(this, object);

        // if autoStore is specified, we relieve user life a little bit here
        // so that if an object create automatically and user create it
        // again, it won't receive exception
        if (_autoStore && _tracker.isTracking(object)) {
            return;
        }

        if (_tracker.isDeleted(object)) {
            OID deletedoid = _tracker.getOIDForObject(object);
            throw new PersistenceException(Messages.format(
                    "persist.objectAlreadyPersistent", object.getClass()
                            .getName(), (deletedoid != null) ? deletedoid
                            .getIdentity() : null));
        }

        // Create the object. This can only happen once for each object in
        // all transactions running on the same engine, so after creation
        // add a new entry for this object and use this object as the view
        // Note that the oid which is created is for a dependent object;
        // this is not a change to the rootObjectOID, and therefore doesn't get
        // trackOIDChange()d.
        OID oid = new OID(molder, rootObjectOID, identity);

        // You shouldn't be able to modify an object marked read-only in this
        // transaction.
        Object trackedObject = _tracker.getObjectForOID(engine, oid, false);
        if (identity != null && trackedObject != null) {
            if (trackedObject != object) {
                throw new DuplicateIdentityException(
                        "Object being tracked with the OID created for a dependent "
                        + "object does not match the object to be marked for "
                        + "creation. Fundamental Tracking Error.");
            } else if (_tracker.isDeleted(object)) {
                // Undelete it.
                _tracker.unmarkDeleted(object);
            }
        }

        try {
            _tracker.trackObject(molder, oid, object);
            _tracker.markCreating(object);

            if (_callback != null) {
                _callback.creating(object, _db);
            } else if (molder.getCallback() != null) {
                molder.getCallback().creating(object, _db);
            }

            engine.markCreate(this, oid, object);
        } catch (LockNotGrantedException lneg) {
            // yip: do we need LockNotGrantedException, or should we
            // removed them?
            _tracker.untrackObject(object);
            // Note: This used to throw a very strange empty-string
View Full Code Here

            // Must perform creation after object is recorded in transaction
            // to prevent circular references.
            Object toBeCreated = creatableIterator.next();
            OID toBeCreatedOID = _tracker.getOIDForObject(toBeCreated);
            ClassMolder toBeCreatedMolder = _tracker.getMolderForObject(toBeCreated);
            LockEngine toBeCreatedLockEngine = toBeCreatedMolder.getLockEngine();

            try {
                // Allow users to create inside the callback.
                // We do this by rechecking that the object is still marked
                // creatable;
                // this will tell us if another process got to it first.
                if (_tracker.isCreating(toBeCreated)) {

                    if (_callback != null) {
                        _callback.creating(toBeCreated, _db);
                    } else if (toBeCreatedMolder.getCallback() != null) {
                        toBeCreatedMolder.getCallback().creating(toBeCreated, _db);
                    }

                    OID oid = toBeCreatedLockEngine.create(this,
                            toBeCreatedOID, toBeCreated);

                    if (oid.getIdentity() == null) {
                        throw new IllegalStateException(
                                "oid.getIdentity() is null after create!");
View Full Code Here

        Iterator it = objectsMarkedForUpdate.iterator();
        while (it.hasNext()) {
            Object toCacheUpdate = it.next();
            if (_tracker.isCreated(toCacheUpdate)) {
                OID toCacheUpdateOID = _tracker.getOIDForObject(toCacheUpdate);
                LockEngine toCacheUpdateLocker =
                    _tracker.getMolderForObject(toCacheUpdate).getLockEngine();
                toCacheUpdateLocker.updateCache(this, toCacheUpdateOID, toCacheUpdate);
                _tracker.unmarkUpdateCacheNeeded(toCacheUpdate);
            }
        }
    }
View Full Code Here

    public final boolean markUpdate(
            final ClassMolder molder, final Object object, final OID depended)
    throws PersistenceException {
        if (object == null) { throw new NullPointerException(); }

        LockEngine engine = molder.getLockEngine();
       
        Identity identity = molder.getActualIdentity(this, object);
        if (molder.isDefaultIdentity(identity)) { identity = null; }

        OID oid = new OID(molder, depended, identity);

        // Check the object is in the transaction.
        Object foundInTransaction = _tracker.getObjectForOID(engine, oid, false);
        if (_autoStore && foundInTransaction != null
                && foundInTransaction == object) {
            return false;
        }

        if (foundInTransaction != null) {
            if (_tracker.isDeleted(foundInTransaction)) {
                throw new ObjectDeletedException(Messages.format(
                        "persist.objectDeleted", object.getClass(), identity));
            }
           
            throw new DuplicateIdentityException(
                    "update object which is already in the transaction");
        }

        try {
            _tracker.trackObject(molder, oid, object);
            if (engine.update(this, oid, object, null, 0)) {
                _tracker.markCreating(object);
                // yip: there is one issue here. Because object might be marked
                // to be created in the update process. However, we have no easy
                // way to fire jdoCreating() events from here.
View Full Code Here

            throw new ObjectNotPersistentException(Messages.format(
                    "persist.objectNotPersistent", object.getClass().getName()));
        }

        ClassMolder molder = _tracker.getMolderForObject(object);
        LockEngine engine = molder.getLockEngine();
        OID oid = _tracker.getOIDForObject(object);

        // Cannot delete same object twice
        if (_tracker.isDeleted(object)) {
            throw new ObjectDeletedException(Messages.format(
                    "persist.objectDeleted", object.getClass().getName(), oid
                            .getIdentity()));
        }

        try {
            if (_callback != null) {
                _callback.removing(object);
            } else if (molder.getCallback() != null) {
                molder.getCallback().removing(object);
            }
        } catch (Exception except) {
            throw new PersistenceException(Messages.format("persist.nested",
                    except));
        }

        // Must acquire a write lock on the object in order to delete it,
        // prevents object form being deleted while someone else is
        // looking at it.
        try {
            _tracker.markDeleted(object);
            engine.softLock(this, oid, _lockTimeout);
            // Mark object as deleted. This will prevent it from being viewed
            // in this transaction and will handle it properly at commit time.
            // The write lock will prevent it from being viewed in another
            // transaction.
            engine.markDelete(this, oid, object, _lockTimeout);

            try {
                if (_callback != null) {
                    _callback.removed(object);
                } else if (molder.getCallback() != null) {
View Full Code Here

        if (!_tracker.isTracking(object)) {
            throw new ObjectNotPersistentException(Messages.format(
                    "persist.objectNotPersistent", object.getClass().getName()));
        }

        LockEngine engine = _tracker.getMolderForObject(object).getLockEngine();
        OID oid = _tracker.getOIDForObject(object);

        if (_tracker.isDeleted(object)) {
            throw new ObjectDeletedException(Messages.format(
                    "persist.objectDeleted", object.getClass(), oid.getIdentity()));
        }

        try {

            engine.writeLock(this, oid, timeout);
        } catch (ObjectDeletedException except) {
            // Object has been deleted outside this transaction,
            // forget about it
            _tracker.untrackObject(object);
            throw new ObjectNotPersistentException(Messages.format(
View Full Code Here

TOP

Related Classes of org.exolab.castor.persist.LockEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.