【问题标题】:Hibernate StatelessSession, "the statement is closed" exceptionHibernate StatelessSession,“语句关闭”异常
【发布时间】:2012-11-26 09:53:24
【问题描述】:

您好,我正在使用 StatelessSession 接口进行批量插入,但出现异常:“语句已关闭”。

以下是我在代码中使用的详细信息,我通过谷歌找到了这个例外,但没有找到解决方案。

示例代码:

    @Name("sample")
    @Scope(ScopeType.STATELESS)
    @AutoCreate
    public Sample{  

        @In
        private SessionFactory hibernateSessionFactory;

        @Begin(join=tru)
        public void migrate(){
            StatelessSession session = hibernateSessionFactory.openStatelessSession();
            Transaction tx = session.beginTransaction();
            for(DoTempCustomers tempCust:doTempCustomers){
                  TempCustomers temp=new TempCustomers();
                  BeanUtils.copyProperties(temp, tempCust);
                  session.insert(temp);
            }       
        }

        tx.commit();
        session.close();
     }
  }

hibernate.cfg.xml

<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.jdbc.batch_size">1</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>

<!-- Here are the mappings -->
<mapping package="com.manam.mobee.persist.entity"/>
<mapping class="com.manam.mobee.persist.entity.TempCustomers"/>

例外

Caused by: java.sql.SQLException: The statement is closed.
        at org.jboss.resource.adapter.jdbc.WrappedStatement.checkState(WrappedStatement.java:599)
        at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.setString(WrappedPreparedStatement.java:313)
        at org.hibernate.type.StringType.set(StringType.java:26)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:107)
        at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1997)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2243)
        ... 96 more
12:16:54,777 INFO  [STDOUT] [Mobee]- INFO 2012-12-08 12:16:54,777 [] StringType - could not bind value 'TIMOTHY NDUNG'U MAINA' to parameter: 1; The statement is closed.
12:16:54,777 INFO  [STDOUT] [Mobee]- WARN 2012-12-08 12:16:54,777 [] JDBCExceptionReporter - SQL Error: 0, SQLState: null
12:16:54,777 INFO  [STDOUT] [Mobee]-ERROR 2012-12-08 12:16:54,777 [] JDBCExceptionReporter - The statement is closed.
12:16:54,778 ERROR [STDERR] org.hibernate.exception.GenericJDBCException: could not insert: [com.manam.mobee.persist.entity.TempCustomers]
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntity

你能告诉我如何解决这个异常吗?

【问题讨论】:

  • 正确缩进代码后,这看起来不正确,您能更正代码吗?

标签: java hibernate session seam


【解决方案1】:

在将 Hibernate 从 3.4 迁移到 5.4.x 版本时,我在使用 IBM DB2 数据库时遇到了类似的问题。

在谷歌搜索和查看各种论坛之后,问题在于 DB2 驱动程序的版本。以下线程有关于该问题的更多详细信息。

https://www.ibm.com/support/pages/why-does-isclosed-throw-exception-when-used-resultset

我更新了支持 JAVA8 的 DB2 驱动程序,它修复了问题。

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 2014-01-18
    • 1970-01-01
    • 2012-05-20
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多