我有一个如此定义的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
}
}
}
}
问题是该程序只是冻结!而且我确信这不是我的逻辑错误,因为程序通过注释插入行成功结束。 所以我猜这是第二个表上的并发问题,即我插入数据的那个。