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 Lance Eason <la...@whisperwire.com> on 2003/03/14 04:27:37 UTC

Transactional isolation at the object layer

I'm using 1.0 rc1 with the PB API.  I'm noticing that I don't have any isolation between multiple brokers when caching is enabled.  For example:
 
  Object[] pk = new Object[] {new Long(42)};
  Identity id = new Identity(Article.class, pk);
  
  PersistenceBroker broker1 = PersistenceBrokerFactory.defaultPersistenceBroker();
  broker.beginTransaction();

  Article a1 = (Article) broker.getObjectByQuery(new QueryByIdentity(id));
  a1.articleName = "My article";
  
  // start a second transaction
  PersistenceBroker broker2 = PersistenceBrokerFactory.defaultPersistenceBroker();
  broker2.beginTransaction();

  Article a2 = (Article) broker.getObjectByQuery(new QueryByIdentity(id));
  // a2 is another reference to the same Article as a1
  a2.unit = "kg";

  broker2.abortTransaction();
  
  broker.store(a1, ObjectModificationDefaultImpl.UPDATE);
  broker.commitTransaction();
  
  broker2.close();   
  broker.close();
 
The changes that I made on the aborted broker2 transaction end up getting persisted on the broker1 transaction because they're sharing the same object reference.  Similarly changes made on other transactions are visible even before they're committed.  In general it doesn't look like I have any transactional isolation if caching is turned on.  I guess my gut expectation was that caches would be broker specific and there would be communication between caches to coordinate invalidations on updates and deletes.
 
So how do other people deal with this?  I had thought maybe I could manage two jdbc-connection-descriptors pointing to the same database, one with a cache used only for reads and another with caching turned off used for updates but it looks like the caching policy is global not on a per descriptor basis.  Am I missing something obvious?

Re: Transactional isolation at the object layer

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

By default OJB uses one large global cache.
To achieve proper isolation you have to tell OJB to use one cache per 
Broker:
In OJB.properties you have to configure to use
org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl
as objectcache implementation

cheers,
Thomas

Lance Eason wrote:
> I'm using 1.0 rc1 with the PB API.  I'm noticing that I don't have any isolation between multiple brokers when caching is enabled.  For example:
>  
>   Object[] pk = new Object[] {new Long(42)};
>   Identity id = new Identity(Article.class, pk);
>   
>   PersistenceBroker broker1 = PersistenceBrokerFactory.defaultPersistenceBroker();
>   broker.beginTransaction();
> 
>   Article a1 = (Article) broker.getObjectByQuery(new QueryByIdentity(id));
>   a1.articleName = "My article";
>   
>   // start a second transaction
>   PersistenceBroker broker2 = PersistenceBrokerFactory.defaultPersistenceBroker();
>   broker2.beginTransaction();
> 
>   Article a2 = (Article) broker.getObjectByQuery(new QueryByIdentity(id));
>   // a2 is another reference to the same Article as a1
>   a2.unit = "kg";
> 
>   broker2.abortTransaction();
>   
>   broker.store(a1, ObjectModificationDefaultImpl.UPDATE);
>   broker.commitTransaction();
>   
>   broker2.close();   
>   broker.close();
>  
> The changes that I made on the aborted broker2 transaction end up getting persisted on the broker1 transaction because they're sharing the same object reference.  Similarly changes made on other transactions are visible even before they're committed.  In general it doesn't look like I have any transactional isolation if caching is turned on.  I guess my gut expectation was that caches would be broker specific and there would be communication between caches to coordinate invalidations on updates and deletes.
>  
> So how do other people deal with this?  I had thought maybe I could manage two jdbc-connection-descriptors pointing to the same database, one with a cache used only for reads and another with caching turned off used for updates but it looks like the caching policy is global not on a per descriptor basis.  Am I missing something obvious?
>