【发布时间】:2014-03-05 12:16:48
【问题描述】:
我的 Grails 应用程序有一个服务方法,可以从 last.fm 的网络服务更新艺术家列表。
@Transactional(propagation = Propagation.NOT_SUPPORTED)
void updateLastFmArtists(Range idRange = null) {
Artist.list().each { Artist artist ->
// We could be updating a lot of artists here, the process could take up
// to an hour and we don't want to wrap all that in a single transaction
Artist.withTransaction { status ->
try {
updateArtistInfo(artist)
} catch (IOException ex) {
status.setRollbackOnly()
}
}
}
}
每个艺术家都在其自己的事务中更新,如果抛出IOException,则应回滚。但是,我注意到以下行为:
如果更新艺术家的尝试抛出IOException - 导致事务回滚 - 那么下一个艺术家的更新总是由于以下错误而失败
org.hibernate.LazyInitializationException: 延迟初始化失败 角色集合:org.example.Artist.topTracks,无会话或 会话已关闭
如果我更改上面的代码,以便每个艺术家在其自己的会话中更新,这似乎可以解决问题,
Artist.withNewSession { session ->
Artist.withTransaction { status ->
try {
updateArtistInfo(artist)
} catch (IOException ex) {
status.setRollbackOnly()
}
}
}
但我不明白为什么我需要这样做,即为什么回滚事务似乎会关闭会话?
【问题讨论】:
标签: hibernate grails transactions grails-orm