1

我有一个这样定义的sql会话:

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
    <constructor-arg index="1" value="BATCH" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="..." />
</bean>

<bean id="taskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="25" />
</bean>

现在,在我的服务中,我必须在一个表上进行一些选择,并在另一个表中进行一些插入

@Transactional
public class MyWebService{
   @Autowired
   private TaskExecutor executor;
   @Autowired
   private SelectTableMapper sm;
   @Autowired
   private InsertTableMapper it;

   public void service(){
     int rowCount = ...//getting row count
     int batch = rowCount/numThreads;
     //computing an interval for each thread with non overlapping rows
     for(int i=0;i<numThread;i++)
        executor.execute(new MyTask(//interval//));

     ..waiting for all tasks and finally returning
   }

   private class MyTask implements Runnable{

         public void run(){
              List<Row> rows = sm.select(//interval//);
              for(Row row : rows){
                   if(//some condition//)
                       it.insert(row); //if I comment here it successfully completes

              }

         }

   }

}

问题是程序只是冻结了!而且我确信这不是我的逻辑的错,因为程序通过注释插入行成功结束。所以我猜这是第二张表的并发问题,我正在插入数据。

4

0 回答 0