0

I am using JPA with spring transactions. I have observed same session is alive till athread exits method1. I believe its happening becoz of @Transactional As per my understanding magic must be happening under the hood becoz of this

As soon as thread enters method1 , if any operation retrieves entityManager(parallel to session in hibernate) . spring proxy binds that entityManager/session(which internally binds to database connection object) with that thread so that down the line if any other operations asks for entityManager/session, spring will provide same entityManager/session. Is that correct?

@Transactional
method1(){}
emilly
  • 10,060
  • 33
  • 97
  • 172
  • check this out ... http://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background – Lalit Mehra Jul 12 '15 at 14:09
  • If you check your Spring/Hibernate config you should see some beans/config concerning a transaction manager. – Tobb Jul 12 '15 at 14:10
  • @lalit that link mainly focus on how proxy does that. But here I am curious to know does spring binds the session with thread so that down the line if any other operations asks for entityManager/session under that thread, same entityManager/sessionis returned ? – emilly Jul 12 '15 at 14:14
  • @Tobb yes I can see those configurations – emilly Jul 12 '15 at 14:15
  • Then, Spring adds an `aspect` around any public method annotated with `@Transactional`, which will run the necessary code to start a transaction. Any JPA operation run after this will be part of an atomic operation, and thus also part of the same session. – Tobb Jul 12 '15 at 14:17
  • @Tobb so aspect will bind session with thread internally right ? – emilly Jul 12 '15 at 14:59
  • An aspect is just a general mechanism, but in this case it will. – Tobb Jul 12 '15 at 17:11

0 Answers0