복붙노트

[SPRING] Spring 3.1 및 Hibernate 4의 현재 스레드 예외에 대해이 No Session이 발견 된 이유

SPRING

Spring 3.1 및 Hibernate 4의 현재 스레드 예외에 대해이 No Session이 발견 된 이유

나는 봄 3.1과 최대 절전 모드를 설정하려고했다. 나는 간단한 테스트 프로젝트를 작성했다. 사용자 도메인과 userDao 및 해당 구현이있는 곳

applicationContext를 보여 드리겠습니다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.rokonoid.apps.user.dao"></context:component-scan>

<!-- Data Source -->

<context:property-placeholder location="classpath:/jdbc.properties" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan" value="com.rokonoid.apps.user.domain"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0</prop>
        </props>
    </property>
</bean>

UserDaoImpl의 일부

 @Repository 
public class UserDaoImpl implements UserDao {

@Autowired
private SessionFactory sessionFactory;

@Override
public User getUser(Long userId) {
    if (userId != null && userId > 0) {
        return (User) sessionFactory.getCurrentSession().get(User.class,
                userId);
    }
    return null;
}

@Override
public void updateUser(User user) {
    if (user != null) {
        sessionFactory.getCurrentSession().update(user);
    }
}

괜찮아 보인다. 그러나 테스트 케이스를 실행하려고 할 때 예외가됩니다.

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)
at com.rokonoid.apps.user.dao.UserDaoImpl.saveUser(UserDaoImpl.java:197)
at com.rokonoid.apps.test.UserDaoTest.setUserTest(UserDaoTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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:49)
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)

모든 종류의 도움을 주시면 감사하겠습니다.

감사

편집하다:

이전 문제가 해결되었습니다. 또 다른 문제가 있습니다. 지금 나는지고있다.

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:328)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

해결법

  1. ==============================

    1.다음과 같이 전달을 "필수"로 설정해야합니다.

    다음과 같이 전달을 "필수"로 설정해야합니다.

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    

    이것이 스프링 버그인지는 모르겠다.

  2. ==============================

    2.샘플 프로젝트에는 트랜잭션이 없습니다. 더하다:

    샘플 프로젝트에는 트랜잭션이 없습니다. 더하다:

    <tx:annotation-driven />
    

    DAO에 @Transactional 주석을 달아주세요.

    @Transactional
    @Repository 
    public class UserDaoImpl implements UserDao
    

    두 가지 메모 :

  3. ==============================

    3.최신 항아리로 업데이트하십시오. 그들의 jira가 만들어졌습니다.

    최신 항아리로 업데이트하십시오. 그들의 jira가 만들어졌습니다.

    https://jira.springsource.org/browse/SPR-8776

  4. from https://stackoverflow.com/questions/9053095/why-this-no-session-found-for-current-thread-exceptions-in-spring-3-1-and-hibern by cc-by-sa and MIT license