Results 1 to 7 of 7
  1. #1
    sajdutt is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default Object Comparison!

    Ok guys,

    I have a problem and I don't know what causes it.

    In one of the classes, I used a statement like:

    if(a.getClientId() == b.getClientId()){...}

    getClientId returns ID object which implements Comparable, Cloneable, Serializable.

    This returns true when I run it from my local resin server. Now, if I link my server to another server by means of synchronization, the above statement returns false. Of course, if I used "equals" instead of "==", there would have been no problem. ..and I know the rules of Object comparison. My question is if the above statement works in a standalone server, then why it doesn't when the two servers are synced? Again, using "equals" solves my problem but that's not what I wanna know. Any idea?


    How it is synced:

    I have two machines (Apple and Orange). Both using resin servers.

    I modified a portion of my web.xml:

    Apple's web.xml file:

    <init-param Apple-peer='Apple Orange'/> <!-- Before it was:<init-param Apple-peer='Apple'/> -->
    <init-param Apple-db='Apple/SERVER'/>
    <init-param Apple-db-connections='5'/>

    Orange's web.xml file:

    <init-param Orange-peer='Orange Apple'/> <!-- Before it was:<init-param Orange-peer='Orange'/> -->
    <init-param Orange-db='Orange/SERVER'/>
    <init-param Orange-db-connections='5'/>

    Now, two machines have to be started simultaneously, otherwise, one will wait for the other one to start.

    Thank a lot in advance.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    take a look at this thread..
    freedom exists in the world of ideas

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Don't use == unless you really want to compare pointer values. Implement and use .equals().
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Hi tbondcom8,

    Please don't put links here other than on English. Our community based on English and most of the people can't read your posts. Please pay attention on this. :)


  5. #5
    sajdutt is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    I know it very well when to use "==" and when to use "equals". I know what JVM does before creating a new object. The Heap and all. That's not what I am looking for as an answer.

    It works when the servers are not synced. How come?
    Last edited by sajdutt; 05-22-2008 at 11:41 AM.

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    How can I answer your question without looking at the code that created the ID instances? Maybe when the servers are not synced the IDs are reused when they mean the same thing. Maybe when you sync your servers one of the instances (or both) of ID come from a communication layer where objects are instantiated every time you sync something (do you use RMI to send parameters across?). No matter what the cause of == working is in the end it is just plain wrong to use it. If you want non generic answers show more code.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  7. #7
    sajdutt is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your replies Daniel.

    It's just that the coding for adding the user and the comparison process involves several Java classes and JSP pages. Let me go through the steps.

    It's a huge Financial System where you can add a Trader (a new user), include him with other employees who belong to the same company who are already registered in the system.

    When you add an user, the JSP calls a certain Java class (Cache.java) to do that which also involves other java classes to complete the task. Cache class assigns the id (an object - ID class) of the company to the newly created user.

    When we make a Trade (A contract between two traders), we search for his colleagues( Other traders/brokers from his company).

    //Get the cache
    ICache cache = (ICache) session.getAttribute("cache");

    //Get the newly created trader
    IUser trader = cache.getUser(userName);

    //get the colleagues. It's in a loop of Traders
    IUser ccUser = (IUser) cache.getUser((ID) iter.next());

    //Compare the traders to see if they belong to the same company
    if (toIncludeUser.getClientId() == mainUser.getClientId()) {...}

    The comparison above returns true when the Trader is not a newly created trader. And also it returns true when servers are not synced. Moreover, it returns true if the server is the elected server.

    Elected Server: When multiple servers are synced, Cache checks what server is elected. The election is done randomly. When we add a new user, we first add that user in the elected server. Then we add it in the other servers.
    Last edited by sajdutt; 05-23-2008 at 02:54 PM.

Similar Threads

  1. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 05:47 AM
  2. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 03:46 AM
  3. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 07:34 PM
  4. Parsing Dates for Comparison
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-19-2007, 06:50 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •