【问题标题】:java.lang.IllegalStateException: No Hibernate Session bound to threadjava.lang.IllegalStateException:没有绑定到线程的休眠会话
【发布时间】:2012-02-12 02:27:54
【问题描述】:

我是 Spring 和 hibernate 的新手,我被这个问题困住了。我一直在寻找解决方法,但尽管对此有很多问题,但它们似乎并没有解决我的问题。我正在使用带有休眠 3.6.9 的 spring 3.1.0 并使用 spring mvc 制作一个 Web 应用程序。经过大量环顾后,我设法通过以下配置解决了这个问题

web.xml

<listener> <description>Spring context loader</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>


<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

applicationContext.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven  />
<context:annotation-config/>

<!-- Scans within the base package of the application for @Components to 
    configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.emumba.cricketcalendar" />



<import resource="hibernate-context.xml"/>

hibernate-context.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.emumba.cricketcalendar.domain.Match</value>
            <value>com.emumba.cricketcalendar.domain.Ground</value>
            <value>com.emumba.cricketcalendar.domain.Umpire</value>
            <value>com.emumba.cricketcalendar.domain.Country</value>
            <value>com.emumba.cricketcalendar.domain.CricketStatus</value>
            <value>com.emumba.cricketcalendar.domain.Series</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

然后我将@Transactional 放在我的服务上,这个异常就消失了。但是随后我的自动装配停止工作,自动装配注释不起作用,具有自动装配属性的 bean 开始抛出异常。我从我的服务中删除了@transactional 注释,它再次开始工作,但“没有绑定到线程的休眠会话”异常返回

所以我真的很困惑,任何帮助将不胜感激

编辑服务代码

@Service(value="calendarManager") 公共类 CalendarMangerImpl 实现 CalendarManager {

@Autowired
@Qualifier("matchDao")
public MatchDaoHibernate matchDao;

@Override
public List<Match> getAllMatches() {
    List<Match> matches=new ArrayList<Match>();
    matches=matchDao.findAll();
    return matches;
}

}

【问题讨论】:

  • 我们可以查看您的服务的代码吗?
  • 我正要讨论 JDK 与 CGLIB 代理问题。看起来你已经得到了答案。
  • @Alex ya 我做到了,最后 :)thanx 无论如何 :)

标签: java hibernate spring spring-mvc


【解决方案1】:

使用服务类参考其接口而不是实际类,因为 Spring 默认使用基于接口的代理

Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables 
any interface (or set of interfaces) to be proxied. 

【讨论】:

  • @Arvind A 我所有的 daos 都在扩展一个通用的 dao,当我将 @transactional 从子类移到父类时,它给出了 org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
  • @khizar 再问一个问题 - 您如何使用该服务?我希望您使用 CalendarManager myservice 而不是 CalendarMangerImpl myservice 从您调用它的任何地方。
  • 好吧,我正在使用 CalendarManagerImpl ,因为 @Service 注释在类而不是接口上。有错吗?
  • 是的。使用 CalendarManager myservice 。即使在这种情况下也会检测到服务注释。您正在使用基于接口的代理 - 因此您需要在那里有一个接口。
  • @Khizar 将 CGLIB jar 添加到您的类路径中,因为您正在执行基于类的代理而不是基于 JDK 接口的代理
【解决方案2】:

我会建议先go through this 简单的例子清除所有的概念。将来也会对您有所帮助。

【讨论】:

  • thanx 但我已经做了一些简单的例子,基本上我只是把它带到更复杂的问题......
  • @Khizar 可以验证您的配置设置吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 2023-03-11
  • 2011-10-25
  • 2011-01-31
相关资源
最近更新 更多