You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Bruce Beaumont <br...@ucs-solutions.co.za> on 2007/06/14 15:46:54 UTC

OpenJPS in standalone environment

Hi

I have a simple (standalone) app connecting to a database and reading a row of 
data from a table using em.fetch() in a loop. The data being returned is 
being cached as it does not refelect changes to the database performed by a 
3rd party. i.e only the first fetch gets the correct data subsequent 
em.fetch() calls return the cached value (SQL=TRACE does not show any 
executed sql)

I have disabled the datacache using
<property name="openjpa.DataCache" value="false"/>
<property name="openjpa.QueryCache" value="false"/>

and tried silly things like
<property name="openjpa.DataCache" value="true(CacheSize=0, 
SoftReferenceSize=0)"/>

Is there a way of bypassing the cache similar to toplink's 
q.setHint("toplink.cache-usage", "DoNotCheckCache");







Re: OpenJPS in standalone environment

Posted by Patrick Linskey <pl...@gmail.com>.
> Most of the data in this system comes form external sources and the
> system MUST pick up the latest changes so using a new em each
> time would not work well.

What do you mean by this? If your transactions are relatively short,
then you can simply get a new EM for each transaction, which will
avoid any EM-level caching. Are your transactions particularly
long-lived? Are you saying that if data in the database changes during
the course of the transaction, you want to simply pick up the changed
values, but not fail the active transaction? Often, when people are
concerned about data integrity issues, they see the transaction as the
atom in the system, and feel that data reads within the transaction
should be consistent with each other. Is that not the case in your
situation?

Also, if you are using optimistic locking throughout your system, you
can obtain an optimistic read lock (using the EM.lock() API) to ensure
that data that you read is still current when the transaction commits.

-Patrick

On 6/15/07, Marc Prud'hommeaux <mp...@apache.org> wrote:
> Bruce-
>
> System #1 appears faster (since it reduces the possibility of 2
> identical queries being issued), but note that it does rely on the
> non-standard OpenJPA-specific findCached() method. Both look like
> they will work, though.
>
> Note that if you *really* want to be 100% up-to-date, you can always
> lock using pessimistic locking, so that exclusive locks are obtained
> when you read the objects. This can have serious performance
> implications, but it is really the only way to be sure that at any
> given time, the absolute most recent data is being reflected in the
> state of the instances. See:
>
>
> http://java.sun.com/javaee/5/docs/api/javax/persistence/
> EntityManager.html#lock(java.lang.Object,%
> 20javax.persistence.LockModeType)
> http://openjpa.apache.org/docs/latest/javadoc/org/apache/openjpa/
> persistence/OpenJPAEntityManager.html#lock(java.lang.Object,%
> 20javax.persistence.LockModeType,%20int)
>
>
>
> On Jun 15, 2007, at 4:50 AM, Bruce Beaumont wrote:
>
> > Hi
> >
> > Hi
> >
> > Most of the data in this system comes form external sources and the
> > system
> > MUST pick up the latest changes so using a new em each time would
> > not work
> > well.
> >
> > The problem with em.refresh() is that on first reads of a row you
> > generate 2
> > database transactions and some of the databases are remote over a WAN.
> >
> > I have come up with 2 fudges that seem to work and was wondering if
> > anyone had
> > any comments on which is better (Or another way totally) :
> >
> > First cast em to EntityManagerImpl then
> >
> > System 1:
> >
> > T ret = this.em.findCached(this.clazz, id);
> >
> > if (ret == null) {
> >     ret = this.em.find(this.clazz, id);
> > } else {
> >     this.em.refresh(ret);
> > }
> >
> >
> > Alternatively system 2:
> >
> >
> > T ret = this.em.find(this.clazz, id);
> >
> > if (ret != null) {
> >    this.em.evict(ret);
> > }
> >
> >
> > Bruce
> >
> > On Thursday 14 June 2007 18:51:04 Patrick Linskey wrote:
> >> Hi,
> >>
> >> Are you using the same entity manager for each call? In normal JPA
> >> usage, a given record does not automatically refresh within the
> >> context of a given EM and transaction.
> >>
> >> You can manually force fresh data to be read with the em.refresh()
> >> call.
> >>
> >> -Patrick
> >>
> >> On 6/14/07, Bruce Beaumont <br...@ucs-solutions.co.za>
> >> wrote:
> >>> Hi
> >>>
> >>> I have a simple (standalone) app connecting to a database and
> >>> reading a
> >>> row of data from a table using em.fetch() in a loop. The data being
> >>> returned is being cached as it does not refelect changes to the
> >>> database
> >>> performed by a 3rd party. i.e only the first fetch gets the
> >>> correct data
> >>> subsequent em.fetch() calls return the cached value (SQL=TRACE
> >>> does not
> >>> show any executed sql)
> >>>
> >>> I have disabled the datacache using
> >>> <property name="openjpa.DataCache" value="false"/>
> >>> <property name="openjpa.QueryCache" value="false"/>
> >>>
> >>> and tried silly things like
> >>> <property name="openjpa.DataCache" value="true(CacheSize=0,
> >>> SoftReferenceSize=0)"/>
> >>>
> >>> Is there a way of bypassing the cache similar to toplink's
> >>> q.setHint("toplink.cache-usage", "DoNotCheckCache");
> >
> >
>
>


