延迟加载和多个事务
问题描述:
我对一个休眠会话中的惰性迭代器和多个事务有点困惑。有以下代码块:延迟加载和多个事务
@Transactional
public void runtProcessing() {
HibernateTemplate hibernateTemplate = ...
Session hibernateSession = hibernateTemplate.getSessionFactory().getCurrentSession();
Iterator<DomainObject> domainObjects = hibernateTemplate.iterate(...);
try {
while (domainObjects.hasNext()) {
hibernateSession.beginTransaction();
DomainObject domainObject = domainObjects.next();
processDomainObject(domainObject);
hibernateSession.getTransaction().commit();
}
}
由于有多个事务,我不知道迭代器在什么事务中工作?
答
从这里http://ayende.com/blog/3775/nh-prof-alerts-use-of-implicit-transactions-is-discouraged
当我们没有定义自己的事务,我们又陷入隐性 交易模式,其中每条语句在其 自己的事务数据库的运行,从而导致更高的性能成本(数据库时间 构建和拆除事务)并降低一致性。
因此迭代器作为其自身事务的一部分运行。希望这是有道理的。
用'@ Transactional'注释更新代码。已经有一个打开的交易。 –