简体   繁体   English

org.hibernate.HibernateException:使用tx:advise找不到当前线程的会话

[英]org.hibernate.HibernateException: No Session found for current thread with tx:advise

I am using spring 3.2 with hibernate 4. I want to use spring to control the transactions. 我正在使用Spring 3.2和Hibernate4。我想使用Spring来控制事务。 However with the configuration mentioned below I get the 但是,使用下面提到的配置,我得到了

'Servlet.service() for servlet spring threw exception: org.hibernate.HibernateException: No Session found for current thread' “ Servlet的Servlet.service()引发异常:org.hibernate.HibernateException:当前会话未找到会话”

exception: 例外:

<aop:config>
    <aop:pointcut id="serviceMethods"
        expression="execution(* com.locator.service.impl.ServiceTypeService.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="hbTransactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<!-- Hibernate session factory -->
<bean id="hbSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>../spring/model/ServiceType.hbm.xml</value>
        </list>
    </property>

</bean>

<bean id="hbTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hbSessionFactory" />
</bean>

<bean id="serviceTypeService" class="com.locator.service.impl.ServiceTypeService">
    <property name="serviceTypeDao" ref="serviceTypeDao"></property>
</bean>

<bean id="serviceTypeDao" class="com.locator.dao.impl.ServiceTypeDao">
    <property name="sessionFactory" ref="hbSessionFactory"></property>
</bean>

The code for the Dao layer and the Service is as follows: Dao层和Service的代码如下:

public class ServiceTypeDao implements IServiceTypeDao{

    private static final Log log = LogFactory.getLog(ServiceTypeDao.class);

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory(){
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }

    public ServiceType findById(int id) {
        log.debug("getting ServiceType instance with id: " + id);
        try {
            Session session = getSessionFactory().getCurrentSession();
            ServiceType instance = (ServiceType)  session.get("com.locator.model.ServiceType", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }

            instance.setName(instance.getName()+"0");
            session.saveOrUpdate(instance);

            return instance;
        }catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
}


public class ServiceTypeService implements IServiceTypeService{

    private ServiceTypeDao serviceTypeDao;

    public void setServiceTypeDao(ServiceTypeDao serviceTypeDao){
        this.serviceTypeDao = serviceTypeDao;
    }

    public ServiceType getServiceTypeById(int id){
        return serviceTypeDao.findById(id);
    }
}

Replacing getSessionFactory().getCurrentSession() with getSessionFactory().openSession() will resolve the above issue however, it will mean that the developer will then be responsible for the session open/close rather than spring. 将getSessionFactory()。getCurrentSession()替换为getSessionFactory()。openSession()将解决上述问题,但是,这意味着开发人员将负责会话的打开/关闭而不是弹簧。 Therefore, please advise how this can be resolved using spring. 因此,请告知如何使用Spring解决此问题。

I was able to resolve the issue. 我能够解决问题。 It was occurring due to the following problems: 发生此问题是由于以下问题:

  1. The Service class had not been Auto wired into the controller ie the @Autowired annotation was missing. 没有将Service类自动连接到控制器,即缺少@Autowired注释。
  2. The configuration for the web.xml had to be modified with the listener class 'org.springframework.web.context.ContextLoaderListener' and the context-param was added. 必须使用侦听器类“ org.springframework.web.context.ContextLoaderListener”修改web.xml的配置,并添加了context-param。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException: 没有找到当前线程 5 的会话 - org.hibernate.HibernateException: No Session found for current thread 5 Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread 请求处理失败; 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread Estado HTTP 500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread Spring Security authenticationFailure:错误org.hibernate.HibernateException:没有找到当前线程的会话 - Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread 嵌套的异常是org.hibernate.HibernateException:当前会话addDepartment控制器找不到会话 - nested exception is org.hibernate.HibernateException: No Session found for current thread addDepartment controller 由于&#39;org.hibernate.HibernateException所需的@Transactional注释:找不到当前线程的异常&#39;异常 - @Transactional annotation required due to 'org.hibernate.HibernateException: No Session found for current thread' exception Micronaut MySQL删除记录org.hibernate.HibernateException:当前会话找不到会话 - Micronaut MySQL delete record org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException:找不到会话 - org.hibernate.HibernateException: No Session found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM