2013-01-22 4 views
0

Hibernate 4.1.9Final과 함께 버전 3.2를 사용하여 Spring을 처음 접했을 때, @Transactional 주석이 무시되고있는 것처럼 보였다. 나는 컨트롤러 메소드, 서비스 메소드 및 DAO, 더 성공Spring 3.2 Hibernate 활성 트랜잭션 없음

나는

web.xml에 패키지를 포함되지했습니다

<?xml version="1.0" encoding="ISO-8859-1"?> 

<web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 
    <display-name> 
     Spring 
    </display-name> 
    <description> 
    Spring Test 
    </description> 

    <servlet> 
    <servlet-name>springapp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 



    <servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 





</web-app> 

springapp-servlet.xml에

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 

    <context:component-scan base-package="com.test.web.controllers,com.test.service.impl" /> 
    <context:annotation-config /> 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull"/> 
    <property name="username" value="medi"/> 
    <property name="password" value="tech"/> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="myDataSource"/> 
    <property name="mappingLocations" value="classpath*:com/test/model/hbm/**/*.hbm.xml" /> 

    <property name="hibernateProperties"> 
     <value> 
     hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 
     hibernate.show_sql=true 
     hibernate.current_session_context_class=thread 
     </value> 
    </property> 
    </bean> 

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



    <bean 

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


    <bean id="categoryDAO" class="com.test.dao.hibernate.HibernateCategoryDAO"> 
     <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
    <bean id="categoryService" class="com.test.service.impl.Categories" scope="singleton"> 
     <property name="dao" ref="categoryDAO"></property> 
    </bean> 

</beans> 

내 테스트 컨트롤러

public class HelloController { 

    @Autowired 
    private CategoryService categories; 

    public HelloController() { 
     System.out.println("test!!!"); 
    } 

    public void setCategoryService(CategoryService categories) { 
     this.categories = categories; 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    @Transactional 
    public String getIndex() { 
     Category c = new Category(); 
     c.setName("Test"); 
     categories.save(c); 
     return "index"; 
    } 
} 

스택 트레이스 :

org.hibernate.HibernateException: save is not valid without active transaction 
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348) 
    at $Proxy19.save(Unknown Source) 
    at com.test.dao.hibernate.HibernateCategoryDAO.save(HibernateCategoryDAO.java:20) 
    at com.test.service.impl.Categories.save(Categories.java:21) 
    at com.test.web.controllers.HelloController.getIndex(HelloController.java:36) 
    at com.test.web.controllers.HelloController$$FastClassByCGLIB$$aa12a3a3.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
+2

아! hibernate.current_session_context_class = thread와 그 작업 ...을 제거했습니다. – user979051

+0

답으로 쓰고 받아주세요. 그것은 미래에 다른 사람들에게 질문을 유용하게 만듭니다. –

답변

1

제거의 hibernate.current_session_context_class = 스레드를 찾을 수 및 작동처럼 <context:component-scan base-package="your package here">

이 보이는 추가 할 수 있습니다. Spring은 Spring 트랜잭션 지원 계층을 사용할 때 구현을 관리하는 자체 세션 컨텍스트를 주입합니까?

0

당신은 컨트롤러 레이어에 @Transactional을 사용하고 있습니다. 서비스 레이어에 있어야합니다.

Categories.save@Transactional으로 작성하면이 오류가 발생할 수 있습니다.

+0

컨트롤러 계층에서 사용하는 것이 잘못되지 않았다고 생각합니다. 특히 Unit-of-Work를 나타 내기 위해 Service를 사용하지 않는 것이 좋습니다. –

+0

설명해 드린대로 시도했습니다. 고맙습니다. 최대 절전 모드 속성에서 세션 컨텍스트 클래스 속성 선언을 제거하면 오류가 사라졌습니다. – user979051

관련 문제