会话已关闭异常 [英] Session is closed Exception

查看:91
本文介绍了会话已关闭异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Struts 2休眠弹簧3构建一个应用程序,并以sql作为后端和c3p0连接池(c3p0-0.9.1.1 jar)。

I am building an application with Struts 2 hibernate spring 3 with my sql as back end and c3p0 connection pooling(c3p0-0.9.1.1 jar).

有时,执行查询时,出现以下错误。每当执行查询时,我都会检查连接是否关闭,如果关闭,我将在执行查询之前打开一个新连接。

Sometimes, when executing a query, I get the below error. When ever I am executing query I check if the connection is closed or not, and if it's closed I will open a new connection before executing the query.

public List<T> find(final Query query, final Object[] values) throws HibernateException, NullPointerException {  
        if (values != null) {  
            for (int i = 0; i < values.length; i++) {  
                query.setParameter(i, values[i]);  
            }  
        }  
        List<T> resultList = null;  
        if (hibernateSession == null || !hibernateSession.isOpen()) {  
            try {  
                openHibernateSession();  
                resultList = query.list();  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                closeHibernateSession();  
            }  
        } else {  
            resultList = query.list();  
        }  
        return resultList;  
    }  







public List<T> find(final String queryString, final Object[] values) throws HibernateException {  
        if (hibernateSession == null || !hibernateSession.isOpen()) {  
            openHibernateSession();  
        }  
        Query queryObject = hibernateSession.createQuery(queryString);  
        return find(queryObject, values);  
    }  







private void openHibernateSession() throws HibernateException {  
    try {  
        hibernateSession = HibernateUtil.getSessionFactory().openSession();  
        hibernateSession.setFlushMode(FlushMode.MANUAL);  
        hibernateTransaction = hibernateSession.getTransaction();  
        hibernateTransaction.begin();  
    } catch (Exception e) {    
        e.printStackTrace();  
    }  

}  







private void closeHibernateSession() {  
    try {  

        if (hibernateTransaction != null) {  
            hibernateSession.flush();  
            if (!hibernateTransaction.wasCommitted()) {  
                hibernateTransaction.commit();  
            }  

        }  
        if (hibernateSession != null) {  
            if (hibernateSession.isOpen()) {  
                hibernateSession.close();  
            }  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  








    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/projectdb</property>  
    <property name="hibernate.connection.username">root</property>  
    <property name="hibernate.connection.password">root</property>  

    <property name="show_sql">true</property>  
    <property name="format_sql">true</property>   

    <property name="hbm2ddl.auto">update</property>  
    <property name="hibernate.c3p0.min_size">5</property>  
    <property name="hibernate.c3p0.max_size">20</property>  
    <property name="hibernate.c3p0.timeout">1800</property>  
    <property name="hibernate.c3p0.max_statements">50</property>  
    <property name="hibernate.c3p0.idle_test_period">50</property>  
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>  
    <property name="hibernate.c3p0.acquire_increment">2</property>  

    <property name="hibernate.c3p0.usesTraditionalReflectiveProxies">true</property>  
    <property name="connection.pool_size">20</property>  
    <property name="current_session_context_class">thread</property>  
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  

    <property name="hibernate.connection.isolation">4</property>  


推荐答案

当您尝试保存或更新临时实例确保与实例关联的实体是持久性的。这是用户尝试使用分离的引用持久保存实体的新实例的用户的常见错误。从持久性上下文中获取实体,然后再将其设置为将要持久化的临时或分离实例的引用。

When you are trying to save or update a transient instance make sure the entities associated with instance are persistent. This is a common error of users trying to persist a new instance of the entity with the detached references. Get the entity from the persistence context before you set its reference to the transient or detached instance that you are going to persist.

这篇关于会话已关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