Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Maintaining object identity across sessions
Maintaining object identity across sessions [message #800938] Fri, 17 February 2012 18:14 Go to next message
Cristian Spiescu is currently offline Cristian SpiescuFriend
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 #800953 is a reply to message #800938] Fri, 17 February 2012 18:44 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This is the way JPA is designed. Each EntityManager context represents its own transaction and must issolate changes from each other context. The only way to do that is to give you a different object to modify. Eclipselink caching is described here: http://wiki.eclipse.org/Introduction_to_Cache_%28ELUG%29
with an EntityManager wrapping/representing the UnitOfWork. JPA gets around this by allowing you to merge unmanaged instances into the current context to pick up changes. This prevents other threads from seeing uncommited data, and allows for rollbacks and refreshes to wipe out the current changes without affecting other threads views.

Why do you need the same object?
Re: Maintaining object identity across sessions [message #801098 is a reply to message #800953] Fri, 17 February 2012 22:55 Go to previous message
Cristian Spiescu is currently offline Cristian SpiescuFriend
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.
Previous Topic:Issue with deployment of EJB project
Next Topic:Weaver Throws ClassNotFoundException For Available Classes
Goto Forum:
  


Current Time: Sat Apr 27 00:34:00 GMT 2024

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

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

Back to the top