执行JPA操作的er/Spring MVC拦截器/线程等
有关多租户方法的更多详细信息,请访问我的博客@Oliver Gierke。您对此有何建议,谢谢@奥利弗·吉尔克,你有什么建议吗,谢谢!粗略地看一下,这样我就需要更新100-200服务层来添加事务注释,这是我试图避免的。相反,我要做的是添加两个新类来完成这项工作。此外,我们正在使用HikariCP数据源,不确定我是否可以使用上述方法来派生Spring JDBC类。所以在这种情况下,我需要在这样的服务方法上使用write datasource。粗略地看一下,这样我需要更新100-200个服务层来添加事务注释,这是我试图避免的。相反,我要做的是添加两个新类来完成这项工作。此外,我们正在使用HikariCP数据源,不确定我是否可以使用上述方法来派生Spring JDBC类。所以在这种情况下,我需要在这些服务方法上使用write-datasource。谢谢您的回复。然后,我想我需要为不同的存储库注册不同的实体管理器,也就是说,我将为执行任何写操作的存储库定义写数据源(写数据源也可以执行读),并为执行读数据源的存储库定义读数据源。我想那将是一个更容易的任务。谢谢你的回复。然后,我想我需要为不同的存储库注册不同的实体管理器,也就是说,我将为执行任何写操作的存储库定义写数据源(写数据源也可以执行读),并为执行读数据源的存储库定义读数据源。我想那将是一项更容易的任务。执行JPA操作的er/Spring MVC拦截器/线程等,spring,spring-boot,spring-data,spring-data-jpa,hikaricp,Spring,Spring Boot,Spring Data,Spring Data Jpa,Hikaricp,有关多租户方法的更多详细信息,请访问我的博客@Oliver Gierke。您对此有何建议,谢谢@奥利弗·吉尔克,你有什么建议吗,谢谢!粗略地看一下,这样我就需要更新100-200服务层来添加事务注释,这是我试图避免的。相反,我要做的是添加两个新类来完成这项工作。此外,我们正在使用HikariCP数据源,不确定我是否可以使用上述方法来派生Spring JDBC类。所以在这种情况下,我需要在这样的服务方法上使用write datasource。粗略地看一下,这样我需要更新100-200个服务层来添加
...
multitenancy:
dvdrental:
dataSources:
-
tenantId: readonly
url: jdbc:postgresql://172.16.69.133:5432/db_dvdrental
username: user_dvdrental
password: changeit
driverClassName: org.postgresql.Driver
-
tenantId: write
url: jdbc:postgresql://172.16.69.133:5532/db_dvdrental
username: user_dvdrental
password: changeit
driverClassName: org.postgresql.Driver
...
...
@Configuration
@EnableConfigurationProperties({ MultiTenantDvdRentalProperties.class, JpaProperties.class })
@ImportResource(locations = { "classpath:applicationContent.xml" })
@EnableTransactionManagement
public class MultiTenantJpaConfiguration {
@Autowired
private JpaProperties jpaProperties;
@Autowired
private MultiTenantDvdRentalProperties multiTenantDvdRentalProperties;
...
}
...
@Configuration
@ConfigurationProperties(prefix = "multitenancy.dvdrental")
public class MultiTenantDvdRentalProperties {
private List<DataSourceProperties> dataSourcesProps;
// Getters and Setters
public static class DataSourceProperties extends org.springframework.boot.autoconfigure.jdbc.DataSourceProperties {
private String tenantId;
// Getters and Setters
}
}
...
public class MultiTenantJpaConfiguration {
...
@Bean(name = "dataSourcesDvdRental" )
public Map<String, DataSource> dataSourcesDvdRental() {
...
}
...
}
...
public class MultiTenantJpaConfiguration {
...
@Bean
public MultiTenantConnectionProvider multiTenantConnectionProvider() {
...
}
@Bean
public CurrentTenantIdentifierResolver currentTenantIdentifierResolver() {
...
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(MultiTenantConnectionProvider multiTenantConnectionProvider,
CurrentTenantIdentifierResolver currentTenantIdentifierResolver) {
...
}
...
}
...
public class MultiTenantJpaConfiguration {
...
@Bean
public EntityManagerFactory entityManagerFactory(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
...
}
@Bean
public PlatformTransactionManager txManager(EntityManagerFactory entityManagerFactory) {
...
}
...
}
...
<jpa:repositories base-package="com.asimio.dvdrental.dao" transaction-manager-ref="txManager" />
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />
...
public interface ActorDao extends JpaRepository<Actor, Integer> {
}
...
@Autowired
private ActorDao actorDao;
...
// Read feature
...
DvdRentalTenantContext.setTenantId("readonly");
this.actorDao.findOne(...);
...
// Or write
DvdRentalTenantContext.setTenantId("write");
this.actorDao.save(...);
...