`

org.hibernate.SessionException: Session is closed

 
阅读更多

    /**
     * <p>Title: executeInCyouSession</p>
     * <p>Description: 将持久化操作放入事务中</p>
     *
     * @author youyan
     * @param  action        --    持久化操作(增删改查)
     * @return Object        --    结果数据
     * @throws CyouBaseException
     */
    public Object executeInCyouSession(CyouCallback action) throws CyouBaseException
    {
        this.session = persistenManager.getCurrentCyouSession();
        Object result = null;
        try {
            log.info("*****************begin transaction*****************");
            session.beginTransaction();
            result = action.doInTemplate(session);
            session.commitTransaction();
            log.info("*****************commit transaction*****************");
        } catch (CyouBaseException e) {
            session.rollbackTransaction();
            log.info("!!!!!!!!!!!!!rollback transaction!!!!!!!!!!!!!");
            log.error("error happend when executeInCyouSession(): " + e.getMessage());
            throw new CyouBaseException
                ("error happend when executeInCyouSession(): " + e.getMessage() , e);
        }
        //由于hibernate是延迟加载机制,故如果在此地方关闭session,会导致延迟加载失败,
        //报org.hibernate.SessionException: Session is closed! 异常
        //另外可以采用lazy=false的配置策略
        //事务提交后会自动关闭session,故注释以下session关闭代码,防止异常出现
        /*finally
        {
            if(session != null && session.isConnected())
                session.close();
        }*/
        return result;
    }

分享到:
评论

相关推荐

    org.hibernate.HibernateException: No Hibernate Session bound to thread

    Hibernate Session 绑定线程解决方案 在 Java web 开发中, Hibernate 是一个非常流行的 ORM(Object-Relational Mapping)框架,用于将 Java 对象映射到关系数据库中。然而,在使用 Hibernate 进行数据库操作时,...

    HIbernate4.3.6整合c3p0所需jar

    org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal....

    hibernate(api 介绍).pdf

    3. org.hibernate.Session Interface:提供了相关的数据库操作,如添、更、删、加、查等基本操作,非线程安全,轻量级,其创建及销毁消耗资源少。每个客户请求对应一个 Session。 4. org.hibernate.Transaction ...

    hibernate-release-5.2.16.Final.zip

    2. **配置文件**:Hibernate的配置文件(hibernate.cfg.xml)用于定义数据源、实体类、JDBC连接参数等,是启动Hibernate的第一步。 3. **实体类(Entity)**:实体类代表数据库表,通过注解或XML映射文件定义其属性...

    hibernate(api 介绍).docx

    2. org.hibernate.SessionFactory Interface:从该接口中获得 Session 实例,具备多线程安全和重量级特点,一个数据库对应一个 SessionFactory,其创建及销毁消耗资源多。 3. org.hibernate.Session Interface:和...

    hibernate.properties

    #hibernate.query.factory_class org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory ################# ### Platforms ### ################# ## JNDI Datasource #hibernate.connection....

    hibernate-src.zip源代码包

    3. **org.hibernate.boot** 包:这部分涉及Hibernate的启动过程和元数据加载,比如`MetadataSources`和`MetadataBuilder`,它们负责从不同来源(如XML配置文件、注解等)收集元数据。 4. **org.hibernate.type** 包...

    hibernate jar包

    - `org.hibernate.Session`:这是与数据库交互的主要接口,负责执行CRUD操作和查询。 - `org.hibernate.cfg.Configuration`:用于配置Hibernate,加载并构建SessionFactory。 - `org.hibernate.SessionFactory`:...

    hibernate annotations

    1. **配置**:首先,需要在项目中引入Hibernate库,并创建一个配置文件(通常是hibernate.cfg.xml),配置数据库连接信息。 2. **实体定义**:使用注解定义实体类及其属性,指定与数据库表的对应关系。 3. **...

    commons-beanutils-1.7.0

    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:251) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:236) at org.hibernate.cfg.HbmBinder.bindRoot...

    hibernate源码包

    2. **org.hibernate.cfg**: 配置相关的类,如Configuration,用于加载Hibernate配置文件,建立SessionFactory。Environment类中定义了一些常量,比如连接数据库所需的属性。 3. **org.hibernate.cache**: 缓存管理...

    jdbc+hibernate的jar包

    1. `org.hibernate.Session`:类似于JDBC中的Connection,代表一次数据库会话,通过Session我们可以操作持久化对象。 2. `org.hibernate.cfg.Configuration`:配置对象,用于设置Hibernate的属性,如数据库连接信息...

    Hibernate3的配置参数汇总

    - `hibernate.dialect`:指定对应数据库的方言,如`org.hibernate.dialect.PostgreSQLDialect`,以便Hibernate生成最优的SQL。 - `hibernate.show_sql`:如果设为`true`,Hibernate会在控制台输出所有执行的SQL...

    com.microsoft.sqlserver.jdbc.SQLServerException: 只进结果集不支持请求的操作 解决方案

    query.setHint("org.hibernate.fetchSize", 20); // 设置查询结果集大小 query.setFetchSize(20); List&lt;Game&gt; games = query.list(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx....

    hibernate-commons-annotations-4.0.1.Final.jar

    hibernate-commons-annotations-4.0.1.Final.jar

    hibernate-release-5.3.18.Final.tgz

    - Session:是Hibernate的主要工作接口,负责与数据库的交互,提供了数据的增删改查操作。 - EntityManager:JPA的一部分,提供了对实体对象的操作,包括管理实体状态、事务处理等。 - Configuration:配置...

    hibernate_src.zip

    5. `org.hibernate.query`:包含HQL(Hibernate Query Language)和JPQL(Java Persistence Query Language)的处理逻辑。 三、核心组件解析 1. SessionFactory:它是所有Session的工厂,负责初始化数据库连接、...

    Hibernate常用包及XML

    7. **org.hibernate.event**:这个包包含了Hibernate的各种事件监听器,比如SaveOrUpdateEventListener、DeleteEventListener等,它们监听实体的生命周期事件,如保存、更新、删除等,可以自定义扩展行为。...

    基于IDEA的SSH项目之五:集成Hibernate--程序包

    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt; &lt;property name="hibernate.connection.url"&gt;...

    hibernate2.1相关jar包

    导入这些JAR文件后,开发者需要配置Hibernate的主配置文件(通常命名为`hibernate.cfg.xml`),指定数据库连接参数、实体类和映射文件等。接着,可以创建SessionFactory并打开Session,通过Session进行CRUD(创建、...

Global site tag (gtag.js) - Google Analytics