Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache key collision when using AbstractRoutingDatasource #1541

Open
lohoso opened this issue May 16, 2019 · 5 comments
Open

Cache key collision when using AbstractRoutingDatasource #1541

lohoso opened this issue May 16, 2019 · 5 comments

Comments

@lohoso
Copy link

lohoso commented May 16, 2019

Hi,
I have raised the issue in google group as below, mybatis cache can not work for multi-tenant environment, https://groups.google.com/forum/#!topic/mybatis-user/n3HxAd8YQR4,

We need to process multiple clients in our project, so we use AbstractRoutingDataSource ,we enable global cache in xml file, when different clients process the same sql clause, the 2nd client db connection doesn't read from database, it directly get data from cache put by the 1st client query, is there any way to create cache for different client, I think we now use BaseExecutor->createCacheKey to create cache key, can we pass client id into the cache key?

@harawata harawata added the enhancement Improve a feature or add a new feature label May 16, 2019
@binakot
Copy link

binakot commented Jun 14, 2019

Hello, @lohoso!
Did you do something with your issue? I'm interesting in similar problem too.

@lohoso
Copy link
Author

lohoso commented Jun 19, 2019

Hello, @lohoso!
Did you do something with your issue? I'm interesting in similar problem too.

@binakot, No, we disable cache currently.

@harawata
Copy link
Member

Hi @lohoso ,

I have been thinking about your use case.
And my conclusion is that adding client ID to CacheKey is not a good solution.

Your idea is to manage caches for all clients in a single cache store, right?
With this approach, however, the cache of all clients will be flushed when insert/update/delete is executed on any client.
This may not be the expected behavior.

The right solution would be to manage a separate cache store for each client and you may be able to write a custom Cache implementation to achieve it (e.g. choosing the right cache store by checking the ThreadLocal value in putObject(), getObject(), etc.).

Please let me know if you have difficulty with writing a custom cache.

@harawata harawata added waiting for feedback and removed enhancement Improve a feature or add a new feature labels Aug 29, 2019
@lohoso
Copy link
Author

lohoso commented Oct 9, 2019

Hi @lohoso ,

I have been thinking about your use case.
And my conclusion is that adding client ID to CacheKey is not a good solution.

Your idea is to manage caches for all clients in a single cache store, right?
With this approach, however, the cache of all clients will be flushed when insert/update/delete is executed on any client.
This may not be the expected behavior.

The right solution would be to manage a separate cache store for each client and you may be able to write a custom Cache implementation to achieve it (e.g. choosing the right cache store by checking the ThreadLocal value in putObject(), getObject(), etc.).

Please let me know if you have difficulty with writing a custom cache.

OK, thanks @harawata , I will try it, I think we need to note this somewhere that mybatis cache cannot work directly with multiple data source.

@harawata harawata changed the title cache issue for multiple client datasouces query Cache key collision when using AbstractRoutingDatasource Oct 9, 2019
@harawata
Copy link
Member

harawata commented Oct 9, 2019

Hi @lohoso ,

Cache works fine with multiple data sources.
AbstractRoutingDatasource, technically, is a single data source and it makes cache handling difficult.
I found a blog entry discussing the same issue except the author uses Hibernate.
http://blog.fennek-it.nl/2012/10/spring-hibernate-and-multiple.html

I am still thinking about it and considering the purpose of AbstractRoutingDatasource, it might be OK to flush the entire cache when INSERT/UPDATE/DELTE is called.
Is that OK for you?
What kind of data you are trying to cache?

Does anyone have any insight on this subject?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants