Answer the question
In order to leave comments, you need to log in
Spring how to integrate second base, third party database?
Good day to all.
Faced such a problem.
The current project uses two databases, Postgress and ClickHouse.
Accordingly, ClickHouse does not have direct integration with Spring.
I was able to integrate ClickHouse using a third party driver. However, I ran into a problem that both databases do not want to work together. All the examples I've seen on the internet show how to use two databases that already have Spring integration. How can I still get them to work together?
Properties file code
####Spring Configuration####
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
####Postgress DB ####
#spring.pg.datasource.driver-class-name=org.postgresql.Driver
spring.pg.datasource.url=jdbc:postgresql://localhost:5432/java_db
spring.pg.datasource.username=****
spring.pg.datasource.password=****
####
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
####
#second db
spring.ch.datasource.url=jdbc:clickhouse://127.0.0.1:9000
spring.ch.datasource.driverClassName=com.github.housepower.jdbc.ClickHouseDriver
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//Adding first database(Postgress) to Spring config, Properties takes in application.properties
@Configuration
@EnableTransactionManagement
public class ConfigPG {
@Bean
@Primary
@ConfigurationProperties("spring.pg.datasource")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.pg.datasource.configuration")
public HikariDataSource firstDataSource() {
return firstDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
@ConfigurationProperties("spring.ch.datasource")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.ch.datasource.configuration")
public BasicDataSource secondDataSource() {
return secondDataSourceProperties().initializeDataSourceBuilder()
.type(BasicDataSource.class).build();
}
}
@Configuration
@EnableScheduling
@EnableTransactionManagement
@ComponentScan("com.github.housepower")
public class ConfigurationClickHouse {
@Bean
public DataSource chDatasource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.github.housepower.jdbc.ClickHouseDriver");
dataSource.setUrl("jdbc:clickhouse://localhost:9000");
return dataSource;
}
@Bean
public JdbcTemplate chJDBC(DataSource chDatasource) {
return new JdbcTemplate(chDatasource);
}
Answer the question
In order to leave comments, you need to log in
Dug a little. I can narrow down the issue.
Disabled a separate description of the connection to CH. The entire customization area narrowed down to a single class.
@Bean
@Primary
@ConfigurationProperties("spring.pg.datasource")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.ch.datasource")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "PG")
@Primary
@ConfigurationProperties("spring.pg.datasource.configuration")
public DataSource firstDataSource() {
return firstDataSourceProperties().initializeDataSourceBuilder()
.type(BasicDataSource.class).build();
}
@Bean(name = "CH")
@ConfigurationProperties("spring.ch.datasource.configuration")
public DataSource secondDataSource() {
return secondDataSourceProperties().initializeDataSourceBuilder()
.type(BasicDataSource.class).build();
}
@Qualifier("CH")
private JdbcTemplate ch;
@Autowired
public ClickHouseDAO1(JdbcTemplate dataSource) {
this.ch = dataSource;
}
public List<DataRecord> query(String query) {
List<DataRecord> answer = ch.query(query, DataRecord.Mapper.INSTANCE);
return answer;
}
Но оно не рабоет должным образом.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question