Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 2.04 KB

README.md

File metadata and controls

50 lines (38 loc) · 2.04 KB

Logo

Build Status

Tenant Transaction is an almost zero-configuration and lightweight plugin for spring framework that allows to change the tenant that will be used in any transaction/queries dynamically. It still keep all the features of spring's @Transactional annotation

Quickstart

First of all, the tenant transaction is build on top of AspectJ, so we need to allow them:

    @Configuration
    @EnableAspectJAutoProxy
    public class AspectConfig {
    }

After that, we have to expose the TenantTransactionAOP bean with the strategy that your project need:

    @Configuration
    public class TenantTransactionConfiguration {

        @PersistenceContext
        private EntityManager entityManager;

        @Bean
        public TenantTransactionAOP getAop(@Value("${myapp-default-schema}") final String defaultSchema){

            final TenantTransactionHandler ttxnHandler = new PostgresqlChangeSchemaTtxnHandler(this.entityManager);
            return new TenantTransactionAOP(ttxnHandler, defaultSchema);
        }

    }

Usage

Similar to using @Transactional, we need to annotate the transactional method with @TenantTransaction and additionally, mark @TenantWrapperIdentifier which parameter contains the tenant to be used

    @Repository
    public class SomeRepository {
        
        @TenantTransaction
        public void persist(final @TenantWrapperIdentifier TenantWrapper tenantIdentifier, final Entity entity) {
           // do persist
        }

    }

Strategies

  • PostgresqlChangeSchemaTtxnHandler the Postgresql supports multiple schemas in the same database, and this strategy take advantage of this feature and switch between schemas to execut the commands in database

  • MysqlChangeDatabaseTtxnHandler the Mysql treats the database and schema as one, so, in this strategy the handler make a switch between databases to execut the commands