-- 
Patrick Linskey
202 669 5907

Re: OpenJPS in standalone environment

Posted by Marc Prud'hommeaux <mp...@apache.org>.
Bruce-

System #1 appears faster (since it reduces the possibility of 2  
identical queries being issued), but note that it does rely on the  
non-standard OpenJPA-specific findCached() method. Both look like  
they will work, though.

Note that if you *really* want to be 100% up-to-date, you can always  
lock using pessimistic locking, so that exclusive locks are obtained  
when you read the objects. This can have serious performance  
implications, but it is really the only way to be sure that at any  
given time, the absolute most recent data is being reflected in the  
state of the instances. See:


http://java.sun.com/javaee/5/docs/api/javax/persistence/ 
EntityManager.html#lock(java.lang.Object,% 
20javax.persistence.LockModeType)
http://openjpa.apache.org/docs/latest/javadoc/org/apache/openjpa/ 
persistence/OpenJPAEntityManager.html#lock(java.lang.Object,% 
20javax.persistence.LockModeType,%20int)



On Jun 15, 2007, at 4:50 AM, Bruce Beaumont wrote:

> Hi
>
> Hi
>
> Most of the data in this system comes form external sources and the  
> system
> MUST pick up the latest changes so using a new em each time would  
> not work
> well.
>
> The problem with em.refresh() is that on first reads of a row you  
> generate 2
> database transactions and some of the databases are remote over a WAN.
>
> I have come up with 2 fudges that seem to work and was wondering if  
> anyone had
> any comments on which is better (Or another way totally) :
>
> First cast em to EntityManagerImpl then
>
> System 1:
>
> T ret = this.em.findCached(this.clazz, id);
>
> if (ret == null) {
>     ret = this.em.find(this.clazz, id);
> } else {
>     this.em.refresh(ret);
> }
>
>
> Alternatively system 2:
>
>
> T ret = this.em.find(this.clazz, id);
>
> if (ret != null) {
>    this.em.evict(ret);
> }
>
>
> Bruce
>
> On Thursday 14 June 2007 18:51:04 Patrick Linskey wrote:
>> Hi,
>>
>> Are you using the same entity manager for each call? In normal JPA
>> usage, a given record does not automatically refresh within the
>> context of a given EM and transaction.
>>
>> You can manually force fresh data to be read with the em.refresh()  
>> call.
>>
>> -Patrick
>>
>> On 6/14/07, Bruce Beaumont <br...@ucs-solutions.co.za>  
>> wrote:
>>> Hi
>>>
>>> I have a simple (standalone) app connecting to a database and  
>>> reading a
>>> row of data from a table using em.fetch() in a loop. The data being
>>> returned is being cached as it does not refelect changes to the  
>>> database
>>> performed by a 3rd party. i.e only the first fetch gets the  
>>> correct data
>>> subsequent em.fetch() calls return the cached value (SQL=TRACE  
>>> does not
>>> show any executed sql)
>>>
>>> I have disabled the datacache using
>>> <property name="openjpa.DataCache" value="false"/>
>>> <property name="openjpa.QueryCache" value="false"/>
>>>
>>> and tried silly things like
>>> <property name="openjpa.DataCache" value="true(CacheSize=0,
>>> SoftReferenceSize=0)"/>
>>>
>>> Is there a way of bypassing the cache similar to toplink's
>>> q.setHint("toplink.cache-usage", "DoNotCheckCache");
>
>


