Announcement Announcement Module
Collapse
No announcement yet.
Basic Spring Transaction and Hibernate Question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Basic Spring Transaction and Hibernate Question

    Sorry if this is basic but I really want to understand this.

    I am using Spring 2.5.4 and Hibernate 3.2.6.

    Currently there is a service layer method that does the following simple steps:
    1. Call Persistence Service to retrieve DB value
    2. Increment DB Value
    3. Call Persistence Service to save updated value

    The methods in Step 1 & 3 are wrapped in their own Spring Transactions with propagation set to Required. The methods are retrieving a Hibernate Session from the Session Factory and calling the appropriate methods.

    The problem is that I was ending up with the same value being generate for multiple concurrent users. I though I could fix this by wrapping all three steps into a single Spring Transaction but this doesn't seem to work. Why? It appears that it is not thread safe. Do I need to synchronize the method that wraps these three steps?

  • #2
    It depends on your implementation and serialization level of the transaction. In general if you have correctly implemented a single transaction it shouldn't be a problem. So post some code and configuration.

    Comment


    • #3
      Unfortunately I can't post the code. I was hoping to get some sort of answer in theory. What I can say is that the service method and the persistence level calls are all wrapped with the following transaction definition:

      Code:
      @Transactional(readOnly=false, propagation=Propagation.Required)
      If this is not enough information to solve my particular problem than it would be great if somebody could explain how this should work if 2 different threads call the same top level method at the same time.

      From my debugging it appears that Spring allows both threads to be in the lop level method at the same time which I wasn't expecting since it is wrapped in a transaction. So I can step through the debugger with two different threads and have the first thread obtain the value from the DB and stop it while the next thread obtains the same value since the first thread didn't have a chance to persist the updated value. I don't want this to happen.

      Comment


      • #4
        I suggest a read on transactions, a transaction has nothing to do with access to a method it is all about transactional resources (in this case the database). It all depends on your isolation level, for which I suggest reading more on transactions.

        Comment


        • #5
          Thanks for your reply. I think you answered my question.

          Comment

          Working...
          X