-
Notifications
You must be signed in to change notification settings - Fork 41
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
Wrong schema for connection to read-instance when using schema based multi tenancy from Hibernate #679
Comments
Hello @matheisco , Let's quickly take a look of what is happening here and see how we can resolve this issue. It seems that you are assigning the schema in the The solution for the issue would be to find a way to pass the schema object along to the internal Once the custom extension of the Please let us know if you have any more questions. |
Hi @brunos-bq, Thanks for the detailed answer! I gave it a try and it doesn't seem to behave in the desired way. The reader instance seems to be never used:
Even when we fix this error, there seems to be a more fundamental problem: when the query hits the writer instance in this situation, the selected schema is wrong.
So it's crucial that every thread in the application executes This is the implementation I used for testing:
Registering with ConnectionProviderManager:
|
Hello @matheisco, We have looked at the sample code you have posted above, and we came back with a couple of updates. We believe there are now two distinct manners to solve your issue. The first one is to continue with the idea of creating a custom ConnectionProvider. The code sample that we believe would help you to achieve what you want would look more or less like this:
We suggest you to give it a try and to check if that solution gives the expected result. The second possibility, would be to use a new ConnectionInitFunc function. This is something that we came up with while studying this issue. In order to try this solution, you would have to checkout our snapshot build. |
Thanks @brunos-bq, I tested both options and couldn't make it work properly. The first problem (maybe unrelated) is an error I'm getting each time after an SQL statement was executed (I tested with readonly statements):
The second problem is that sometimes the I can also see that the The most important thing for me is that the schema is set dynamically on every SQL statement that is executed. EDIT:
By removing those, the exception doesn't occur anymore, which also makes it easier to reproduce that |
Hello @matheisco, Thanks for your reply. The idea behind |
Hi @brunos-bq, my response was imprecise, with the multi tenancy implementation we had up until now, So I was hoping to replicate this behaviour with the I'm now evaluating a different strategy: defining two DataSources, both pointing to the same database, but with a different |
After reading more through the documentation, I was also wondering if you have general guidelines about the plugins which should be activated alongside the I was wondering if that is intentional (eg. That's maybe a bit off-topic, but any pointer is appreciated 😅 |
Hello @matheisco, Thank you for reaching out. All the plugins of the AWS JDBC Driver are meant to be compatible with each other. If there was a case of an incompatibility between two plugins, there would be a strong warning indicating it in the plugin documentation. The examples in our repository are basic samples to guide users on how to setup or enable a plugin or feature, but do not represent a 'proper' or 'default' manner on how to use the feature/plugin. With that in mind, it is absolutely fine to have Please do not hesitate to reach out if you are still uncertain about some functionality of the AWS JDBC Driver. By the way, did the solution you mentioned two messages ago was successful? |
Thanks for the clarification! The solution seems successful. For other people coming across this topic: I implemented an AbstractRoutingDataSource, there is a good explanation here. When performance testing, I came across a performance degradation with the readWriteSplitting plugin, which is also present when removing any multitenancy code from the project, so I will create a separate issue for that. Thanks for your support! |
Describe the bug
In our Spring Boot application we use schema-based multi tenancy from hibernate. When activating the readWriteSplitting plugin, the connection to the read-instance doesn't get the correct schema set.
Expected Behavior
The overridden
ConnectionProvider.getConnection()
method should also be executed for the read-instance, and not only for the write instance.What plugins are used? What other connection properties were set?
readWriteSplitting,failover,efm
Current Behavior
connection.setSchema(schema);
is only executed for the write-instance. Therefore, when the aws wrapper uses the read-instance, thepublic
schema is used, which is not correct.Reproduction Steps
We instantiate two
TenantBasedConnectionProvider
, each with a differentschema
:And here the correct
TenantBasedConnectionProvider
is selected, depending on which schema should be used for the current request:The connection to the DB is defined in the application.properties like this:
Possible Solution
No response
Additional Information/Context
No response
The AWS Advanced JDBC Driver version used
same behaviour with 1.0.1 and 2.2.5
JDK version used
17.0.6
Operating System and version
macOS 13.2.1
The text was updated successfully, but these errors were encountered: