2012-01-17 4 views
2

를 찾지 세션, 반환 (참고 : 하지 Grails의를), 나는 내가 No session found for current thread 때 받고 있어요 것을 찾는거야 시작시 일부 @Transactional 작업을 수행하려고합니다.그루비/최대 절전 모드/@Transaction 내가 봄/JPA/그루비/최대 절전 모드 스택이 현재의 thread

컨트롤러 클래스가 @PostConstruct 일 때 다른 클래스에서 @Transactional 메서드를 호출하여 시뮬레이션에 사용할 샘플 데이터를 데이터베이스에 채 웁니다. 당신이 볼 수 있듯이 이유 here 논의를 위해, 나는 @PostConstruct에서 순수 @Transactional 접근 방식에서 멀리 이동 한

@Component 
public class SimulationController { 

    private TransactionTemplate transactionTemplate; 
    @Autowired 
    private PlatformTransactionManager transactionManager; 
    @Autowired 
    private IPublisher publisher; 

    @PostConstruct 
    public void intialize() 
    { 
     this.transactionTemplate = new TransactionTemplate(transactionManager); 
     transactionTemplate.execute(new TransactionCallbackWithoutResult() { 
      @Override 
      protected void doInTransactionWithoutResult(TransactionStatus arg0) { 
       racePublisher.populateData(); 
      } 
     }); 
    } 
    // Also tried, with no success:  
    // @PostConstruct 
    // public void initialize() 
    // { 
    //  publisher.populateData(); 
    // } 
} 

:

다음은 controller 클래스입니다. 의 구현입니다

@Component 
class Publisher implements IPublisher { 

@Autowired 
IStockDAO stockDAO 

void populateData() 
{ 
    createStock() 
} 
@Transactional 
void createStock() 
{ 
    def list = [new Stock(ticker: "ADBE", name: "Adobe"), 
       new Stock(ticker: "MSFT", venueCode: "Microsoft")] 
    list.each { stockDAO.create it } 
} 

: 다음과 같이

IPublisher는, 그루비 클래스입니다

public interface IPublisher { 

    public void populateData(); 
    public void createStock(); 
} 

주, 나 또한 영향을 미치지 않고, @TransactionalpopulateData()을 표시 시도했습니다.

내 스프링 컨텍스트 클래스에서 <tx:annotation-driven/>을 정의하려고합니다.

내가 알 수있는 한, 나는 모든 것을 올바르게했습니다. 그러나, 나는 이것을 작동시킬 수 없다.

그 외 필요한 것은 무엇입니까? 업데이트

: 여기 내 DATAACCESS 관련 콩을 설정하는 콩과 같습니다

<beans> 
<bean id="transactionManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <tx:annotation-driven/> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://${database.host}:${database.port}/${database.name}" /> 
     <property name="username" value="${database.username}" /> 
     <property name="password" value="${database.password}" /> 
     <property name="initialSize" value="5" /> 
     <property name="maxActive" value="50" /> 
    </bean> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.mangofactory.concorde</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider 
       </prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop><!-- use create for full drop/create --> 
       <prop key="hibernate.connection.autocommit">true</prop> 
       <prop key="hibernate.statement_cache.size">0</prop> 
       <prop key="hibernate.jdbc.batch_size">20</prop> 
      </props> 
     </property> 
    </bean> 
    <beans> 
+0

트랜잭션 관리자는 어떻게 정의됩니까? – mrembisz

+0

@mrembisz 질문에 대한 선언을 추가했습니다. –

답변

3

DataSourceTransactionManager 대신 HibernateTransactionManager을 사용해야합니다.

+0

감사! 그거였다! 정말 고마워! –

+0

@mrembisz +1 간단한 것을 알아 차림) –

0

코드 기본적인 문제가 있습니다. 트랜잭션 경계는 Publisher 클래스의 createStock 메소드에서 시작하여 끝납니다. 그것은 귀하의 컨트롤러에 전파되지 않습니다. 트랜잭션 내에서 DB 쿼리 호출을 사용하십시오. 즉, Transactional 주석이있는 메소드 내부에서 수행됩니다. 그렇지 않으면 절대로 작동하지 않을 것입니다.

+0

여기서 무슨 말을하는지 잘 모르겠습니다. 트랜잭션 내에서 DB 쿼리 호출을 사용하고 있습니다.'create' 호출은'@ Transactional' 블록 안에 싸여 있습니다. –

+0

@MartyPitt 트랜잭션 템플릿 코드는 트랜잭션 경계 밖에 있습니다. intialize() 메서드에서는 작동하지 않습니다. 트랜잭션 주석이있는 메소드에서 작동합니다. –

+0

@AravindA'transactionTemplate.execute()'는'@ Transactional'과 동일한 API입니다. 이 코드는 IMO를 사용해야합니다. – mrembisz