views:

50

answers:

2

To be specific let me illustrate the question with Spring http-remoting example.

Suppose we have such implementation of a simple interface:

public SearchServiceImpl implements SearchService {
    public SearchJdo processSearch(SearchJdo search) {
        search.name = "a funky name";
        return search;
    }
}

SearchJdo is itself a simple POJO.

Now when we call the method from a client through http-remoting (Spring's mechanism of calling remote objects much like EJB that uses serialization) we'll get:

public class HTTPClient {
    public static void main(final String[] arguments) {
        final ApplicationContext context = new ClassPathXmlApplicationContext(
            "spring-http-client-config.xml");
        final SearchService searchService =
            (SearchService) context.getBean("searchService");

        SearchJdo search = new SearchJdo();
        search.name = "myName"; 
        // this method actually returns the same object it gets as an argument
        SearchJdo search2 = searchService.processSearch(search);
        System.out.println(search == search2); // prints "false"
    }
}

The problem is that the search objects are different because of serializaton although from logical prospective they are the same.

The question is whether there are some technique that allows to support or emulate object identity across VMs.

+1  A: 

IMHO attempting to preserve object identity equality across VMs is a losing proposition. To the best of my knowledge the language specification does not require a VM to support that, so you would be limited in where you can pull off if you truly want to be portable.

May I ask why you don't just use some unique ID that you supply yourself? Java GUIDs, while expensive, are serializable.

Uri
The approach you propose with GUIDs is fine. But in real world search jdo may have references to other objects which we also need to resolve via their GUIDs. Moreover every time a new reference is added to the search class the code that performs the GUID resolution must be changed accordingly. I would like to find a solution that will handle such things automatically.
wheleph
Use equals() and define it appropriately. There is not and cannot by definition be a solution that works for ==.
EJP
+1  A: 

You said it - object identity is different from logical equality.

  • object identity is compared with ==
  • logical equality is compared with .equals(..)

So override the equals() method and all will be fine. Remember to override hashCode() based on the same field(s) as well. Use your IDE to generate these 2 methods for you.

(Teracotta VM clustering allows sharing objects between VMs, but that doesn't fit your case.)

Bozho