Results 1 to 3 of 3

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

  1. #1
    Join Date
    Apr 2008
    Posts
    8

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2008
    Posts
    8

    Default

    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.

Posting Permissions

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