Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Find - returns detached or cached?(Recommended Practise: Using a find() for detached or shared cache instance?)
Find - returns detached or cached? [message #801744] Sat, 18 February 2012 22:11 Go to next message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
A query can be programmed to returned a cached instance of an entity:

// This hint ensure that the shared instance be returned from cache.
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);

Is there the same control for a "find()"?

Does a find (by default) return a detached instance or reference to shared cache if an entity is cacheable?


Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com

[Updated on: Sat, 18 February 2012 22:13]

Report message to a moderator

Re: Find - returns detached or cached? [message #804383 is a reply to message #801744] Wed, 22 February 2012 15:10 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

By default find returns the object from the persistence context. To use find in a read-only fashion, call the find that takes a properties Map and pass the read-only hint.

You could also just execute a query that queries by id.


James : Wiki : Book : Blog : Twitter
Re: Find - returns detached or cached? [message #804414 is a reply to message #804383] Wed, 22 February 2012 15:46 Go to previous messageGo to next message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
Is a return from the "persistence context" a detached instance? (Said another way, when the term 'detached instance' is used does that mean detached from Level 2 Shared Cache? And is the term 'persistence context' synonymous with Level 1 cache?)


Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com
Re: Find - returns detached or cached? [message #808350 is a reply to message #804414] Mon, 27 February 2012 17:34 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

> And is the term 'persistence context' synonymous with Level 1 cache?

Basically yes. The managed object in the current EntityManager.

Detached means an object that is not in the persistence context, normally an object not in the L1 or L2 cache.



James : Wiki : Book : Blog : Twitter
Re: Find - returns detached or cached? [message #808369 is a reply to message #808350] Mon, 27 February 2012 18:05 Go to previous messageGo to next message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
I'm sorry, I'm having trouble parsing the above. If an instance that is "detached" is not in the L1 or L2 cache... and an instance that is "cached" is in Level 1 AND Level 2 cache... what is an instance that is only in L1 cache? (Is it considered "cached" or "detached" or "managed"???)

If there are three unique states... cached/detached/managed... it is unclear to me the 'recommended practise' for an update given that our queries are all read-only.

Said antoher way, following a read-only query... is it necessary to update a "detached" instance or simply a "managed"???



Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com
Re: Find - returns detached or cached? [message #808388 is a reply to message #808369] Mon, 27 February 2012 18:33 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

An object that is in the L1 cache (i.e. the EntityManager's persistence context) is a managed object.

You should only update managed objects (objects registered in the EntityManager).


James : Wiki : Book : Blog : Twitter
Re: Find - returns detached or cached? [message #809651 is a reply to message #808388] Wed, 29 February 2012 04:45 Go to previous messageGo to next message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
Let's go back to the starting point. If a read-only query is executed are the results in both L1 and L2 cache? That is, are the instances considered "cached" - meaning both L2 and L1?

We have ~6 GB of data we want in L2 cache only (in the form of a List<DebtorQO>)... so that it can be shared by multiple requests. How do we accomplish this? We do not have sufficient memory for every request (that executes the query - and refreshes L2 cache) to have it's own L1 set of instances.

/**
* This class is a Query Object for performance optimization.
*/
@Entity
@ReadOnly
@Cacheable(true)
@Cache(size = 60000000, alwaysRefresh = true, refreshOnlyIfNewer = true)
@Table(name = "DEBTOR")
public class DebtorQO extends TopBaseUuidNoAuditBO {

private static final long serialVersionUID = 1L;
@Column(name = "DEBTOR_TIN_NR")
private int debtorTin;

@Column(name = "UUID_KEY")
@Id
private byte[] debtorUuid;

/**
* Constructor
*/
public DebtorQO() {
super();

return;
}

/**
* Constructor
*
* @param debtorUuid
* @param debtorTin
* @param updateTime
*
*/
public DebtorQO(final byte[] debtorUuid, final int debtorTin,
final Timestamp updateTime) {

this.debtorUuid = debtorUuid;
this.debtorTin = debtorTin;
this.updateTime = updateTime;

return;
}

... Accessor methods here...

And the query:

<named-query name="QueryDebtorTinByStatus">

<query>
SELECT new gov.treas.fms.top.domain.entity.debt.DebtorQO(debtor.uuid, debtor.debtorTin, debtor.updateTime)
FROM DebtorBO debtor
WHERE debtor.debtorStatus = :debtorStatus

</query>

</named-query>


Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com

[Updated on: Wed, 29 February 2012 13:06]

Report message to a moderator

Re: Find - returns detached or cached? [message #810808 is a reply to message #809651] Thu, 01 March 2012 15:33 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Any read-only object, or read-only query will only be in the L2 cache, not the L1 cache.

In your query you are not return Entity objects, but using a DebtorQO constructor, so these objects will not be in any cache, as they are not persistent objects.


James : Wiki : Book : Blog : Twitter
Re: Find - returns detached or cached? [message #815007 is a reply to message #810808] Wed, 07 March 2012 05:31 Go to previous message
Marvin Toll is currently offline Marvin TollFriend
Messages: 34
Registered: July 2009
Member
As follow-up... here is the API we are implementing in SOAj to clarify the state of L1 and L2 cache:

• containsInstanceInPersistenceContext()
• containsInstanceInSharedCache()
• clearAllInstancesFromPersistenceContext()
• detachInstanceFromPersistenceContext()
• evictSingleInstanceFromSharedCache()
• evictInstancesFromSharedCache()
• evictAllInstancesFromSharedCache()


Marvin Toll
CTO, Pattern Enabled Development
http://pedCentral.com
Previous Topic:JPA and Temporal Databases
Next Topic:Dynamic JPA: How to handle auto generated primary key in Dynamic Type?
Goto Forum:
  


Current Time: Thu Mar 28 18:26:16 GMT 2024

Powered by FUDForum. Page generated in 0.04570 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top