当前位置: 首页 > 知识库问答 >
问题:

与spring一起使用时HibernateSessionFactory.getCurrentSession()

商宝
2023-03-14

我试图将Hibernate与我的spring应用程序上下文联系起来。我正在成功地连接SessionFactory,但当我尝试调用getCurrentSession时,我看到以下错误:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:270)
at $Proxy8.getCurrentSession(Unknown Source)
at org.company.web.ws.sample.impl.CheckSecurityImpl.verify(CheckSecurityImpl.java:31)
at org.company.web.ws.sample.impl.CheckSecurityImplTest.testCheckSecurity(CheckSecurityImplTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我正在配置数据源会话工厂和事务管理器。我还指定了事务处理将是注释驱动的

下面是我的上下文XML:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    ">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:murex" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean> 

<jdbc:initialize-database data-source="dataSource">
  <jdbc:script location="classpath:/db-schema.sql"/>
  <jdbc:script location="classpath:/db-testdata.sql"/>
</jdbc:initialize-database>

<!-- Hibernate Session Factory  -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
       <list>
          <value>/security.hbm.xml</value>
       </list>
    </property> 
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

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

<tx:annotation-driven/>

<bean id="checkSecurityService" class="org.example.web.ws.sample.impl.CheckSecurityImpl" />

null

下面是我发现错误的类:

@WebService(endpointInterface = "org.example.web.ws.sample.CheckSecurity")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class CheckSecurityImpl implements CheckSecurity {

    @Autowired 
    private SessionFactory sessionFactory;

    @Transactional
    public CheckSecurityResponse verify(String propertyValue, String propertyType) {
        Session session = sessionFactory.getCurrentSession();
    }
}

当这被称为sessionFactory时不是null,而是带有上述错误的错误。如果我使用openSession,一切工作都很好,但我知道这不能用于事务。

有什么想法吗?谢谢

共有2个答案

郎意
2023-03-14

你能把整个堆栈的痕迹贴出来吗?

我猜异常是由TransactionManager引发的,而不是由Verify()中的GetCurrentSession()引发的。

您将分别使用transactionmanagersession

您可以通过删除事务的注释来快速测试这一点。

要获得正确的修复,请尝试使用HibernateTemplate而不是直接使用SessionFactory

芮化
2023-03-14

上下文中缺少组件扫描,因此不运行@Transactional后处理器来修饰事务包装的方法。

 类似资料:
  • 问题内容: 我是Spring的新手。 我们正在使用Spring Security功能。数据库连接:JPA的eclipselink实现。数据库:MySql 使用spring security时,身份验证提供程序的配置如下- 但是在JPA中,我们没有定义数据源,我们将Persistence unit与provider一起使用 那么,我们如何配置身份验证提供程序,以便将JPA用于数据库连接? 数据源引用

  • 我想在我的Web应用程序中使用服务器托管连接池,现在我使用JdbcTemboard来获取连接。如何从WebSphere获取数据源并在我的应用程序中使用。

  • 我有一个带有Spring(5.7.2)Web/Security的REST API项目。 我只有一个表,它包含一个id和一个名称。 当我删除光盘时,我的DiscsService会 当我需要插入或更新时,我会: 我了解到,当您希望在同一事务中使用值(提交之前)时,将使用,并且将同步并清空SQL缓存。 在我的例子中,我看到我在数据库中插入/更新或删除了值。 我应该使用flush()(我认为不应该),但可

  • 作为参考,我们有两个选项可以让Multipart与Spring Security一起工作: 在Spring Security之前放置MultipartFilter 在行动中包括CSRF令牌 对于第一种选择,我尝试了以下方法: 创建安全应用程序初始化类: > 在application.properties中更改Spring安全过滤器顺序 然后添加FilterRegistrationBean 不幸的是

  • 我有一个烧瓶服务器运行在http://127.0.0.1:5000和一个vuejs前端运行http://localhost:8080我已经做了api,并用postman测试了它,一切都如预期的那样工作:( > 将请求发布到/登录- (将请求发送至/登录)- 烧瓶API代码: 登录。vue: 指数vue 当我使用邮递员登录时,我得到的响应为;当我使用邮递员获取url/索引时,我得到响应。数据但当我使

  • 我想了解更多这句话发现Grails留档: 如果测试涉及证明测试对象以特定方式与合作者交互,则使用模拟。如果协作者以某种方式的行为暴露了测试主题中的特定行为,那么该行为的结果就是您正在测试的,请使用存根