Access multiple namespaces within one thread
See original GitHub issueIs your feature request related to a problem? Please describe. Hey - I am trying ti figure out how to access multiple namespaces within on thread. I have a spring boot set up and when a request is coming in, I need to access multiple namespaces to process it. In the first step I need to retrieve data from one namespace based on the incoming request. This is information is then used to process the request and persist it in a different namespace.
Describe the solution you’d like I would like to be able to annotate an entity object with a specific namespace where it “lives”
@Entity
@Namespace("someNamespace")
public class Document {
String name;
}
Describe alternatives you’ve considered I’ve looked into the suggestion from this issue: https://github.com/spring-attic/spring-cloud-gcp/issues/2204 But this solution seems to only work if you use the same namespace within a thread. We would like to switch namespaces based on the entity we are using.
Additional context None
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Right! Datastore does not have a concept of a logical grouping of different Kinds (this is what a “namespace” would be with, say, Oracle). Some applications use different GCP projects to separate applications, but for your use-case this will pose the same difficulties as trying to access a different namespace – you’ll have to maintain multiple client library
Datastore
objects to connect to different projects.I don’t have good architectural advice here. It sounds like microservices with some common base data, so from purist standpoint they should not have access to the same database at all, but from a practical standpoint they often do, which is what brings you to having different datastore Kinds mixed in the same GCP project.
If application permissions are important to separate, then you’ll have to have separate projects. Datastore does not have grandular per-kind or per-namespace permissions (IAM reference).
Without specifying a namespace, all datastore entities get saved into the default namespace. I think the term “entity” might be creating confusion. A Datastore Entity is a single object, equivalent to a row in a relational database. A Spring Data Entity is a class corresponding to a table in a relational database, or a kind in Datastore. So you can have a Spring Data Entity corresponding to a Datastore Kind with instances of the entity mapping to Datastore data in multiple namespaces. However, a Datastore entity (being a single object/row) will be stored in a specific namespace, and cannot change its namespace after creation. So, a Spring Data Entity corresponds to multiple Datastore entities that can be in multiple namespaces, but each Java object instance of the entity corresponds to a single Datastore entity in a single, fixed-at-creation namespace.
From the Spring Cloud GCP perspective, you can typically get any custom requirement implemented by dropping down to the client library level. In your case, you could create 3 different
com.google.cloud.datastore.Datastore
objects going against different namespaces like this:In principle, you could also construct higher level Spring Cloud GCP objects by combining these client library objects with autowired helper objects, following how it’s done in autoconfiguration.
A word of caution, though – if namespaces A, B and C all have different data (kind) structures, then it’s really not what namespaces were designed for. Namespaces are for splitting data between different users of the same data structures. You will likely run into other cases where using namespaces to split data into “databases” will cause friction.