2014-03-13 62 views
1

我在使用mybatis-spring中的映射器時遇到了問題。 (Spring批處理) 我需要在BATCH模式下使用帶ExecutorType的SqlSessionTemplate來解決性能問題(我的程序必須在表中執行數千個插入語句)。 但是在我的程序中,我需要記錄錯誤並更新數據庫的另一個表中的狀態,並且如果在當前步驟的執行中出現問題,則所有回滾都包含日誌,這不是可接受的行爲。 我以爲我可以簡單地設置兩個不同的ExecutorType的SqlSessionTemplate,但如果在我的步驟中我使用兩個具有不同模板的映射器,我得到一個異常,說我不能在事務中更改ExecutorType,但我不知道如何解決這個問題。 任何幫助表示讚賞。這裏有一些XML配置。爲特定的mybatis-spring映射器設置不同的ExecutorType

<!-- connect to database --> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> 
    <property name="targetDataSource"> 
     <ref local="mainDataSource" /> 
    </property> 
</bean> 


<bean id="mainDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > 
    <property name="driverClassName" value="${db.driver}" /> 
    <property name="url" value="${db.url}" /> 
    <property name="username" value="${db.user}" /> 
    <property name="password" value="${db.pass}" /> 
</bean> 

<bean id="infrastructureSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="mapperLocations" 
     value="classpath*:com/generali/danni/sipo/mdv/dao/mybatis/*Mapper*.xml" /> 
    <property name="configLocation" value="classpath:mybatis-config.xml" /> 
</bean> 

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

<bean id="infrastructureSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 
    <constructor-arg index="0" ref="infrastructureSqlSessionFactory" /> 
</bean> 

    <bean id="infrastructureAbstractMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" 
    abstract="true"> 
    <property name="sqlSessionTemplate" ref="infrastructureSqlSessionTemplate" /> 
</bean> 

<bean id="infrastructureAbstractMapperBatch" class="org.mybatis.spring.mapper.MapperFactoryBean" 
    abstract="true"> 
    <property name="sqlSessionTemplate" ref="infrastructureSqlSessionTemplateBatch" /> 
</bean> 

<bean id="erroriMapper" parent="infrastructureAbstractMapper"> 
    <property name="mapperInterface" 
     value="com.mdv.dao.ErroriMapper" /> 
</bean> 

<bean id="stagingFileMapper" parent="infrastructureAbstractMapperBatch"> 
    <property name="mapperInterface" 
     value="com.mdv.dao.StagingFileMapper" /> 
</bean> 

在這裏,我有兩個映射器,一個我想在批處理模式在簡單模式下使用,其他。 我該如何完成這項任務?每一個建議表示讚賞。 提前致謝,對不起我的英文不好。

+0

解決方案XML風格的配置可以在這裏找到http://stackoverflow.com/a/22383688/1239904 –

+0

是啊,其實我發現,設置很久以前,但它不是適合我們的應用程序需要批處理執行只爲少數映射器,而不是所有的人。僅僅使用xml我們無法實現這一點,因爲一旦設置了Spring,就不允許更改ExecutorType。 – Karura91

回答

3

經過很多嘗試,我決定改變我的方法來解決這個問題。 我以編程方式定義了一個新的SqlSessionFactory,用批處理執行器生成一個新的SqlSession,並使用該SqlSession。 由於它是一個完全不同的SqlSessionFactory,它似乎不會給出問題,如果我使用2個不同的ExecutorType。 這裏的樣本工作代碼:

Environment environment = new Environment("TEST", new JdbcTransactionFactory(), dataSource); 
     Configuration configuration = new Configuration(environment); 
     configuration.addMappers("com.mdv.dao"); 

     SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(configuration); 
     SqlSession sqlSession = ssf.openSession(ExecutorType.BATCH); 
     try { 

      StagingFileMapper sfm = sqlSession.getMapper(StagingFileMapper.class); 
      for(Record r : staging){ 
       StagingFile sf = new StagingFile(); 
       //set your sf fields 
       sfm.insert(sf); 
      } 
      sqlSession.commit(); 
     } catch (Exception e) { 
      //manage exception 
     } 
     finally{ 
      sqlSession.close(); 
     }