Skip to content

Cache is still used when setting useCache as false #3309

Closed
@iscy

Description

@iscy

MyBatis version

3.5.17

Database vendor and version

PostgreSQL 16.4

Steps to reproduce

  1. Enable the statement logs in Postgresql
  2. Set @Options(useCache = false) on a read query
  3. Create a single SqlSession and invoke that query multiple times

Expected result

You should be able to see the query in the statement logs of Postgresql

Actual result

The result of the query will be coming from the cache even though it's supposed to be disabled

Explanation

We have a use case where we simply want to ensure that our query gets to the database every time. We don't want to flush the cache. We simply want to control whether the query hits the database or the "cache or database". While investigating, we noticed where the issue is:

CachingExecutor.java:99 has the right logic
BaseExecutor.java:154 is where the problem is

Caching is only allowed when useCache is true and no resultHandler have been specified. CachingExecutor properly checks for both conditions. However, BaseExecutor doesn't currently check useCache. Adding this simple check fixes the problem.

There are also other "reports" of this issue on stackoverflow:
https://stackoverflow.com/a/26487839

The current workaround is to flush the cache, which is not optimal for many use cases.

PR will be coming shortly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions