You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by James Nyika <ja...@novell.com> on 2003/05/25 18:14:20 UTC

Re: Serialized objects returning to the database

Hi 

 I am working with OJB 1.0 rc 2 and i have objects that i read out of
the database, and then serialize over a message queue to waiting threads
that do some processing. However, once the processing is done, i would
like to simply take these objects and save them back to the database
instead of reading the object out (hopefully its in the cache still) ,
copying over the attributes then saving.

  Part of the reason i do not want to do this is so that i can
generically handle a number of different objects this way. 

  One of the things that works for now is a method i added to my class
that handles my broker instance called a merge:

     it looks like so:

   /**
	 * Method merge.
	 * This method is used to merge back into the cache all objects
that may have been serialized out and only need updating.
	 * Note.. the merge is probably not a full traversal of the tree
... so do not assume that it is. 
	 *  Will later implement that way.
	 * @param broker
	 * @param objectToMerge
	 */
	public void merge(PersistenceBroker broker, Object
objectToMerge)
	{
		
		//replaces the identity in the cache for this object
with the updated one
				
		broker.serviceObjectCache().cache(new
Identity(objectToMerge,broker),objectToMerge);
	}

 As you can see, this basically creates a new identity for my updated
instance and caches it. However, this does not release the instance that
may already exist in the cache with similar data. I know this is very
dangerous but it meets my needs for now till i can actually find a way
to do a replacement of the cached reference instead of creating a new
one. 

  My question is :
      1. how do i look up the Identity object for anything in the cache
if i have a reference to that object ? ie.. how do i say something like

 Cache.giveMeIdentityFor(ObjectReadFromDB). 

All the methods i see require that you have the Identity object in
order manipulate the object, but i see no methods for doing things in
any other way.

      2. Any suggestions or ideas on a better way to 'merge' in changes
(in a generic way for any object) that may have come in through
serialization but is not referenced in the cache ?

 

James Nyika
Cambridge Technology Partners
The global eServices company of Novell, Inc.
http://www.novell.com

Re: Serialized objects returning to the database

Posted by Thomas Mahler <th...@web.de>.
Hi James,

James Nyika wrote:
> Hi 
> 
>  I am working with OJB 1.0 rc 2 and i have objects that i read out of
> the database, and then serialize over a message queue to waiting threads
> that do some processing. However, once the processing is done, i would
> like to simply take these objects and save them back to the database
> instead of reading the object out (hopefully its in the cache still) ,
> copying over the attributes then saving.
> 
>   Part of the reason i do not want to do this is so that i can
> generically handle a number of different objects this way. 
> 
>   One of the things that works for now is a method i added to my class
> that handles my broker instance called a merge:
> 
>      it looks like so:
> 
>    /**
> 	 * Method merge.
> 	 * This method is used to merge back into the cache all objects
> that may have been serialized out and only need updating.
> 	 * Note.. the merge is probably not a full traversal of the tree
> ... so do not assume that it is. 
> 	 *  Will later implement that way.
> 	 * @param broker
> 	 * @param objectToMerge
> 	 */
> 	public void merge(PersistenceBroker broker, Object
> objectToMerge)
> 	{
> 		
> 		//replaces the identity in the cache for this object
> with the updated one
> 				
> 		broker.serviceObjectCache().cache(new
> Identity(objectToMerge,broker),objectToMerge);
> 	}
> 
>  As you can see, this basically creates a new identity for my updated
> instance and caches it. However, this does not release the instance that
> may already exist in the cache with similar data. I know this is very
> dangerous but it meets my needs for now till i can actually find a way
> to do a replacement of the cached reference instead of creating a new
> one. 

Before placin the merged Object into the cache you could remove the old 
one by calling
broker.serviceObjectCache().remove(new Identity(objectToMerge,broker));

> 
>   My question is :
>       1. how do i look up the Identity object for anything in the cache
> if i have a reference to that object ? ie.. how do i say something like
> 
>  Cache.giveMeIdentityFor(ObjectReadFromDB). 

There is no such API. Two different Identities A and B, referring to the 
same Object, have the same hashcode and (A.equals(B) == true).

We did it this way, because there is no easy way to ensure that at one 
point in time for a given Object there is only one and the same Identity 
object in use. (Identies could be transferred between JVM etc.)
But OJB has to ensure that we provide the same "Uniqueness" as provided 
by the RDBMS.

So the cache will treat A and B as one and the same Identity!
Writing a little testcase that proves this behaviour should be fairly easy.
So actually the above mentioned call remove(...) is not required at all.

cheers,
Thomas

> All the methods i see require that you have the Identity object in
> order manipulate the object, but i see no methods for doing things in
> any other way.
> 
>       2. Any suggestions or ideas on a better way to 'merge' in changes
> (in a generic way for any object) that may have come in through
> serialization but is not referenced in the cache ?
> 
>  
> 
> James Nyika
> Cambridge Technology Partners
> The global eServices company of Novell, Inc.
> http://www.novell.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
>