我对我对spring和java的了解感到沮丧,我希望有人能帮助我:)
我阅读了spring的文档并搜索了如何在hanacloud平台中实现多租户的示例,但没有人使用spring data/eclipseLink ...
我真的不知道框架中对象的生命周期如何以及如何正确配置以在spring中隔离租户
采用的多租户策略是用于隔离租户的列描述符,该列在子实体中映射的超类中描述...
每个消费者客户端都由 url 标识,并且可以使用此API进行访问,我创建了一个路由来测试租户 ID,并且每个 URl 的租户 ID 都不同,这意味着我的应用程序的订阅可以...
但是spring数据没有隔离数据,我猜他不是根据消费者的租户ID创建实体管理器和存储库,而是根据提供者的租户ID创建,想法是只创建一个持久性单元和数据源分开基于列描述符的数据,
租户的 ID 由系统生成,不可能创建额外的持久性单元,它们是在订阅时创建的......
这是问题
- 关于 LocalContainerEntityManagerFactoryBean,当他在堆栈中被调用时?
- 是否可以根据请求为每个租户 ID 创建一个实体管理器?
我的配置类来设置数据源
@Configuration
@Profile({ "neo" })
@EnableTransactionManagement
public class DataSourceNeoConfig {
@Bean
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
final JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("java:comp/env/jdbc/DefaultDB");
return dataSource;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);
return jpaVendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
try {
lef.setDataSource(jndiDataSource());
} catch (IllegalArgumentException | NamingException e) {
e.printStackTrace();
}
lef.setJpaVendorAdapter(jpaVendorAdapter());
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty(PersistenceUnitProperties.LOGGING_LEVEL, "FINE");
properties.setProperty("eclipselink.ddl-generation", "create-tables");
// eclipselink.tenant.id is column the identifier in mapped class
// @TenantDiscriminatorColumn(name = "TENANT_ID", contextProperty = "eclipselink.tenant.id", length = 36)
properties.setProperty("eclipselink.tenant.id", TenantHolder.getTenant());
lef.setPackagesToScan("com.strongit.models");
lef.setJpaProperties(properties);
lef.afterPropertiesSet();
return lef;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
springBoot应用程序
@SpringBootApplication
@EnableJpaRepositories(basePackageClasses = AppNameApplication.class, entityManagerFactoryRef = "entityManagerFactory")
public class AppNameApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(AvaliacaoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AvaliacaoApplication.class);
}
}
提前致谢 !!