简体   繁体   English

为特定的mybatis-spring映射器设置不同的ExecutorType

[英]Set different ExecutorType for a specific mybatis-spring mapper

I have a problem using mappers in mybatis-spring. 我在mybatis-spring中使用映射器时遇到问题。 (Spring Batch) I need to use a SqlSessionTemplate with ExecutorType in BATCH mode for performance issues (my program must execute thousands of insert statement in a table). (Spring Batch)我需要在BATCH模式下使用带有ExecutorType的SqlSessionTemplate来解决性能问题(我的程序必须在表中执行数千个insert语句)。 However in my program I need to log errors and updating states in another table of the database and if something goes wrong in the execution of the current step everything is rollback, included the logs, which is not an acceptable behaviour. 但是在我的程序中,我需要记录错误并更新数据库的另一个表中的状态,如果在执行当前步骤时出现问题,则所有内容都是回滚,包括日志,这是不可接受的行为。 I thought I could simple set two different SqlSessionTemplate with differents ExecutorType, but if in my step I use two mappers with different templates I get an exception which says that I can't change ExecutorType during transaction, but I don't know how to solve this issue. 我以为我可以简单地设置两个不同的SqlSessionTemplate与不同的ExecutorType,但如果在我的步骤中我使用两个具有不同模板的映射器我得到一个异常,说我不能在事务期间更改ExecutorType,但我不知道如何解决这个问题。 Any help is appreciated. 任何帮助表示赞赏。 Here some XML configuration. 这里有一些XML配置。

<!-- 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>

Here i have two mappers, one I'd like to use in BATCH mode, the other in SIMPLE mode. 这里我有两个映射器,一个我想在BATCH模式下使用,另一个在SIMPLE模式下。 How can I accomplish this task? 我怎样才能完成这项任务? Every suggestion is appreciated. 每个建议都表示赞赏。 Thanks in advance, and sorry for my bad english. 提前谢谢,抱歉我的英语不好。

After a lot of tries, I decided to change my approach to solve this problem. 经过多次尝试,我决定改变我的方法来解决这个问题。 I defined programmatically a new SqlSessionFactory, generating a new SqlSession with the Batch Executor and I used that one. 我以编程方式定义了一个新的SqlSessionFactory,使用Batch Executor生成一个新的SqlSession并使用了那个。 Since it is an entirely different SqlSessionFactory, it seems it doesn't give problem if I use 2 differents ExecutorType. 因为它是一个完全不同的SqlSessionFactory,如果我使用2个不同的ExecutorType,它似乎没有问题。 Here a sample working code: 这是一个示例工作代码:

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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM