Skip to content

Commit

Permalink
checkpoint for multi-datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 26, 2024
1 parent 3f4d340 commit 5ff1a46
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ghostchu.btn.sparkle.config;

import jakarta.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(basePackages = "com.ghostchu.btn.sparkle.module.analyse",
entityManagerFactoryRef = "analyseEntityManagerFactory",
transactionManagerRef = "analyseTransactionManager")
public class AnalyseDatabaseConfig {

@Bean("analyseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.analyse")
public DataSource analyseDatasource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "analyseEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("analyseDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.ghostchu.btn.sparkle.module.analyse").persistenceUnit("customer").build();
}

@Bean(name = "analyseTransactionManager")
public PlatformTransactionManager customerTransactionManager(@Qualifier("analyseEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory) {
return new JpaTransactionManager(customerEntityManagerFactory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ghostchu.btn.sparkle.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DatabaseConfig {

}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/sparkledb
spring.datasource.username=sparkleusr
spring.datasource.password=sparklepwd
spring.datasource.analyse.driver-class-name=org.postgresql.Driver
spring.datasource.analyse.url=jdbc:postgresql://localhost:5432/sparkledb
spring.datasource.analyse.username=sparkleusr
spring.datasource.analyse.password=sparklepwd

druid.management.username=admin
druid.management.password=admin
Expand Down

0 comments on commit 5ff1a46

Please sign in to comment.