下面是Hibernate中关于批处理的代码示例:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
在开始编写代码时,它使用openSession()
.However,当我编写代码时,我使用getCurreentSession()
。它似乎会生成org.hibernate.TransactionException: nested transactions not supported
错误。
有人能解释一下为什么会发生这种情况吗?
发布于 2013-10-10 16:39:49
SessionFactory.openSession()总是打开一个新会话,一旦您完成操作,就必须关闭该会话。SessionFactory.getCurrentSession()返回一个绑定到上下文的会话--您不需要关闭它。
https://stackoverflow.com/questions/19301424
复制相似问题