Search code examples
mybatisspring-mybatis

Is there a way to get the current executor type?


I'm trying to make a batch insert with the for each method, I get an exception telling me that I cannot change the running executor type. This is very strange as I have an SQL session so defined:

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

Either way I'd like to check what's the current executor type to make sure it is a BATCH one. How to do that? I see no method in SQLSession...


Solution

  • In your example sqlSession is of type SqlSessionTemplate and it has getExecutorType() method so you just need to use correct type and have several options to do that

    1. inject SqlSessionType instead of plain SqlSession to the service where you need access to executor type
    2. cast SqlSession to SqlSessionTemplate and then use getExecutorType
    3. inject SqlSessionType's property use SPEL to the service like this:

    @Value("#{sqlSession.executorType}") private ExecutorType executorType;