You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for advice on a good way to handle schema-based multi-tenancy with Exposed. I'm getting tripped up because of Ktor and Coroutines and I'm coming from a Spring background where I would normally just use MultiTenantConnectionProvider from Hibernate.
Context:
Ktor
Postgres
Identical Schema per Tenant (managed by Flyway)
Schema name is derived from Auth
Connection pooling using Hikari
Here is my current solution:
importorg.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransactionsuspendfun <T> suspendedTransaction(schema:String, block: () ->T): T= newSuspendedTransaction(Dispatchers.IO) {
SchemaUtils.setSchema(schema =Schema(schema))
val result = block()
SchemaUtils.setSchema(schema = publicSchema)
result
}
Theoretically, this could work, too:
importorg.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransactionsuspendfun <T> suspendedTransaction(schema:String, block: () ->T): T= newSuspendedTransaction(Dispatchers.IO) {
connection.schema = schema
val result = block()
connection.schema = publicSchema.identifier
result
}
Questions:
Is there a difference between the above?
Is there a better way of doing what I'm trying to do?
Is what I'm currently doing "safe" in a Ktor server context?
Thanks!
The text was updated successfully, but these errors were encountered:
This looks a good approach to me. Another option could be having separate Database instance for all the tenant databases, but that would make connection pooling trickier and probably it would waste a lot of resources.
First approach looks more robust because it delegates the database specific switch to the framework, so if you switch from Postgres, you probably don't have to rewrite this part.
I'm looking for advice on a good way to handle schema-based multi-tenancy with Exposed. I'm getting tripped up because of Ktor and Coroutines and I'm coming from a Spring background where I would normally just use
MultiTenantConnectionProvider
from Hibernate.Context:
Here is my current solution:
Theoretically, this could work, too:
Questions:
Thanks!
The text was updated successfully, but these errors were encountered: