Announcement Announcement Module
Collapse
No announcement yet.
Correct Propogation-Illegal attempt to associate a collection with two open sessions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Correct Propogation-Illegal attempt to associate a collection with two open sessions

    I know why I am getting that exception, but cant really get around it. Here is how my code looks

    Code:
    public class FooService
    {
         public void foo(){
           Bar bar= barDAO.getBar();
           bar.lock(true);
           barDAO.updateBar(bar);
           //Do some stuff  
               All in one transction
    
    
    
    
           //Some stuff ended
           bar.lock(false)
           barDAO.updateBar(bar);
        }
    }
    This gets called in a web application with OpenSessionInViewInterceptor.

    bar has associated collections to it. and updateBar's Propagation behavior has to be PROPOGATION_REQUIRES_NEW, because i want to lock the bar as soon as process is started and unlock it when its ended, everything in between must be in one transaction.

    I tried foo() -> PROPOGATION_REQUIRED
    getBar() --> PROPOGATION REQUIRED
    updateBar() --> PROPOGATION_REQUIRES_NEW
    It didnt work.

    The only time it worked was when i made foo() non transactional, the getBar() started a session and closed it afterwards.

    Experts, please let me know your suggestions

  • #2
    the foo method should be transactional, now you have multiple transactions which can leave your database in quite an undesirable state (what if the first update succeeds and the second fails? No way to rollback the first one).

    Make your foo method transactional and change your configuration to make that work (judging from your code you will need to enable classproxying as your service doesn't implement a interface).

    Comment


    • #3
      I want updateBar() has to be in a new transaction always. Because as soon as the process starts i lock the bar so any other process checks the lock before starting the process.

      All the stuff in between that goes in one transaction has exception handling, So the second updateBar() always gets called to unlock the bar.

      Comment

      Working...
      X