-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Issues with collection association in two open sessions
PostPosted: Fri Sep 17, 2004 12:36 pm 
Newbie

Joined: Thu Jun 03, 2004 10:29 am
Posts: 13
Hibernate version: 2.1.6

I seem to be constantly running into issues with Illegal attempt to associate a collection in two open sessions. I almost exclusivly use criteria queries. After every session call I make I implicitly call

session.close();
session = null;

This had solved some issue that I have had in the past. I don't know what I can do to stop these errors.

I'm now at a loss and don't know what else I can do.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 12:55 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
how are you managing your sessions?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 2:30 pm 
Newbie

Joined: Thu Jun 03, 2004 10:29 am
Posts: 13
This is the code snippit from a class called ConnectionFactory:

Code:
private static void buildSessionFactory()
    {
        try
        {
            Configuration hibernateConfig = new Configuration();
            try
            {
                hibernateConfig.configure();
            }
            catch (HibernateException ex)
            {
                ResourceBundle bundle = PropertyResourceBundle.getBundle("J2EE");
                hibernateConfig.addFile((String)bundle.getObject("sobetech.hibernate.config")
                        + "/" + configFileName);
            }

            factory = hibernateConfig.buildSessionFactory();
        }
        catch (HibernateException ex)
        {
            factory = null;
        }
    }

    public static Session getSession() throws HibernateException
    {
        if(factory == null)
        {
            buildSessionFactory();
        }
        return factory.openSession();
    }


So when I want a session I call:

Code:
Session session = ConnectionFactory.getSession();


I get the session Object

This is an offending method that is causing my errors:

Code:
    public Cart updateCart(Cart cart) throws EAOException
    {
        try
        {
            session = ConnectionFactory.getSession();
            session.update(cart);
            session.flush();
            session.close();
            session = null;
            return cart;
        }
        catch (HibernateException ex)
        {
            throw new EAOException(ex);
        }
    }


These are the settings that I have in the hibernate.cfg.xml:
Code:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.query.substitutions">true 'T', false 'F', yes 'Y', no 'N'</property>
      <property name="hibernate.connection.datasource">sobetech.jdbc.datasource.smoke</property>
      <property name="hibernate.connection.pool_size">1</property>
      <property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
      <property name="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.WeblogicTransactionManagerLookup</property>
      <property name="hibernate.show_sql">false</property>
      <property name="hibernate.jdbc.batch_size">0</property>
      <property name="hibernate.jdbc.use_streams_for_binary">true</property>
      <property name="hibernate.jndi.url">t3://localhost:7001</property>
      <property name="hibernate.jndi.class">weblogic.jndi.WLInitialContextFactory</property>
      <property name="hibernate.max_fetch_depth">1</property>
      <property name="hibernate.cache.region_prefix">hibernate.test</property>
      <property name="hibernate.cache.use_query_cache">true</property>
      <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
      <property name="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</property>
       

[i]mappings....[/i]

   </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 2:55 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it's better to use threadlocal pattern...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 3:15 pm 
Newbie

Joined: Thu Jun 03, 2004 10:29 am
Posts: 13
I think I have found a solution, but would like a bit of confirmation.

I had the Session object scoped as a class member. When I moved the scope to the method level, it seems to have fixed my current issue.

Does this make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 3:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
A lot of sense - especially in a multithreaded environment like for example a servlet.

Still recommending ThreadLocal pattern.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.