1

嗨,我正在使用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();
     }
  }

休眠.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

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

4

1 回答 1

0

在将 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 驱动程序,它解决了问题。

于 2021-07-02T01:46:16.743 回答