Package org.exolab.castor.jdo

Examples of org.exolab.castor.jdo.PersistenceException


                            }
                        } else {
                            // fail-fast principle: if the object depend on another object,
                            // throw exception
                            if ( !tx.isDepended( oid, o ) )
                                throw new PersistenceException(
                                "Dependent object may not change its master. Object: "+o+" new master: "+oid);
                        }
                    } else if ( tx.isAutoStore() ) {
                        if ( !tx.isRecorded( o ) ) {
                            tx.markCreate( fieldEngine, fieldClassMolder, o, null );
                            if ( !_fhs[i].isStored() && fieldClassMolder._isKeyGenUsed ) {
                                updateCache = true;
                            }
                        }
                    }
                }
                break;

            case FieldMolder.ONE_TO_MANY:
                // create dependent objects if exists
                fieldClassMolder = _fhs[i].getFieldClassMolder();
                fieldEngine = _fhs[i].getFieldLockEngine();
                o = _fhs[i].getValue( object, tx.getClassLoader() );
                if ( o != null ) {
                    Iterator itor = getIterator( o );
                    while (itor.hasNext()) {
                        Object oo = itor.next();
                        if ( _fhs[i].isDependent() ) {
                            if ( !tx.isRecorded( oo ) ) {
                                //autoCreated = true;
                                tx.markCreate( fieldEngine, fieldClassMolder, oo, oid );
                                if ( fieldClassMolder._isKeyGenUsed )
                                    updateCache = true;
                            } else
                                // fail-fast principle: if the object depend on another object,
                                // throw exception
                                if ( !tx.isDepended( oid, oo ) )
                                    throw new PersistenceException("Dependent object may not change its master");
                        } else if ( tx.isAutoStore() ) {
                            if ( !tx.isRecorded( oo ) ) {
                                tx.markCreate( fieldEngine, fieldClassMolder, oo, null );
                                if ( fieldClassMolder._isKeyGenUsed )
                                    updateCache = true;
View Full Code Here


        Iterator itor;
        ArrayList orgFields;
        int fieldType;

        if ( oid.getIdentity() == null )
            throw new PersistenceException("The identity of the object to be stored is null");

        if ( !oid.getIdentity().equals( getIdentity( tx, object ) ) )
            throw new PersistenceException( Messages.format(
                "jdo.identityChanged", _name, oid.getIdentity(),
                getIdentity( tx, object ) ) );

        fields = (Object[]) locker.getObject( tx );

        if ( fields == null )
            throw new PersistenceException("Object, "+oid+",  isn't loaded in the persistence storage!");

        newfields = new Object[_fhs.length];
        boolean updateCache = false;
        boolean updatePersist = false;

        // iterate thru all the data object fields for modification
        for ( int i=0; i<newfields.length; i++ ) {
            fieldType = _fhs[i].getFieldType();
            switch (fieldType) {
            case FieldMolder.PRIMITIVE:
                fieldClassMolder = _fhs[i].getFieldClassMolder();
                fieldEngine = _fhs[i].getFieldLockEngine();
                value =  _fhs[i].getValue( object, tx.getClassLoader() );
                if ( !isEquals( fields[i], value ) ) {
                    if (_fhs[i].isReadonly()) {
                        _fhs[i].setValue( object, fields[i], tx.getClassLoader() );
                    } else {
                        if ( _fhs[i].isStored() /*&& _fhs[i].isCheckDirty()*/ )
                            updatePersist = true;
                        updateCache = true;
                    }
                }
                break;

            case FieldMolder.SERIALIZABLE:
                // deserialize byte[] into java object
                try {
                    byte[] bytes = (byte[]) fields[i];
                    Object fieldValue = _fhs[i].getValue( object, tx.getClassLoader() );
                    if ( fieldValue == null && bytes == null) {
                        // do nothing
                    } else if ( fieldValue == null || bytes == null ) {
                        // indicate store is needed
                        if ( _fhs[i].isStored() /*&& _fhs[i].isCheckDirty()*/ )
                            updatePersist = true;
                        updateCache = true;
                    } else { // both not null
                        // The following code can be updated, after Blob-->InputStream
                        // to enhance performance.
                        ByteArrayInputStream bis = new ByteArrayInputStream( bytes );
                        ObjectInputStream os = new ObjectInputStream( bis );
                        Object dependent = os.readObject();
                        if ( !dependent.equals( fieldValue ) ) {
                            if ( _fhs[i].isStored() && _fhs[i].isCheckDirty() )
                                updatePersist = true;
                            updateCache = true;
                        }
                    }
                } catch ( OptionalDataException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                } catch ( ClassNotFoundException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                } catch ( IOException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                }
                break;

            case FieldMolder.PERSISTANCECAPABLE:
                fieldClassMolder = _fhs[i].getFieldClassMolder();
                fieldEngine = _fhs[i].getFieldLockEngine();
                value = _fhs[i].getValue( object, tx.getClassLoader() );
                if ( value != null )
                    newfields[i] = fieldClassMolder.getIdentity( tx, value );

                // | yip: don't delete the following comment,
                //      until it proved working by time. :-P
                // if ids are the same or not canCreate
                //    if object is deleted
                //        warn
                //    if object are the same
                //        done
                //    not the same
                //        exception
                // ids not the same or canCreate
                //    if depend
                //       if old is not null
                //          delete old
                //          removeRelation
                //       if canCreate
                //          create new
                //    not depend and autoStore
                //       if old is not null
                //          removeRelation
                //       if canCreate
                //          createObject
                //    not depend nor autoStore
                //       if old is not null
                //          removeRelation
                //       if new is not null
                if ( isEquals( fields[i], newfields[i] ) ) {
                    if ( !_debug )
                        break;

                    if ( fields[i] == null )
                        break; // do the next field if both are null

                    if ( value != null && tx.isDeleted(value) ) {
                        _log.warn ("Deleted object found!");
                        if ( _fhs[i].isStored() && _fhs[i].isCheckDirty() )
                            updatePersist = true;
                        updateCache = true;
                        _fhs[i].setValue( object, null, tx.getClassLoader() );
                        break;
                    }

                    if ( tx.isAutoStore() || _fhs[i].isDependent() )
                        if ( value != tx.fetch( fieldEngine, fieldClassMolder, fields[i], null ) )
                            throw new DuplicateIdentityException("");
                } else {
                    if ( _fhs[i].isStored() && _fhs[i].isCheckDirty() )
                        updatePersist = true;
                    updateCache = true;

                    if ( _fhs[i].isDependent() ) {
                        if ( fields[i] != null ) {
                            Object reldel = tx.fetch( fieldEngine, fieldClassMolder, fields[i], null );
                            if ( reldel != null )
                                tx.delete( reldel );
                        }

                        if ( value != null && !tx.isRecorded( value ) ) {
                            tx.markCreate( fieldEngine, fieldClassMolder, value, oid );
                        }

                    } else if ( tx.isAutoStore() ) {
                        if ( fields[i] != null ) {
                            Object deref = tx.fetch( fieldEngine, fieldClassMolder, fields[i], null );
                            if ( deref != null )
                                fieldClassMolder.removeRelation( tx, deref, this, object );
                        }

                        if ( value != null && !tx.isRecorded( value ) )
                            tx.markCreate( fieldEngine, fieldClassMolder, value, null );
                    } else {
                        if ( fields[i] != null ) {
                            Object deref = tx.fetch( fieldEngine, fieldClassMolder, fields[i], null );
                            if ( deref != null )
                                fieldClassMolder.removeRelation( tx, deref, this, object );
                        }

                        // yip: user're pretty easily to run into cache
                        // integrity problem here, if user forgot to create
                        // "value" explicitly. Let's put error message here
                        if ( value != null && !tx.isRecorded( value ) )
                            throw new PersistenceException(
                            "Object, "+object+", links to another object, "+value
                            +" that is not loaded/updated/created in this transaction");
                    }
                }
                break;
View Full Code Here

        Object[] fields;
        Object value;
        int fieldType;

        if ( oid.getIdentity() == null )
            throw new PersistenceException("The identities of the object to be stored is null");

        if ( !oid.getIdentity().equals( getIdentity( tx, object ) ) )
            throw new PersistenceException("Identities changes is not allowed!");

        fields = (Object[]) locker.getObject( tx );

        if ( fields == null )
            throw new PersistenceException("Object, "+oid+",  isn't loaded in the persistence storage!");

        newfields = new Object[_fhs.length];
        for ( int i=0; i<newfields.length; i++ ) {
            fieldType = _fhs[i].getFieldType();
            switch (fieldType) {
            case FieldMolder.PRIMITIVE:
                newfields[i] = _fhs[i].getValue( object, tx.getClassLoader() );
                break;
            case FieldMolder.SERIALIZABLE:
                try {
                    Object dependent = _fhs[i].getValue( object, tx.getClassLoader() );
                    if ( dependent != null ) {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutputStream os = new ObjectOutputStream( bos );
                        os.writeObject( dependent );
                        newfields[i] = bos.toByteArray();
                    } else {
                        newfields[i] = null;
                    }
                } catch ( IOException e ) {
                    throw new PersistenceException( "Error during serializing dependent object", e );
                }
                break;

            case FieldMolder.PERSISTANCECAPABLE:
                if ( _fhs[i].isStored() ) {
View Full Code Here

        if ( objectTimestamp > 0 && oid.getIdentity() != null ) {
            // valid range of timestamp
           
            if ((_timeStampable) && (lockTimestamp == TimeStampable.NO_TIMESTAMP)) {
                _log.warn (Messages.format("persist.objectNotInCache", _name, oid.getIdentity()));
                throw new PersistenceException (Messages.format("persist.objectNotInCache", _name, oid.getIdentity()));
            }

            if ( _timeStampable && objectTimestamp != lockTimestamp )
                throw new ObjectModifiedException("Timestamp mismatched!");

            if ( !_timeStampable && isDependent() && fields == null  ) {
                // allow a dependent object not implements timeStampable
                fields = new Object[_fhs.length];
                Connection conn = (Connection)tx.getConnection(oid.getLockEngine());
                stamp = _persistence.load( conn, fields, oid.getIdentity(), accessMode );
                oid.setDbLock( accessMode == AccessMode.DbLocked );
                locker.setObject( tx, fields );
            }

            // load the original field into the transaction. so, store will
            // have something to compare later.
            try {
                for ( int i=0; i <_fhs.length; i++ ) {
                    fieldType = _fhs[i].getFieldType();
                    switch (fieldType) {
                    case FieldMolder.PRIMITIVE:
                        break;
                    case FieldMolder.SERIALIZABLE:
                        break;
                    case FieldMolder.PERSISTANCECAPABLE:
                        fieldClassMolder = _fhs[i].getFieldClassMolder();
                        fieldEngine = _fhs[i].getFieldLockEngine();
                        o = _fhs[i].getValue( object, tx.getClassLoader() );
                        if ( _fhs[i].isDependent() ) {
                            // depedent class won't have persistenceInfo in LockEngine
                            // must look at fieldMolder for it

                            if ( o != null && !tx.isRecorded(o) )
                                tx.markUpdate( fieldEngine, fieldClassMolder, o, oid );

                            // load the cached dependent object from the data store.
                            // The loaded will be compared with the new one
                            if ( fields[i] != null )
                                tx.load( fieldEngine, fieldClassMolder, fields[i], null, suggestedAccessMode );
                        } else if ( tx.isAutoStore() ) {
                            if ( o != null && !tx.isRecorded(o) )
                                tx.markUpdate( fieldEngine, fieldClassMolder, o, null );

                            if ( fields[i] != null )
                                tx.load( fieldEngine, fieldClassMolder, fields[i], null, suggestedAccessMode );
                        }
                        break;

                    case FieldMolder.ONE_TO_MANY:
                        fieldClassMolder = _fhs[i].getFieldClassMolder();
                        fieldEngine = _fhs[i].getFieldLockEngine();
                        if ( _fhs[i].isDependent() ) {
                            if ( !_fhs[i].isLazy() ) {
                                Iterator itor = getIterator( _fhs[i].getValue( object, tx.getClassLoader() ) );
                                ArrayList v = (ArrayList)fields[i];
                                ArrayList newSetOfIds = new ArrayList();

                                // iterate the collection of this data object field
                                while ( itor.hasNext() ) {
                                    Object element = itor.next();
                                    Object actualIdentity = fieldClassMolder.getActualIdentity( tx, element );
                                    newSetOfIds.add( actualIdentity );
                                    if ( v != null && v.contains( actualIdentity ) ) {
                                        if ( !tx.isRecorded( element ) )
                                            tx.markUpdate( fieldEngine, fieldClassMolder, element, oid );
                                    } else {
                                        /*
                                        if ( !tx.isRecorded( element ) )
                                            tx.markCreate( fieldEngine, fieldClassMolder, element, oid );
                                         */
                                    }
                                }
                                if ( v != null ) {
                                    for ( int j=0,l=v.size(); j<l; j++ ) {
                                        if ( !newSetOfIds.contains( v.get(j) ) ) {
                                            // load all the dependent object in cache for modification
                                            // check at commit time.
                                            tx.load( oid.getLockEngine(), fieldClassMolder, v.get(j), null, suggestedAccessMode );

                                        }
                                    }
                                }
                            } else {
                                // ArrayList avlist = (ArrayList) fields[i];
                                fieldClassMolder = _fhs[i].getFieldClassMolder();
                                fieldEngine = _fhs[i].getFieldLockEngine();
                                // RelationCollection relcol = new RelationCollection( tx, oid, fieldEngine, fieldClassMolder, accessMode, avlist );
                            }
                        } else if ( tx.isAutoStore() ) {
                            Iterator itor = getIterator( _fhs[i].getValue( object, tx.getClassLoader() ) );
                            ArrayList v = (ArrayList)fields[i];
                            ArrayList newSetOfIds = new ArrayList();

                            // iterate the collection of this data object field
                            while ( itor.hasNext() ) {
                                Object element = itor.next();
                                Object actualIdentity = fieldClassMolder.getActualIdentity( tx, element );
                                newSetOfIds.add( actualIdentity );
                                if ( v != null && v.contains( actualIdentity ) ) {
                                    if ( !tx.isRecorded( element ) )
                                        tx.markUpdate( fieldEngine, fieldClassMolder, element, null );
                                } else {
                                    if ( !tx.isRecorded( element ) )
                                        tx.markUpdate( fieldEngine, fieldClassMolder, element, null );
                                }
                            }
                            // load all old objects for comparison in the preStore state
                            if ( v != null ) {
                                for ( int j=0,l=v.size(); j<l; j++ ) {
                                    if ( !newSetOfIds.contains( v.get(j) ) ) {
                                        // load all the dependent object in cache for modification
                                        // check at commit time.
                                        tx.load( oid.getLockEngine(), fieldClassMolder, v.get(j), null, suggestedAccessMode );
                                    }
                                }
                            }
                        }
                        break;
                    case FieldMolder.MANY_TO_MANY:
                        fieldClassMolder = _fhs[i].getFieldClassMolder();
                        fieldEngine = _fhs[i].getFieldLockEngine();
                        if ( tx.isAutoStore() ) {
                            Iterator itor = getIterator( _fhs[i].getValue( object, tx.getClassLoader() ) );
                            ArrayList v = (ArrayList)fields[i];
                            ArrayList newSetOfIds = new ArrayList();

                            // iterate the collection of this data object field
                            while ( itor.hasNext() ) {
                                Object element = itor.next();
                                Object actualIdentity = fieldClassMolder.getActualIdentity( tx, element );
                                newSetOfIds.add( actualIdentity );
                                if ( v != null && v.contains( actualIdentity ) ) {
                                    if ( !tx.isRecorded( element ) )
                                        tx.markUpdate( fieldEngine, fieldClassMolder, element, null );
                                } else {
                                    if ( !tx.isRecorded( element ) )
                                        tx.markUpdate( fieldEngine, fieldClassMolder, element, null );
                                }
                            }
                            // load all old objects for comparison in the preStore state
                            if ( v != null ) {
                                for ( int j=0,l=v.size(); j<l; j++ ) {
                                    if ( !newSetOfIds.contains( v.get(j) ) ) {
                                        // load all the dependent object in cache for modification
                                        // check at commit time.
                                        tx.load( oid.getLockEngine(), fieldClassMolder, v.get(j), null, suggestedAccessMode );
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            } catch ( ObjectNotFoundException e ) {
                e.printStackTrace();
                throw new ObjectModifiedException("dependent object deleted concurrently");
            }
            return false;
        } else if ( objectTimestamp == TimeStampable.NO_TIMESTAMP || objectTimestamp == 1 ) {
            // work almost like create, except update the sub field instead of create
            // iterate all the fields and mark all the dependent object.
            boolean updateCache = false;

            for ( int i=0; i<_fhs.length; i++ ) {
                fieldType = _fhs[i].getFieldType();
                switch (fieldType) {
                case FieldMolder.PRIMITIVE:
                case FieldMolder.SERIALIZABLE:
                    // nothing need to be done here for primitive
                    break;

                case FieldMolder.PERSISTANCECAPABLE:
                    // create dependent object if exists
                    fieldClassMolder = _fhs[i].getFieldClassMolder();
                    fieldEngine = _fhs[i].getFieldLockEngine();
                    o = _fhs[i].getValue( object, tx.getClassLoader() );
                    if ( o != null ) {
                        if ( _fhs[i].isDependent() ) {
                            // creation of dependent object should be delayed to the
                            // preStore state.
                            // otherwise, in the case of keygenerator being used in both
                            // master and dependent object, and if an dependent
                            // object is replaced by another before commit, the
                            // orginial dependent object will not be removed.
                            //
                            // the only disadvantage for that appoarch is that an
                            // OQL Query will not able to include the newly generated
                            // dependent object.
                            if ( !tx.isRecorded( o ) ) {
                                tx.markCreate( fieldEngine, fieldClassMolder, o, oid );
                                if ( !_fhs[i].isStored() && fieldClassMolder._isKeyGenUsed ) {
                                    updateCache = true;
                                }
                            } else {}
                                // fail-fast principle: if the object depend on another object,
                                // throw exception
                                // if ( !tx.isDepended( oid, o ) )
                                //    throw new PersistenceException("Dependent object may not change its master. Object: "+o+" new master: "+oid);
                        } else if ( tx.isAutoStore() ) {
                            if ( !tx.isRecorded( o ) ) {
                                // related object should be created right the way, if autoStore
                                // is enabled, to obtain a database lock on the row. If both side
                                // uses keygenerator, the current object will be updated in the
                                // store state.
                                boolean creating = tx.markUpdate( fieldEngine, fieldClassMolder, o, null );
                                // if _fhs[i].isStore is true for this field,
                                // and if key generator is used
                                // and if the related object is replaced this object by null
                                // and if everything else is not modified
                                // then, objectModifiedException will be thrown
                                // there are two solutions, first introduce preCreate state,
                                // and walk the create graph, and create non-store object
                                // first. However, it doesn't guarantee solution. because
                                // every object may have field which uses key-generator
                                // second, we can do another SQLStatement at the very end of
                                // this method.
                                // note, one-many and many-many doesn't affected, because
                                // it is always non-store fields.
                                if ( creating && !_fhs[i].isStored() && fieldClassMolder._isKeyGenUsed ) {
                                    updateCache = true;
                                }
                            }
                        }
                    }
                    break;

                case FieldMolder.ONE_TO_MANY:
                    // create dependent objects if exists
                    fieldClassMolder = _fhs[i].getFieldClassMolder();
                    fieldEngine = _fhs[i].getFieldLockEngine();
                    o = _fhs[i].getValue( object, tx.getClassLoader() );
                    if ( o != null ) {
                        Iterator itor = getIterator( o );
                        while (itor.hasNext()) {
                            Object oo = itor.next();
                            if ( _fhs[i].isDependent() ) {
                                if ( !tx.isRecorded( oo ) ) {
                                    //autoCreated = true;
                                    tx.markCreate( fieldEngine, fieldClassMolder, oo, oid );
                                    if ( fieldClassMolder._isKeyGenUsed )
                                        updateCache = true;
                                } else
                                    // fail-fast principle: if the object depend on another object,
                                    // throw exception
                                    if ( !tx.isDepended( oid, oo ) )
                                        throw new PersistenceException("Dependent object may not change its master");
                            } else if ( tx.isAutoStore() ) {
                                if ( !tx.isRecorded( oo ) ) {
                                    boolean creating = tx.markUpdate( fieldEngine, fieldClassMolder, oo, null );
                                    if ( creating && fieldClassMolder._isKeyGenUsed )
                                        updateCache = true;
View Full Code Here

                        fieldClassMolder.removeRelation( tx, fobject, this, object );
                    }
                }
                break;
            default:
                throw new PersistenceException("Invalid field type!");
            }
        }
    }
View Full Code Here

        Object[] fields;
        Object value;
        int fieldType;

        if ( oid.getIdentity() == null )
            throw new PersistenceException("The identities of the object to be revert is null");

        fields = (Object[]) locker.getObject( tx );

        setIdentity( tx, object, oid.getIdentity() );

        for ( int i=0; i < _fhs.length; i++ ) {
            fieldType = _fhs[i].getFieldType();

            _log.debug( "Reverting field: " + ( ( FieldMolder ) _fhs[i] ).getName() );

            switch (fieldType) {
            case FieldMolder.PRIMITIVE:
                Object temp = fields[i];
                _fhs[i].setValue( object, temp, tx.getClassLoader() );
                break;
            case FieldMolder.SERIALIZABLE:
                // deserialize byte[] into java object
                try {
                    byte[] bytes = (byte[]) fields[i];
                    if ( bytes != null ) {
                        // The following code can be updated, after Blob-->InputStream
                        // to enhance performance.
                        ByteArrayInputStream bis = new ByteArrayInputStream( bytes );
                        ObjectInputStream os = new ObjectInputStream( bis );
                        Object o = os.readObject();
                        _fhs[i].setValue( object, o, tx.getClassLoader() );
                    } else {
                        _fhs[i].setValue( object, null, tx.getClassLoader() );
                    }
                } catch ( OptionalDataException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                } catch ( ClassNotFoundException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                } catch ( IOException e ) {
                    throw new PersistenceException( "Error while deserializing an dependent object", e );
                }
                break;
            case FieldMolder.PERSISTANCECAPABLE:

                fieldClassMolder = _fhs[i].getFieldClassMolder();
View Full Code Here

        if ( _ids.length > 1 ) {
            if ( identity instanceof Complex ) {
                Complex com = (Complex) identity;
                if ( com.size() != _ids.length )
                    throw new PersistenceException( "Complex size mismatched!" );

                for ( int i=0; i<_ids.length; i++ ) {
                    _ids[i].setValue( object, com.get(i), tx.getClassLoader() );
                }
            }
        } else {
            if ( identity instanceof Complex )
                throw new PersistenceException( "Complex type not accepted!" );
            _ids[0].setValue( object, identity, tx.getClassLoader() );
        }
    }
View Full Code Here

                _values[ i ] = null;
            }
            _stmt.execute();
            _rs = _stmt.getResultSet();
        } catch ( SQLException except ) {
            throw new PersistenceException( Messages.format( "persist.nested", except ) );
        }
    }
View Full Code Here

                _lastIdentity = SQLTypes.getObject( _rs, 1, _sqlTypes[ 0 ] );
            }
            return new Complex( _lastIdentity );
        } catch ( SQLException except ) {
            _lastIdentity = null;
            throw new PersistenceException( Messages.format( "persist.nested", except ) );
        }
    }
View Full Code Here

            if ( nextRow() )
                _lastIdentity = SQLTypes.getObject( _rs, 1, _sqlTypes[ 0 ] );
            else
                _lastIdentity = null;
        } catch ( SQLException except ) {
            throw new PersistenceException( Messages.format( "persist.nested", except ) );
        }
        return stamp;
    }
View Full Code Here

TOP

Related Classes of org.exolab.castor.jdo.PersistenceException

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.