Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/victorhsr/ttxn

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
https://github.com/victorhsr/ttxn

aspect-oriented-programming aspectj jpa-hibernate library multitenancy multitenant plugin postgresql spring spring-boot spring-data spring-framework tenant tenant-management tenant-resolver transaction transaction-manager transactional

Last synced: 4 months ago
JSON representation

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

Awesome Lists containing this project

README

        

![Logo](./ttxn.png)

![Build Status](https://travis-ci.com/victorhsr/TTXN.svg?branch=master)

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:
```Java
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {
}
```
After that, we have to expose the `TenantTransactionAOP` bean with the strategy that your project need:
```Java
@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
```Java
@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