[英]@PostConstruct and “No Hibernate Session bound to thread” exception
I have to do some database stuff in my repository' @PostConstruct
: 我必须在我的存储库'
@PostConstruct
做一些数据库的东西:
@Repository
public class SomeRepositoryHibernate implements SomeRepository {
private SessionFactory sessionFactory;
@Autowired
public SomeRepositoryHibernate(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
...
@PostConstruct
public void doSomestuffWithDb() {
...
}
}
but it fails with: 但它失败了:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and
configuration does not allow creation of non-transactional one here
is there any easy solution for that? 那有什么简单的解决方案吗?
Thanks! 谢谢!
@PostConstruct
@PostConstruct
没有正在运行的事务 @Transactional
there (except for mode="aspectj"
), so spring can't start the transaction @Transactional
(除了mode="aspectj"
),所以spring无法启动事务 So, you would have to create your session from the session factory (via .openSession()
) there and start a transaction manually. 因此,您必须从会话工厂(通过
.openSession()
)创建会话并手动启动事务。
assuming you are using hibernate combining with spring and you have configured your sessionFactory and transaction manager correctly in your spring config file. 假设您正在使用与spring结合的hibernate,并且已在spring配置文件中正确配置了sessionFactory和事务管理器。 Then the root cause is that when your doSomestuffWithDb() method is invoked the transaction prepare work has not been finished by spring.
然后根本原因是当调用doSomestuffWithDb()方法时,事务准备工作尚未完成。 The @postconstruct can only ensure the method is called after the bean is created, it can not ensure the container is ready for everything- here, I mean the transaction related stuff- at the moment.
@postconstruct只能确保在创建bean之后调用该方法,它无法确保容器已准备好用于所有内容 - 此处,我的意思是与事务相关的东西 - 此刻。 There is a detailed discussion in spring forum.
春季论坛有详细的讨论。 http://forum.springsource.org/showthread.php?58337-No-transaction-in-transactional-service-called-from-PostConstruct Also, the author submitted his solution to jira at https://jira.springsource.org/browse/SPR-5966?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs
http://forum.springsource.org/showthread.php?58337-No-transaction-in-transactional-service-called-from-PostConstruct此外,作者还在https://jira.springsource.org上向jira提交了他的解决方案。 /browse/SPR-5966?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.