0

Currently i am learning concurrency in java.

below is my doubt which i trying to explain here--

here i am fetching two object from datatase using their id.(using spring boot +hibernate)

object object1=getObjectFromDataBaseUsingId(id1);
object object2=getObjectFromDataBaseUsingId(id2);
  1. are both objects referencing the same object in heap if both id are same??
  2. in which case they will be always referencing the same object in heap.

when i checked it using some getter and setter method as explained below,i got that changes made in one object reflecting to other object.

//print any property of object2 
print(object2.getSomething());

//change that property of object1
object1.setSomething(some value);

//again print that property of object2
print(object2.getSomething());
  1. what if we are fetching these objects in different-different thread ??. will same things apply here that changes made in one thread to this object will always reflect to other objects in different threads.

(please explain 4th doubt in more details and suggest some article about it)

3
  • 2
    Please read: Can I ask only one question per post? Commented Aug 26, 2020 at 19:36
  • @Turing85 my all doubts are related to each other so i asked in this way Commented Aug 26, 2020 at 19:41
  • 1
    This isn’t about concurrency I think. Read about hibernate first level cache. Commented Aug 26, 2020 at 20:18

1 Answer 1

1

Hibernate performs a kind of "uniquing". In one Hibernate session, each database record corresponding to an entity class will be represented by the same instance when you ask Hibernate to retrieve that object for you. This is the Hibernate first level cache. This is true as long as the session is alive until you explicitly remove the object from the session, for example calling session.detach(object) or session.clear().

For point 3., you additionally need to take the usual Java memory model considerations into account. You should read about memory barriers there, namely Java's happens-before conditions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.