Reputation: 2076
In Spring Data - Cassandra (SDC), I want to reuse the same DataStax driver Session, but access different Keyspaces depending on which tenant (in a multi-tenant SaaS model) is accessing Cassandra at the moment. That said, it appears in my research that adding the keyspace to make fully-qualified queries is preferred way to accomplish this.
Does SDC have hooks to do this? I'm looking for something similar to Hibernate's multi-tenancy support. In Hibernate, it can reuse the same Connection pool. The app provides a way to tell Hibernate which tenant is in the current context and can control how Hibernate's Session connects - possibly a different schema per tenant by calling "SET SCHEMA 'foo_tenant_schema' " on the JDBC connection or something.
If not, I need to write it myself. That's fine. I'm thinking I'd load a Map of "tenantID-to-keyspace" and somehow dynamically append it to make a fully-qualified call. I'm not sure how this would affect PreparedStatements of the DataStax Session, etc.
If there are any thoughts on a better way, please do tell.
Upvotes: 0
Views: 466
Reputation: 689
You can definitely do the way you are doing, the other option is to execute the following before executing the actual query.
cassandraTemplate.execute("use " + keySpaceName + ";");
You can combine the above query with your actual query in batch as well. This way you do not need to append anything in the existing query. The assumption here is that all the keyspaces are on the same cluster.
Upvotes: 1