Maintaining object identity across sessions [message #800938] |
Fri, 17 February 2012 18:14 |
Cristian Spiescu Messages: 100 Registered: July 2009 |
Senior Member |
|
|
Hello,
I have read that Eclipselink, using it's cache mechanism that's enabled by default (of type SoftWeak, cf. http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Table_19-13) guarantees object identity.
However, I have a small test app, that is unsuccessful in showing how object identity is preserved.
I have the following code, that, in my understanding, should return true:
User user1 = dao.getUser(Long.valueOf(1));
User user2 = dao.getUser(Long.valueOf(1));
// compare instances
if (user1 == user2) {
System.out.println("User: same instance");
} else {
System.out.println("User: not the same instance");
}
dao.getUser() looks like this:
EntityManager em = EntityManagerFactoryUtil.getFactory().createEntityManager();
Map<String, Object> props = new HashMap<String, Object>();
User obj = em.find(User.class, id, props);
em.close();
However, the output I get is that the 2 objects are not the same instance.
I have noticed that if I add the following property when doing the request, then the object identity exists:
props.put("eclipselink.read-only", "true");
But the documentation says that the objects retrieved this way, shouldn't be modified.
Is there anything that I'm doing wrong? Or the only way to have object identity across sessions is to use "eclipselink.read-only"?
Thank you in advance.
Best regards,
Cristian.
|
|
|
|
Re: Maintaining object identity across sessions [message #801098 is a reply to message #800953] |
Fri, 17 February 2012 22:55 |
Cristian Spiescu Messages: 100 Registered: July 2009 |
Senior Member |
|
|
My app is a little bit atypical.
The main data of my application is not stored in a database. It is stored in files that are large and expensive to load. That's why, the server application loads the data and keeps it in the memory, saving it from time to time.
The users of the application, and the permissions (i.e. ACLs), are stored in a database. These entities are kept in the memory as well, because the application needs to know quickly (i.e. without interrogating the DB) which are the rights of a given client, for its current request.
When administrative operations are performed on these entites (i.e. CRUD), the classical web app flow is followed. E.g. getById(), modify, save(). At this point, the fact that the entities that I get from the DB are the same ones that exist in the application (within the ACL mechanism/data structures/algorithm), helps me, because I don't need to reinitialize them.
So am I correct when I say that if I want to achieve the above, the only way is to use the "eclipselink.read-only" property?
When doing this (i.e. retrieving read-only entities), the doc says that these instances should not be modified. I tried to do this (i.e. persist an entity retrieved as read-only), and it works. However I am wondering if there are other associated risks when doing this.
Thank you.
Best regards,
Cristian.
|
|
|
Powered by
FUDForum. Page generated in 0.03243 seconds