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.  [ 2 posts ] 
Author Message
 Post subject: error: 2 open sessions on delete.
PostPosted: Mon Oct 17, 2005 7:48 am 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
Hibernate version: 3.0

Name and version of the database you are using: Postgres 8.0

My app is not on an appserver it is a swing app.
I am getting the following error when I try to delete Illegal attempt to associate a collection with two open sessions

Here is my code. I have the transaction stuff commented out but it fails the same way either way

If someone knows what is happening here I would really appreciate smoe insight. I kind of need to get going with this project and was really hoping to use Hibernate with it.

Code:
        if (o instanceof edu.bju.aem.util.model.Server) {
           edu.bju.aem.util.model.Server server = (edu.bju.aem.util.model.Server) o;
           if (currentSelection != null) {
              //TransactionFactory tx =   TransactionFactory.HIBERNATE;
              try{
              //   tx.beginTransaction();
       //          HibernateUtil.closeSession();
                 HibernateUtil.getCurrentSession().delete(server);
                 //server.delete();
                 

               MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
               if (parent != null) {
                   tree.getTreeModel().removeNodeFromParent(currentNode);
                   return;
               }
              // tx.commitTransaction();
              }
              catch(Exception ex){
             //   tx.rollbackTransaction();
                 ex.printStackTrace();
              }
           }
      }


The Server Object and its mapping.
Code:
package edu.bju.aem.util.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import edu.bju.aem.util.persistance.dao.DAOFactory;
import edu.bju.aem.util.persistance.dao.ServerDAO;

public class Server {

   private long id;
   private String name;
   private String description;
   private boolean production;
   private OS os;
   private Set<Service> services = new TreeSet<Service>();
   /**
    * @param description
    * @param name
    * @param os
    * @param production
    */
   public Server(long id, String name, String description, OS os, boolean production) {

      this.id = id;
      this.description = description;
      this.name = name;
      this.os = os;
      this.production = production;
   }
   public Server(){}
   public static ArrayList<Server> loadServersWithOs(){
      ArrayList<Server> servers = new ArrayList<Server>();
      ServerDAO sdao = DAOFactory.HIBERNATE.getServerDAO();
      servers.addAll(sdao.findAll());
      return servers;
   }
   public void save(){
      ServerDAO sdao = DAOFactory.HIBERNATE.getServerDAO();
      sdao.save(this);
      //   sdao.makePersistent(this);
   }
   public void delete(){
      ServerDAO sdao = DAOFactory.HIBERNATE.getServerDAO();
      sdao.makeTransient(this);
   }
   /**
    * @return Returns the description.
    */
   public String getDescription() {
      return description;
   }
   /**
    * @param description The description to set.
    */
   public void setDescription(String description) {
      this.description = description;
   }
   /**
    * @return Returns the name.
    */
   public String getName() {
      return name;
   }
   /**
    * @param name The name to set.
    */
   public void setName(String name) {
      this.name = name;
   }
   /**
    * @return Returns the os.
    */
   public OS getOs() {
      return os;
   }
   /**
    * @param os The os to set.
    */
   public void setOs(OS os) {
      this.os = os;
   }
   /**
    * @return Returns the production.
    */
   public boolean isProduction() {
      return production;
   }
   /**
    * @param production The production to set.
    */
   public void setProduction(boolean production) {
      this.production = production;
   }
   /**
    * @return Returns the id.
    */
   public long getId() {
      return id;
   }

   /**
    * @return Returns the services.
    */
   public Set<Service> getServices() {
      return services;
   }
   /**
    * @param services The services to set.
    */
   public void setServices(Set<Service> services) {
//      TreeSet<Service> ts = new TreeSet<Service>();
//      ts.addAll(services);
//      this.services = ts;
      this.services = services;
   }
   /**
    * @param id The id to set.
    */
   public void setId(long id) {
      this.id = id;
   }
   public String toString(){
      String prod = "";
      if(this.production){
         prod = "Production";
      }

      return this.name + " " + prod + " " + this.description;
   }
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="edu.bju.aem.util.model.Server" table="server" schema="public">
        <id name="id" type="long">
            <column name="id" />
            <generator class="sequence" >
               <param name="sequence">server_id_seq</param>
            </generator>
        </id>
        <many-to-one name="os" class="edu.bju.aem.util.model.OS" unique="true">
            <column name="fkos" not-null="true" />
        </many-to-one>
        <property name="name" type="string">
            <column name="sname" length="25" />
        </property>
        <property name="description" type="string">
            <column name="description" />
        </property>
        <property name="production" type="boolean">
            <column name="production" not-null="true" />
        </property>
        <set name="services" table="serverservice" cascade="save-update" lazy="true" sort="edu.bju.aem.util.model.Service">
        <key>
        <column name="fkserver" not-null="false"/>
        </key>
         <many-to-many class="edu.bju.aem.util.model.Service" column="fkservice" />
        </set>
    </class>
</hibernate-mapping>



The method that the delete() in Server calls
Code:
    public void makeTransient(T entity) {
        getSession().delete(entity);
    }


Top
 Profile  
 
