Search code examples
springcursormybatistransactional

Why my Cursor is empty?


I'm trying to use MyBatis' Cursor.

Mapper XML

<select id="selectCursor51" resultMap="someResultMap" resultOrdered="true">
    SELECT
        ...
    FROM
        ...
    <where>
        ...
    </where>
    ORDER BY
        ..., <!-- is this part can be wrong? -->
        some_id ASC
</select>

Mapper interface,

@Mapper
public interface SomeMapper {
    List<Some> selectCursor51(...);
}

Spring Service,

@Service
public class SomeService {

    @Transactional
    public <R> R some(..., final Function<Cursor<Some>, R> function) {
        ...
        final Cursor<Some> cursor = someMapper.selectCursor51(...);
        return function.apply(cursor);
    }

    @Autowired
    private SomeMapper someMapper;
}

The actual query yields non empty results. But the cursor is empty.

What did I do wrong?


Solution

  • I'm answering for my own quest with updates.

    The method actually works.

    The problem is that there are more methods directly or indirectly calls the method.

    @Transactional
    public <R> R some(..., final Function<Cursor<Some>, R> function) {
        return function.apply(someMapper.selectCursor51(...));
    }
    
    // not annotated with @Transactional
    public void some(..., final Consumer<Some> consumer) {
        some(
            ...,
            cursor -> {
                for(Some some: cursor) {
                    consumer.accept(some);
                }
            }
        );
    }
    

    When, say a Controller, invokes the some with a function which is annotated with @Transactional it works.

    When it invokes the some method with a consumer, it doesn't.

    I found @Transactional method calling another method without @Transactional anotation? and solved my problem.