Multi-tenant capability with hierarchies

#1

Hello,

in this thread there was already the question about multi-tenant setups but only with an generel answer.

What we need are multiple tenants (would be best if every tenant has its own file/db) but also the possibility of hierarchies. So If a tenant is parent of another parent, he can access and display the data of the child. Also the other way around if a parent tenant provides basic data the child tenant should load and display those. The hierarchiy can be edited, so the database must be selected dynamically like getDBForTenant(String tenantId).

Is this possible or what would be the best way to implement this with objectDB?

We are using spring boot, is there an example on how to access two different db files or how to distinct tenants in one file from one application?

Thanks and regards

Kevin

#2

> Is this possible or what would be the best way to implement this with ObjectDB?

ObjectDB doesn't support queries on multiple databases, so it is possible, but if every tenant has its own database file, managing the hierarchy, etc. has to be implemented by your application, possibly with keeping relevant data in a master database that contains information on all the tenant databases.

Alternatively you can use one database for all the tenants.

> is there an example on how to access two different db files or how to distinct tenants in one file from one application?

The most up to date example of using ObjectDB with Spring Boot is posted here. You will have to configure the EntityManagerFactory creation to accept a parameter, if you want to open multiple databases, but currently we don't have an example that does that.

If you use one database (which may be much easier), then distinguishing between tenants should probably be done in the usual way, by associating objects in the database with specific tenants (e.g. by an extra tenant field in an ancestor entity class that all the other entity classes inherit from). Every query should restrict retrieval of data to the relevant tenants.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thanks for the answer and the link to the post of kirshamir.

I currently switch between databases using a Components getEnitityManager method before every db call. But then the @Transactional isn't working any more and I have to do the transaction management myself. Which figures because the @Transactional uses the entityManagerFactory bean which I don't use.

public EntityManager getEntityManger() {
    String tenantId = ThreadContext.getEnvironment();

    if (managerMap.containsKey(tenantId)) {
        return managerMap.get(tenantId);
    }
    else {
        EntityManager em = Persistence.createEntityManagerFactory("../db/" + tenantId + "-database.odb").createEntityManager();
        managerMap.put(tenantId, em);
        return em;
    }
}

I also still need to create the entityManagerFactory bean for startup or else I am getting an error "Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne", even if I don't use the bean because I get it from my Component.

@Bean(name = "entityManagerFactory")
public EntityManagerFactory getEntityManagerFactoryBean() {
    return Persistence.createEntityManagerFactory("../db/default-database.odb");
}

Is this how you meant > You will have to configure the EntityManagerFactory creation to accept a parameter?

Or is there a smoother way to switch the entityManagerFactory bean so I can still use the normal entityManager bean and the Transactional annotation?

#4

Possible way to go could be to move from a Singleton Bean to a Prototype Bean and retrieve it by name or by the other means Spring Core gives for retrieving beans. Once you have several databases you need to manage several EntityManagerFactory instances.

For the @Transactional annotation you may want to pass a transaction manager, see: https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#tx-multiple-tx-mgrs-with-attransactional. This stackoverflow post may also be relevant: https://stackoverflow.com/questions/48954763/spring-transactional-with-a-transaction-across-multiple-data-sources

But this topic is related to Spring Core / Spring Data more than to ObjectDB. If you are interested in further consulting please send a private message and we can send you contact details of an external consulting company that may help you with your specific needs.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login