Hibernate Exception: Illegal attempt to associate a collection with two open sessions

That is the error that I have been trying to solve for the last two days and the solution turned out to be something very straight forward (and dare I say simple); the proper exception thrown was: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions.

Cause: It showed up when I attempt to update an element in a collection; adding to the collection was not a problem, hibernate mapping files are ok and everything else seemed to be in order but the error keeps turning up.

Solution: it turned out that I was preserving a value across requests whose presence cause a select statement to be executed before the update hence the exception.

Hmm … I can now get back to making some lightening fast progress.

About these ads
  1. #1 by Emad on May 19, 2008 - 1:21 pm

    I get the same exception, and I do not understand what u mean by “it turned out that I was preserving a value across requests whose presence cause a select statement to be executed before the update hence the exception.”

    can u give more explanation ..it is my second day in this one

  2. #2 by yuguuk on May 20, 2008 - 9:09 pm

    A little context about the exception mentioned: I was using Hibernate, Spring and JSF. I was trying to update the collection using a JSF managed bean that has a request scope. Note that the hibernate data access object is managed by Spring hence is not (in my setup) part of JSF life cycle.

    So with the above scenario, the first request attaches the collection to a hibernate session, subsequent attempts to modify the collection threw up the mentioned exception.

    Changing the scope of the JSF managed bean seems to solve the issue.

    I hope this helps.

  3. #3 by jack on November 6, 2008 - 2:31 am

    still missing the explanation. I can say I solved something without saying how… that’s too easy…

  4. #4 by Ibrahim on November 24, 2009 - 1:43 pm

    I am using NHibernate2.1… i got solved this issue when i use session.Merge() instead of session.update()/session.SaveorUpdate()

    hope this heps some one..

    ibrahim(SCPL)

  5. #5 by Alex Grigoriadis on December 3, 2009 - 2:45 pm

    Ibrahim, this solution saved me, using merge instead of saveOrUpdate. I use zk + hibernate. I managed to make it work only when using lazy-loading = false. However, when I tried to use lazy loading, many exceptions occured: either two sessions were open, or there was no session at all. I really don’t understand the matter in depth, it has to do with sessions, threads and requests, but it is beyond my understanding for the moment.

    Thanks anyway

  6. #6 by reda on December 17, 2010 - 3:47 pm

    thanks Ibrahim.

  7. #7 by hassanko on January 21, 2011 - 12:27 am

    Ibrahim : ys3dak ma arw3ak

  8. #8 by Chuck on January 30, 2011 - 11:27 pm

    I am using NHibernate2.1… i got solved this issue when i use session.Merge() instead of session.update()/session.SaveorUpdate()

    It works for me, thank you very much.

  9. #9 by Chuck on January 30, 2011 - 11:29 pm

    I am using NHibernate2.1… i got solved this issue when i use session.Merge() instead of session.update()/session.SaveorUpdate()

    It works for me,
    Thank you very much.

  10. #10 by Fernando on May 21, 2011 - 6:21 pm

    Hey thanks a lot! i got solved when i changed to session.Merge() instead of session.saveOrUpdate too!

  11. #11 by jaideep on January 5, 2012 - 11:24 pm

    Hi All,

    Hibernate Exception: Illegal attempt to associate a collection with two open sessions

    Plz help me as soon as possible.I am also getting same exception when i am using a filter to close the session.Previously i was closing session in every methods but currently i am closing session using filter.I am getting this exception any time when saveOrupdate method is called or not also.

  12. #12 by Someone on January 7, 2012 - 5:38 am

    Thanks Ibrahim!

  13. #13 by Sergio Curay on January 15, 2012 - 11:02 am

    Hey men! this is awesome! thanks a lot!

  14. #14 by Javier on March 5, 2012 - 6:06 pm

    You have to check what it the CascadeType of your Object, you have to merge the object previously to persist or delete it.

  15. #15 by edu on November 13, 2012 - 7:08 pm

    Thanks Ibrahim. finally solved!!

  16. #16 by @bb on January 18, 2013 - 10:13 am

    im impliment Tread local in hibernate session managment ………. i face this like error
    Illegal attempt to associate a collection with two open sessions
    Please Help to fix this Problem

  17. #17 by http://tinyurl.com/pausguest47484 on February 5, 2013 - 4:12 pm

    Thanks for applying free time in order to create “Hibernate Exception: Illegal attempt to associate a collection with two open sessions imma”.
    Many thanks again -Bonnie

  18. #18 by Abdul on April 17, 2013 - 12:42 pm

    Wow, wonderful blog layout! How long have you been blogging
    for? you make blogging look easy. The overall look of your
    website is excellent, let alone the content!

  19. #19 by lord on April 19, 2013 - 7:15 pm

    hi i don’t know how it work i have that problem in my class doa my class is
    public void ajouter(Universite univ) {
    Session session =HibernateUtil.getSessionFactory().openSession();
    try {

    session.beginTransaction();
    session.save(univ);
    session.beginTransaction().commit();
    } catch (Exception e) {
    System.out.println(“ereur dans l’ajout de universite:”+e.getMessage());
    session.beginTransaction().rollback();
    }

    }

    @Override
    public void modiefier(Universite univ) {

    Session session =HibernateUtil.getSessionFactory().openSession();
    try {
    session.beginTransaction();
    //session.load(Universite.class, univ.getIdUniv());

    // session.saveOrUpdate(univ);
    session.update(univ);
    session.getTransaction().commit();
    //session.beginTransaction().commit();
    } catch ( HibernateException e) {
    System.out.println(“ereur dans la mise a jour de universite:”+e.getMessage());
    session.beginTransaction().rollback();
    }

  20. #20 by Marcel on April 20, 2013 - 9:38 pm

    What’s up to every one, because I am really keen of reading this blog’s post to be updated daily.
    It includes nice stuff.

  21. #21 by Free Online Car Insurance Quotes on May 26, 2013 - 10:20 pm

    After going over a handful of the blog posts on your website,
    I really appreciate your technique of writing a blog.
    I book marked it to my bookmark webpage list and will be checking
    back soon. Please check out my website too and let me know what you think.

  22. #22 by Carina on July 2, 2013 - 5:05 pm

    Hello there! This is my first visit to your blog!
    We are a group of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us valuable information to work on.
    You have done a marvellous job!

  23. #23 by web site on July 29, 2013 - 6:02 pm

    Just desire to say your article is as astounding. The clarity in
    your post is simply great and i can assume you’re an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the enjoyable work.

  24. #24 by webpage on August 1, 2013 - 4:16 am

    A person essentially assist to make critically articles I might state.
    That is the first time I frequented your web
    page and to this point? I surprised with the analysis
    you made to make this particular put up incredible.
    Magnificent job!

  25. #25 by site on August 3, 2013 - 2:52 am

    Online shops offer year long reductions since it is easier to run an electronic online shop than to run a
    shop in the market place that into a shop which offers you everything underneath the sun that can be purchased.
    Also these online retailers like Overstock.
    com provide special discount including overstock discount signal for promotional and advertising purpose.
    Though a geniune Overstock promotion is hard to find but once
    you get hold of it be sure to be dazzled.

    Overstock Coupon is of very good use, since it has improved the trend of on line buying by often among your brain of people.
    It has also several availing things which improve its
    need. Various kinds coupon rules are there Free Ship Coupon Code,
    specifically Money Saver Coupon Code and many more; all are useful for diverse uses.

    Now these rules are being out of stock to complete business.
    This can be also comfortable for that consumers since they are now free from the risk of advertising with
    a large amount of money. Actually some charge sites and web sites have started the Overstock Coupon program.
    They offer schemes like on a recharge of Rs.10 only
    one will get 20-cent concession on buying Rs 999 and above.
    By using these systems, even the small-scale sites are extending their sales.
    Several shopping internet sites have mounted a border such that shopping beyond that certain amount would undergo no shipping charge.
    Coupon codes are actually available even to reduce the delivery cost.

  26. #26 by server status on July 30, 2014 - 3:11 am

    Sweet blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  1. Common Hibernate Errors and Causes | Lam Nguyen’s Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: