Hibernate4 integrated spring4 error----No Session found for current thread

This error occurred when writing a small demo that integrates spring4 with Hibernate4:

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)

You can clearly see the reason, that is, the current thread does not find the session.
Detailed principle analysis (reproduced):

SessionFactory's getCurrentSession does not guarantee that a new Session will be created automatically if there is no current Session, it depends on the implementation of CurrentSessionContext, SessionFactory will call CurrentSessionContext's currentSession() method to get Session. In Spring, if we directly call getCurrentSession() without configuring TransactionManager and calling SessionFactory.openSession() beforehand, the program will throw a "No Session found for current 
thread" exception. If TranactionManager is configured and the transaction boundary is configured by @Transactional or declaration, Spring will create a Session for the current thread through AOP before starting the transaction. At this time, calling getCurrentSession() will get the correct result.

However, the reason for the above exception is that Spring provides its own implementation of CurrentSessionContext. If we do not plan to use Spring, but create a SessionFactory directly from hibernate.cfg.xml, and set current_session_context_class to thread in hibernate.cfg.xml, Even if ThreadLocalSessionContext is used, then when we call getCurrentSession(), if there is no Session in the current thread, it will create a binding to the current thread.

Hibernate will use JTASessionContext by default. Spring provides its own SpringSessionContext, so we do not need to configure current_session_context_class. When Hibernate integrates with Spring, it will use this SessionContext, so the effect of calling getCurrentSession() at this time completely depends on the implementation of SpringSessionContext.

Using Hibernate without Spring, if the current_session_context_class is not configured in hibernate.cfg.xml, and there is no JTA, the program will throw a "No 
CurrentSessionContext configured!" exception. The solution at this time is to configure current_session_context_class as thread in hibernate.cfg.xml.

Using Hibernate in Spring, if we configure TransactionManager, then we should not call openSession() of SessionFactory to get Session, because the Session obtained in this way is not managed by transaction.

 

The specific operation is as follows---

Configuration of transaction management in spring:

copy code
<!-- Configure Spring's declarative transaction -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
copy code

Business logic classes use annotations:

copy code
@Service
public class CustomerServiceImpl implements CustomerService {  
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
    ...
}
copy code

 

In addition, in the hibernate configuration file, you can also add this configuration to avoid this error:

<property name="current_session_context_class">thread</property>
Source: https://www.cnblogs.com/Beansczm/p/6767030.html




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326551207&siteId=291194637