복붙노트

[SPRING] CreateQuery가 활성 트랜잭션없이 유효하지 않습니다.

SPRING

CreateQuery가 활성 트랜잭션없이 유효하지 않습니다.

Spring @ Transactional annotation을 사용하려고하는데 findAll 메서드가 호출 될 때 문제가 발생합니다.이 오류가 있습니다.

  org.hibernate.HibernateException: createQuery is not valid without active transaction
  at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
  at com.sun.proxy.$Proxy57.createQuery(Unknown Source)
  at org.munaycoop.taskmanager.daos.PersonDataAccesObject.findAll(PersonDataAccesObject.java:20)
  at org.munaycoop.taskmanager.services.PersonService.findAll(PersonService.java:26)
  at org.munaycoop.taskmanager.controllers.PersonController.ShowAllPersons(PersonController.java:20)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
  at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
  at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
  at org.mortbay.jetty.Server.handle(Server.java:326)
  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
  at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

DAO 사람 :

@Repository
@Transactional
public class PersonDataAccesObject implements IPersonDataAccesObject {

    @Autowired
    private SessionFactory session;

    @Override
    public List findAll() {
        return session.getCurrentSession().createQuery("from persons").list();
    }

이것은 내 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/bootstrap/" />

<!-- Database Configuration -->
<context:property-placeholder location="classpath:database.properties" />

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="${jdbc.driverClassName}" />
    <beans:property name="url" value="${jdbc.databaseUrl}" />
    <beans:property name="username" value="${jdbc.username}" />
    <beans:property name="password" value="${jdbc.password}" />
</beans:bean>

<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>

    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.connection.pool_size">${hibernate.pool_size}</beans:prop>
            <beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
            <beans:prop key="hibernate.current_session_context_class">${hibernate.current_session}</beans:prop>
            <beans:prop key="hibernate.cache.provider_class">${hibernate.provider_class}</beans:prop>
            <beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl}</beans:prop>
        </beans:props>
    </beans:property>

    <beans:property name="mappingResources">
        <beans:list>
            <beans:value>hibernate.cfg.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<!-- Hibernate TransactionManager -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>

{bean : prop key = "hibernate.current_session_context_class"> $ {hibernate.current_session} 이 줄을 지우려고했으나 또 다른 오류가 발생했습니다.

해결법

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

    1.트랜잭션 관리자 주석에 대한 지원을 명시 적으로 선언해야합니다.

    트랜잭션 관리자 주석에 대한 지원을 명시 적으로 선언해야합니다.

    설정에 추가 :

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

    tx는 xmlns : tx = "http://www.springframework.org/schema/tx"네임 스페이스입니다.

    이 행을 삭제하십시오.

    <beans:prop key="hibernate.current_session_context_class">${hibernate.current_session}</beans:prop>
    
  2. ==============================

    2.이 코드 삭제

    이 코드 삭제

    <beans:prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext </beans:prop>
    

    이 줄을 추가하십시오.

    <tx:annotation-driven transaction-manager="transactionManager"
      mode="proxy" proxy-target-class="true" />
    

    XML 파일에서 여기서 당신은 당신의 Hibernate 트랜잭션 속성을 설정한다.

  3. from https://stackoverflow.com/questions/26836560/createquery-is-not-valid-without-active-transaction by cc-by-sa and MIT license