 Post subject: The hibernate Util class
PostPosted: Mon Oct 17, 2005 7:50 am 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
Forgot the hibernate util class.

Code:
public class HibernateUtil {

    private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final String INTERCEPTOR_CLASS = "hibernate.util.interceptor_class";

    private static Configuration configuration;
    private static SessionFactory sessionFactory;
    private static ThreadLocal threadSession = new ThreadLocal();
    private static ThreadLocal threadTransaction = new ThreadLocal();

    private static boolean useThreadLocal = true;

    static {
        // Create the initial SessionFactory from the default configuration files
        try {

            // Replace with Configuration() if you don't use annotations or JDK 5.0
//            configuration = new AnnotationConfiguration();
            configuration = new Configuration();
            // Read not only hibernate.properties, but also hibernate.cfg.xml
            configuration.configure();

            // Assign a global, user-defined interceptor with no-arg constructor
            String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
            if (interceptorName != null) {
                Class interceptorClass =
                        HibernateUtil.class.getClassLoader().loadClass(interceptorName);
                Interceptor interceptor = (Interceptor)interceptorClass.newInstance();
                configuration.setInterceptor(interceptor);
            }

            // Disable ThreadLocal Session/Transaction handling if CMT is used
            if (org.hibernate.transaction.CMTTransactionFactory.class.getName()
                 .equals( configuration.getProperty(Environment.TRANSACTION_STRATEGY) ) )
                useThreadLocal = false;

            if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
                // Let Hibernate bind it to JNDI
                configuration.buildSessionFactory();
            } else {
                // or use static variable handling
                sessionFactory = configuration.buildSessionFactory();
            }

        } catch (Throwable ex) {
            // We have to catch Throwable, otherwise we will miss
            // NoClassDefFoundError and other subclasses of Error
            log.error("Building SessionFactory failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * Returns the original Hibernate configuration.
     *
     * @return Configuration
     */
    public static Configuration getConfiguration() {
        return configuration;
    }

    /**
     * Returns the global SessionFactory.
     *
     * @return SessionFactory
     */
    public static SessionFactory getSessionFactory() {
        SessionFactory sf = null;
        String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
        if ( sfName != null) {
            log.debug("Looking up SessionFactory in JNDI.");
            try {
                sf = (SessionFactory) new InitialContext().lookup(sfName);
            } catch (NamingException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            sf = sessionFactory;
        }
        if (sf == null)
            throw new IllegalStateException("SessionFactory not available.");
        return sf;
    }

    /**
     * Closes the current SessionFactory and releases all resources.
     * <p>
     * The only other method that can be called on HibernateUtil
     * after this one is rebuildSessionFactory(Configuration).
     */
    public static void shutdown() {
        log.debug("Shutting down Hibernate.");
        // Close caches and connection pools
        getSessionFactory().close();

        // Clear static variables
        configuration = null;
        sessionFactory = null;

        // Clear ThreadLocal variables
        threadSession.set(null);
        threadTransaction.set(null);
    }


    /**
     * Rebuild the SessionFactory with the static Configuration.
     * <p>
     * This method also closes the old SessionFactory before, if still open.
     * Note that this method should only be used with static SessionFactory
     * management, not with JNDI or any other external registry.
     */
     public static void rebuildSessionFactory() {
        log.debug("Using current Configuration for rebuild.");
        rebuildSessionFactory(configuration);
     }

    /**
     * Rebuild the SessionFactory with the given Hibernate Configuration.
     * <p>
     * HibernateUtil does not configure() the given Configuration object,
     * it directly calls buildSessionFactory(). This method also closes
     * the old SessionFactory before, if still open.
     *
     * @param cfg
     */
     public static void rebuildSessionFactory(Configuration cfg) {
        log.debug("Rebuilding the SessionFactory from given Configuration.");
        synchronized(sessionFactory) {
            if (sessionFactory != null && !sessionFactory.isClosed())
                sessionFactory.close();
            if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null)
                cfg.buildSessionFactory();
            else
                sessionFactory = cfg.buildSessionFactory();
            configuration = cfg;
        }
     }

    /**
     * Retrieves the current Session local to the thread.
     * <p/>
     * If no Session is open, opens a new Session for the running thread.
     * If CMT is used, returns the Session bound to the current JTA
     * container transaction. Most other operations on this class will
     * then be no-ops or not supported, the container handles Session
     * and Transaction boundaries, ThreadLocals are not used.
     *
     * @return Session
     */
    public static Session getCurrentSession() {
        if (useThreadLocal) {
            Session s = (Session) threadSession.get();
            if (s == null) {
                log.debug("Opening new Session for this thread.");
                s = getSessionFactory().openSession();
                threadSession.set(s);
            }
            return s;
        } else {
            return getSessionFactory().getCurrentSession();
        }
    }

    /**
     * Closes the Session local to the thread.
     * <p>
     * Is a no-op (with warning) if called in a CMT environment. Should be
     * used in non-managed environments with resource local transactions, or
     * with EJBs and bean-managed transactions.
     */
    public static void closeSession() {
        if (useThreadLocal) {
            Session s = (Session) threadSession.get();
            threadSession.set(null);
            Transaction tx = (Transaction) threadTransaction.get();
            if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack()) )
                throw new IllegalStateException("Closing Session but Transaction still open!");
            if (s != null && s.isOpen()) {
                log.debug("Closing Session of this thread.");
                s.close();
            }
        } else {
            log.warn("Using CMT/JTA, intercepted superfluous close call.");
        }
    }

