apache / apache/grails-core

Multi tenancy with multiple datasources is not working

Open
#14,357 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

If I add "implements MultiTenancy" to the domain class, it tries to use default datasource instead of the one defined in the datasource mapping field and then it returns a table not found error.

If I remove "implements MultiTenant" or remove the multiple databases setup, everything works fine.

```
class SystemTransaction implements MultiTenant {
static mapping = {
datasource 'accountTransaction'

18:51:05.0222 [http-nio-8080-exec-9] DEBUG o.s.j.d.DataSourceUtils - Fetching JDBC Connection from DataSource
18:51:05.0239 [http-nio-8080-exec-9] DEBUG o.h.e.j.s.SqlExceptionHelper - could not extract ResultSet [n/a]
java.sql.SQLSyntaxErrorException: Table 'gaming.system_transaction' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:118)
at jdk.proxy2/jdk.proxy2.$Proxy153.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2322)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2075)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2037)
at org.hibernate.loader.Loader.doQuery(Loader.java:956)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357)
at org.hibernate.loader.Loader.doList(Loader.java:2868)
at org.hibernate.loader.Loader.doList(Loader.java:2850)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2682)
at org.hibernate.loader.Loader.list(Loader.java:2677)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1917)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:370)
at org.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder.invokeMethod(AbstractHibernateCriteriaBuilder.java:1699)
at org.grails.orm.hibernate.query.AbstractHibernateCriteriaBuilder.list(AbstractHibernateCriteriaBuilder.java:1570)
at org.grails.datastore.mapping.query.api.BuildableCriteria$list$0.call(Unknown Source)

```

Contributor guide

Open the contributing guide

Research direction

Start with the SystemTransaction domain class and its static mapping for the accountTransaction datasource, then reproduce the MultiTenancy setup with multiple datasources. Trace why the query selects the default datasource; done means the multi-tenant query uses accountTransaction and no longer reports that gaming.system_transaction is missing.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, mysql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.