JPA config refactoring
This commit is contained in:
parent
248c268d6a
commit
a98ae2c519
@ -13,11 +13,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
@ -29,6 +29,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.thingsboard.server.dao.model.sql.AuditLogEntity;
|
||||
import org.thingsboard.server.dao.model.sql.ErrorEventEntity;
|
||||
import org.thingsboard.server.dao.model.sql.LifecycleEventEntity;
|
||||
import org.thingsboard.server.dao.model.sql.RuleChainDebugEventEntity;
|
||||
@ -38,74 +39,55 @@ import org.thingsboard.server.dao.model.sql.StatisticsEventEntity;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Objects;
|
||||
|
||||
/*
|
||||
* To make entity use a dedicated datasource:
|
||||
* - add its JpaRepository to exclusions list in @EnableJpaRepositories in JpaDaoConfig
|
||||
* - add the package of this JpaRepository to @EnableJpaRepositories in DefaultDedicatedJpaDaoConfig
|
||||
* - add the package of this JpaRepository to @EnableJpaRepositories in DedicatedJpaDaoConfig
|
||||
* - add the entity class to packages list in dedicatedEntityManagerFactory in DedicatedJpaDaoConfig
|
||||
* */
|
||||
@ConditionalOnProperty(value = "spring.datasource.dedicated.enabled", havingValue = "true")
|
||||
@Configuration
|
||||
@EnableJpaRepositories(value = "org.thingsboard.server.dao.sql.event", bootstrapMode = BootstrapMode.LAZY,
|
||||
@EnableJpaRepositories(value = {"org.thingsboard.server.dao.sql.event", "org.thingsboard.server.dao.sql.audit"},
|
||||
bootstrapMode = BootstrapMode.LAZY,
|
||||
entityManagerFactoryRef = "dedicatedEntityManagerFactory", transactionManagerRef = "dedicatedTransactionManager")
|
||||
public class DedicatedJpaDaoConfig {
|
||||
|
||||
@Value("${spring.datasource.dedicated.enabled:false}")
|
||||
private boolean dedicatedDataSourceEnabled;
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.dedicated")
|
||||
public DataSourceProperties dedicatedDataSourceProperties() {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return new DataSourceProperties();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@ConfigurationProperties(prefix = "spring.datasource.dedicated.hikari")
|
||||
@Bean
|
||||
public DataSource dedicatedDataSource(@Qualifier("dedicatedDataSourceProperties") DataSourceProperties dedicatedDataSourceProperties) {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return dedicatedDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return dedicatedDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean dedicatedEntityManagerFactory(@Qualifier("dedicatedDataSource") DataSource dedicatedDataSource,
|
||||
@Qualifier("dataSource") DataSource defaultDataSource,
|
||||
EntityManagerFactoryBuilder builder) {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return builder
|
||||
.dataSource(dedicatedDataSource)
|
||||
.packages(LifecycleEventEntity.class, StatisticsEventEntity.class, ErrorEventEntity.class, RuleNodeDebugEventEntity.class, RuleChainDebugEventEntity.class)
|
||||
.persistenceUnit("dedicated")
|
||||
.build();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return builder
|
||||
.dataSource(dedicatedDataSource)
|
||||
.packages(LifecycleEventEntity.class, StatisticsEventEntity.class, ErrorEventEntity.class, RuleNodeDebugEventEntity.class, RuleChainDebugEventEntity.class, AuditLogEntity.class)
|
||||
.persistenceUnit("dedicated")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JpaTransactionManager dedicatedTransactionManager(@Qualifier("dedicatedEntityManagerFactory") LocalContainerEntityManagerFactoryBean dedicatedEntityManagerFactory) {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return new JpaTransactionManager(Objects.requireNonNull(dedicatedEntityManagerFactory.getObject()));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return new JpaTransactionManager(Objects.requireNonNull(dedicatedEntityManagerFactory.getObject()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TransactionTemplate dedicatedTransactionTemplate(@Qualifier("dedicatedTransactionManager") JpaTransactionManager dedicatedTransactionManager) {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return new TransactionTemplate(dedicatedTransactionManager);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return new TransactionTemplate(dedicatedTransactionManager);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate dedicatedJdbcTemplate(@Qualifier("dedicatedDataSource") DataSource dedicatedDataSource) {
|
||||
if (dedicatedDataSourceEnabled) {
|
||||
return new JdbcTemplate(dedicatedDataSource);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return new JdbcTemplate(dedicatedDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.data.repository.config.BootstrapMode;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@ConditionalOnProperty(value = "spring.datasource.dedicated.enabled", havingValue = "false", matchIfMissing = true)
|
||||
@Configuration
|
||||
@EnableJpaRepositories(value = {"org.thingsboard.server.dao.sql.event", "org.thingsboard.server.dao.sql.audit"}, bootstrapMode = BootstrapMode.LAZY)
|
||||
public class DefaultDedicatedJpaDaoConfig {
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate dedicatedJdbcTemplate(@Qualifier("jdbcTemplate") JdbcTemplate defaultJdbcTemplate) {
|
||||
return defaultJdbcTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TransactionTemplate dedicatedTransactionTemplate(@Qualifier("transactionTemplate") TransactionTemplate defaultTransactionTemplate) {
|
||||
return defaultTransactionTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
@ -33,6 +34,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.thingsboard.server.dao.sql.audit.AuditLogRepository;
|
||||
import org.thingsboard.server.dao.sql.event.EventRepository;
|
||||
import org.thingsboard.server.dao.util.TbAutoConfiguration;
|
||||
|
||||
@ -45,9 +47,8 @@ import java.util.Objects;
|
||||
@TbAutoConfiguration
|
||||
@ComponentScan({"org.thingsboard.server.dao.sql", "org.thingsboard.server.dao.attributes", "org.thingsboard.server.dao.sqlts.dictionary", "org.thingsboard.server.dao.cache", "org.thingsboard.server.cache"})
|
||||
@EnableJpaRepositories(value = {"org.thingsboard.server.dao.sql", "org.thingsboard.server.dao.sqlts.dictionary"},
|
||||
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
|
||||
EventRepository.class
|
||||
}), bootstrapMode = BootstrapMode.LAZY)
|
||||
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {EventRepository.class, AuditLogRepository.class}),
|
||||
bootstrapMode = BootstrapMode.LAZY)
|
||||
public class JpaDaoConfig {
|
||||
|
||||
@Bean
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.dao;
|
||||
package org.thingsboard.server.dao.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@ -19,7 +19,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.audit.AuditLog;
|
||||
@ -48,7 +47,6 @@ public class JpaAuditLogDao extends JpaPartitionedAbstractDao<AuditLogEntity, Au
|
||||
|
||||
private final AuditLogRepository auditLogRepository;
|
||||
private final SqlPartitioningRepository partitioningRepository;
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Value("${sql.audit_logs.partition_size:168}")
|
||||
private int partitionSizeInHours;
|
||||
|
||||
@ -25,6 +25,10 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.thingsboard.server.common.stats.StatsFactory;
|
||||
import org.thingsboard.server.dao.config.DedicatedJpaDaoConfig;
|
||||
import org.thingsboard.server.dao.config.JpaDaoConfig;
|
||||
import org.thingsboard.server.dao.config.SqlTsDaoConfig;
|
||||
import org.thingsboard.server.dao.config.SqlTsLatestDaoConfig;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
|
||||
@ -24,6 +24,10 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.thingsboard.server.common.stats.StatsFactory;
|
||||
import org.thingsboard.server.dao.config.DedicatedJpaDaoConfig;
|
||||
import org.thingsboard.server.dao.config.JpaDaoConfig;
|
||||
import org.thingsboard.server.dao.config.SqlTsDaoConfig;
|
||||
import org.thingsboard.server.dao.config.SqlTsLatestDaoConfig;
|
||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user