简体   繁体   English

@PostConstruct和“没有Hibernate Session绑定到线程”异常

[英]@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! 谢谢!

  • You don't have a running transaction in @PostConstruct 您在@PostConstruct没有正在运行的事务
  • you can't use @Transactional there (except for mode="aspectj" ), so spring can't start the transaction 你不能在那里使用@Transactional (除了mode="aspectj" ),所以spring无法启动事务
  • a transaction is required for hibernate to mutate data (insert/update/delete) hibernate需要一个事务来改变数据(插入/更新/删除)

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.

相关问题 使用@PostConstruct时,Hibernate无法找到绑定到线程的会话 - Hibernate cannot find session bound to thread when using @PostConstruct 没有Hibernate Session绑定到线程异常 - No Hibernate Session bound to thread exception Hibernate异常:没有Hibernate Session绑定到线程 - Hibernate exception: No Hibernate Session bound to Thread Spring + Hibernate异常:没有绑定到线程的hibernate会话 - Spring+Hibernate Exception : No hibernate session bound to thread HibernateException:没有 Hibernate Session 绑定到线程 - HibernateException: No Hibernate Session bound to thread HibernateSystemException:没有 Hibernate Session 绑定到线程 - HibernateSystemException: No Hibernate Session bound to thread Hibernate 和 Spring3 事务管理注释-配置问题:休眠异常:没有 Hibernate Session 绑定到线程 - Hibernate and Spring3 Transaction Management Annotation-Configuration problems: Hibernate-Exception: No Hibernate Session bound to thread 带注释的Spring + Hibernate:没有Hibernate会话绑定到线程 - Spring + Hibernate with annotations: No Hibernate Session bound to thread Spring + Hibernate:没有Hibernate Session绑定到线程 - Spring + Hibernate: No Hibernate Session bound to thread 未找到当前线程的会话:Spring 3和Hibernate 4(使用@PostConstruct批注) - No Session found for current thread: Spring 3 and Hibernate 4 (Using @PostConstruct annotation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM