sunitk / multitenancy-dynamic-tenant

Spring Boot 2 JPA Hibernate with database per tenant with dynamic configuration of tenants, secured by Spring Security
Apache License 2.0
99 stars 76 forks source link

no transaction is in progress by inserting #18

Closed paipeng closed 2 years ago

paipeng commented 2 years ago

Hello,

Thanks for sharing your project and it helps me lots on the solution for multitenancy.

But I was facing a runtime exception when trying to insert an object: no transaction is in progress. (When I set "allow_update_outside_transaction: true", it works )

Reading works fine.

After some google, I find a similar problem on stackoverflow, with his solution I can now do inserting within transaction. https://stackoverflow.com/questions/58102106/jpa-hib-not-saving-to-database-but-reading-db-mysql-hikari-tomcat-9-spring

`

@Bean(name = "tenantTransactionManager")
public JpaTransactionManager transactionManager(
        @Qualifier("tenantEntityManagerFactory") EntityManagerFactory tenantEntityManager) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(tenantEntityManager);
    return transactionManager;
}

` My env: Java 8; Spring Boot 2.6.6; HikariCP 4.0.3 with MySQL 8.0 under MAC OSX

I hope it can help others to save time on this issue.

Regards,

Pai

sunitk commented 2 years ago

Thank you for sharing this solution when using Spring Boot.