【问题标题】:Spring/Hibernate No session found for current thread (config)Spring/Hibernate 没有为当前线程找到会话(配置)
【发布时间】:2014-04-23 04:11:40
【问题描述】:

我正在尝试一起了解 Spring 和 Hibernate 的配置。每次我调用数据库时,我都会在我的控制台中找到一个没有找到当前线程错误的会话(不会停止应用程序,因为如果找不到,我只是创建一个新会话)。我有一个标准的 STS Maven 设置。以下是我当前设置的配置。但是,如果我将 root-context.xml 中的所有内容放在 servlet-context.xml 的底部,则它可以正常工作而不会出现任何错误。因此,我猜测 servlet-context.xml 中的某些内容是“覆盖”某些内容,而不是使用我的服务上的 @Transactional 注释。但是你如何配置呢?

根上下文.xml

    <!-- Configure JDBC Connection-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:C:\Users\Steven\Desktop\Programming\db-derby-10.10.1.1-bin\db-derby-10.10.1.1-bin\bin\mydb" />
</bean>

<!-- Configure Hibernate 4 Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="packagesToScan" value="com.css.genapp" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

servlet-context.xml

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basenames">
        <beans:list>
            <beans:value>format</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<context:component-scan base-package="com.css.genapp" />

【问题讨论】:

  • 你是在 web.xml 中加载 root-context.xml 吗?如果不在你的 servlet-context.xml 中使用这个
  • 是的,它被加载到 web.xml 中。我知道它是从 root-context.xml 中提取的,因为那是数据库配置所在的位置,但它不采用事务注释。

标签: spring hibernate spring-mvc


【解决方案1】:

您必须在 servlet-context.xml 和 root-context.xml 上定义组件扫描

  • root-context.xml:用于服务、存储库...
  • servlet-context.xml:用于控制器和 Web 相关组件。

希望对您有所帮助。您可以查看此示例的详细信息Spring, JPA and hibernate integration

【讨论】:

  • 这是正确的,您通常在 servlet 中指定控制器,而其余的则在 root 中。您通过正则表达式或 dir structre 匹配
  • 是的,似乎是问题所在。
【解决方案2】:

我认为在您的 Hibernate 配置中您配置了 &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt; 像这样。

删除该属性,因为它确实在春季破坏了正确的会话/事务管理。

【讨论】:

    【解决方案3】:

    您必须导入 root-context.xml,因为调度程序 servlet 只会读取 servlet-context.xml。在您的 servlet-context.xml 中添加以下行:

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 2014-01-10
      • 2013-06-21
      • 2014-08-16
      • 2012-05-05
      • 2012-05-14
      • 1970-01-01
      • 2013-11-19
      相关资源
      最近更新 更多