Re: OpenJPS in standalone environment

Posted by Bruce Beaumont <br...@ucs-solutions.co.za>.
Hi

Hi

Most of the data in this system comes form external sources and the system 
MUST pick up the latest changes so using a new em each time would not work 
well.

The problem with em.refresh() is that on first reads of a row you generate 2 
database transactions and some of the databases are remote over a WAN.

I have come up with 2 fudges that seem to work and was wondering if anyone had 
any comments on which is better (Or another way totally) :

First cast em to EntityManagerImpl then

System 1:

T ret = this.em.findCached(this.clazz, id);

if (ret == null) {
    ret = this.em.find(this.clazz, id);
} else {
    this.em.refresh(ret);
}


Alternatively system 2:

 
T ret = this.em.find(this.clazz, id);
    
if (ret != null) {
   this.em.evict(ret);
}


Bruce

On Thursday 14 June 2007 18:51:04 Patrick Linskey wrote:
> Hi,
>
> Are you using the same entity manager for each call? In normal JPA
> usage, a given record does not automatically refresh within the
> context of a given EM and transaction.
>
> You can manually force fresh data to be read with the em.refresh() call.
>
> -Patrick
>
> On 6/14/07, Bruce Beaumont <br...@ucs-solutions.co.za> wrote:
> > Hi
> >
> > I have a simple (standalone) app connecting to a database and reading a
> > row of data from a table using em.fetch() in a loop. The data being
> > returned is being cached as it does not refelect changes to the database
> > performed by a 3rd party. i.e only the first fetch gets the correct data
> > subsequent em.fetch() calls return the cached value (SQL=TRACE does not
> > show any executed sql)
> >
> > I have disabled the datacache using
> > <property name="openjpa.DataCache" value="false"/>
> > <property name="openjpa.QueryCache" value="false"/>
> >
> > and tried silly things like
> > <property name="openjpa.DataCache" value="true(CacheSize=0,
> > SoftReferenceSize=0)"/>
> >
> > Is there a way of bypassing the cache similar to toplink's
> > q.setHint("toplink.cache-usage", "DoNotCheckCache");



Re: OpenJPS in standalone environment

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

Are you using the same entity manager for each call? In normal JPA
usage, a given record does not automatically refresh within the
context of a given EM and transaction.

You can manually force fresh data to be read with the em.refresh() call.

-Patrick

On 6/14/07, Bruce Beaumont <br...@ucs-solutions.co.za> wrote:
> Hi
>
> I have a simple (standalone) app connecting to a database and reading a row of
> data from a table using em.fetch() in a loop. The data being returned is
> being cached as it does not refelect changes to the database performed by a
> 3rd party. i.e only the first fetch gets the correct data subsequent
> em.fetch() calls return the cached value (SQL=TRACE does not show any
> executed sql)
>
> I have disabled the datacache using
> <property name="openjpa.DataCache" value="false"/>
> <property name="openjpa.QueryCache" value="false"/>
>
> and tried silly things like
> <property name="openjpa.DataCache" value="true(CacheSize=0,
> SoftReferenceSize=0)"/>
>
> Is there a way of bypassing the cache similar to toplink's
> q.setHint("toplink.cache-usage", "DoNotCheckCache");
>
>
>
>
>
>
>


-- 
Patrick Linskey
202 669 5907