2011-10-16 1 views
1

내 서비스 클래스의 init 메소드에서 DB의 일부 데이터를로드하려고하지만 "getResultList()"메소드를 호출하면 Exception을 throw합니다. "세션이 닫혔습니다".init-method의 getResultList()가 "세션 닫힘"

내 applicationContext.xml

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  
<bean id="testService" class="com.impl.TestServiceImpl" init-method="init" /> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

내 서비스 클래스 :이 오류 메시지

public Class TestServiceImpl implements TestService { 
private EntityManager entityManager; 

@PersistenceContext 
public void setEntityManager(EntityManager entityManager) { 
    this.entityManager = entityManager; 
} 

public void init() { 
    Query query = entityManager.createQuery("from myTable"); 
    query.getResultList(); // this causes error... 
} 
} 

입니다

:

SEVERE: Exception sending context initialized event to listener instance of class 
org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'testService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
Invocation of init method failed; nested exception is 
javax.persistence.PersistenceException: org.hibernate.SessionException: Session is 
closed! 
Caused by: javax.persistence.PersistenceException: org.hibernate.SessionException: 
Session is closed! 
at 
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630) 

그래서 내가 잘못 여기서 뭐하는거야? 이 문제를 어떻게 해결할 수 있습니까? 감사. 모든 TestServiceImpl

답변

2

첫째는 @Transactional로 주석되지 않지만이 경우에도, 그것을 참조 작동하지 않을 것입니다 : Transactional init-methodSPR-2740 -이 설명 디자인입니다.

init() 메서드를 사용하여 @Transactional이라고 표시된 다른 bean의 비즈니스 메서드를 호출하는 것만 가능합니다.

private TestDao testDao; 

public void init() { 
    testDao.findAll(); 
} 

그리고 TestDao 콩에서 : 대답에 대한

private EntityManager entityManager; 

@Transactional 
public findAll() { 
    Query query = entityManager.createQuery("from myTable"); 
    return query.getResultList(); 
} 
+0

감사합니다, 호출 다른 빈의 비즈니스 메소드가 이미 서비스 클래스에 @Transactional 있었다 그런데, 일한 (에 넣어하는 것을 잊지 질문)하지만 거기에 있더라도 언급했듯이 그것은 효과가 없었을 것입니다. – Rizwan