Making spring-data-mongodb multi-tenant

后端 未结 3 1008
野趣味
野趣味 2020-11-27 17:29

In a post last august sbzoom proposed a solution to make spring-data-mongoDB multi-tenant:

\"You have to make your own RepositoryFactoryBean. Here is the example fro

3条回答
  •  天涯浪人
    2020-11-27 18:11

    I had a similar approach to Oliver Gierke. At least on database-level. https://github.com/Loki-Afro/multi-tenant-spring-mongodb You should be able to do things like this:

            MultiTenantMongoDbFactory.setDatabaseNameForCurrentThread("test");
            this.personRepository.save(createPerson("Phillip", "Wirth", ChronoUnit.YEARS.between(
                    LocalDate.of(1992, Month.FEBRUARY, 3),
                    LocalDate.now())));
    
            System.out.println("data from test: " + this.personRepository.findAll());
    //        okay? fine. - lets switch the database
            MultiTenantMongoDbFactory.setDatabaseNameForCurrentThread("test666");
    
    //        should be empty
            System.out.println("data from test666: " + this.personRepository.findAll());
    

提交回复
热议问题