    /**
     * Start a new database transaction.
     * <p>
     * Is a no-op (with warning) if called in a CMT environment. Should be
     * used in non-managed environments with resource local transactions, or
     * with EJBs and bean-managed transactions. In both cases, it will either
     * start a new transaction or join the existing ThreadLocal or JTA
     * transaction.
     */
    public static void beginTransaction() {
        if (useThreadLocal) {
            Transaction tx = (Transaction) threadTransaction.get();
            if (tx == null) {
                log.debug("Starting new database transaction in this thread.");
                tx = getCurrentSession().beginTransaction();
                threadTransaction.set(tx);
            }
        } else {
            log.warn("Using CMT/JTA, intercepted superfluous tx begin call.");
        }
    }

    /**
     * Commit the database transaction.
     * <p>
     * Is a no-op (with warning) if called in a CMT environment. Should be
     * used in non-managed environments with resource local transactions, or
     * with EJBs and bean-managed transactions. It will commit the
     * ThreadLocal or BMT/JTA transaction.
     */
    public static void commitTransaction() {
        if (useThreadLocal) {
            Transaction tx = (Transaction) threadTransaction.get();
            try {
                if ( tx != null && !tx.wasCommitted()
                                && !tx.wasRolledBack() ) {
                    log.debug("Committing database transaction of this thread.");
                    tx.commit();
                }
                threadTransaction.set(null);
            } catch (RuntimeException ex) {
                log.error(ex);
                rollbackTransaction();
                throw ex;
            }
        } else {
            log.warn("Using CMT/JTA, intercepted superfluous tx commit call.");
        }
    }

    /**
     * Rollback the database transaction.
     * <p>
     * Is a no-op (with warning) if called in a CMT environment. Should be
     * used in non-managed environments with resource local transactions, or
     * with EJBs and bean-managed transactions. It will rollback the
     * resource local or BMT/JTA transaction.
     */
    public static void rollbackTransaction() {
        if (useThreadLocal) {
            Transaction tx = (Transaction) threadTransaction.get();
            try {
                threadTransaction.set(null);
                if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
                    log.debug("Tyring to rollback database transaction of this thread.");
                    tx.rollback();
                    log.debug("Database transaction rolled back.");
                }
            } catch (RuntimeException ex) {
                throw new RuntimeException("Might swallow original cause, check ERROR log!", ex);
            } finally {
                closeSession();
            }
        } else {
            log.warn("Using CMT/JTA, intercepted superfluous tx rollback call.");
        }
    }

    /**
     * Reconnects a Hibernate Session to the current Thread.
     * <p>
     * Unsupported in a CMT environment.
     *
     * @param session The Hibernate Session to be reconnected.
     */
    public static void reconnect(Session session) {
        if (useThreadLocal) {
            log.debug("Reconnecting Session to this thread.");
            session.reconnect();
            threadSession.set(session);
        } else {
            log.error("Using CMT/JTA, intercepted not supported reconnect call.");
        }
    }

    /**
     * Disconnect and return Session from current Thread.
     *
     * @return Session the disconnected Session
     */
    public static Session disconnectSession() {
        if (useThreadLocal) {
            Transaction tx = (Transaction) threadTransaction.get();
            if (tx != null && (!tx.wasCommitted() || !tx.wasRolledBack()) )
                throw new IllegalStateException("Disconnecting Session but Transaction still open!");
            Session session = getCurrentSession();
            threadSession.set(null);
            if (session.isConnected() && session.isOpen()) {
                log.debug("Disconnecting Session from this thread.");
                session.disconnect();
            }
            return session;
        } else {
            log.error("Using CMT/JTA, intercepted not supported disconnect call.");
            return null;
        }
    }

    /**
     * Register a Hibernate interceptor with the current SessionFactory.
     * <p>
     * Every Session opened is opened with this interceptor after
     * registration. Has no effect if the current Session of the
     * thread is already open, effective on next close()/getCurrentSession().
     * <p>
     * Attention: This method effectively restarts Hibernate. If you
     * need an interceptor active on static startup of HibernateUtil, set
     * the <tt>hibernateutil.interceptor</tt> system property to its
     * fully qualified class name.
     */
    public static void registerInterceptorAndRebuild(Interceptor interceptor) {
        log.debug("Setting new global Hibernate interceptor and restarting.");
        configuration.setInterceptor(interceptor);
        rebuildSessionFactory();
    }

    public static Interceptor getInterceptor() {
        return configuration.getInterceptor();
    }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.