Skip to content

Commit 91b85b9

Browse files
committed
Fixed db profiles
1 parent b49ec6b commit 91b85b9

15 files changed

+135
-152
lines changed

docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ services:
1717
timeout: 5s
1818
retries: 5
1919

20+
mysql:
21+
image: mysql:8.0
22+
container_name: petclinic-mysql
23+
ports:
24+
- "3306:3306"
25+
environment:
26+
MYSQL_ROOT_PASSWORD: root
27+
MYSQL_DATABASE: patient_records
28+
MYSQL_USER: patient_user
29+
MYSQL_PASSWORD: patient_pass
30+
volumes:
31+
- mysql_data:/var/lib/mysql
32+
healthcheck:
33+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
34+
interval: 10s
35+
timeout: 5s
36+
retries: 5
37+
2038
app:
2139
build: ./
2240
environment:
@@ -72,3 +90,4 @@ services:
7290

7391
volumes:
7492
postgres_data:
93+
mysql_data:

src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.beans.factory.InitializingBean;
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.beans.factory.annotation.Qualifier;
13+
import org.springframework.context.annotation.Profile;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.jdbc.core.JdbcTemplate;
@@ -22,6 +23,7 @@
2223

2324
@RestController
2425
@RequestMapping("/api/clinic-activity")
26+
@Profile("postgres")
2527
public class ClinicActivityController implements InitializingBean {
2628

2729
private static final Logger logger = LoggerFactory.getLogger(ClinicActivityController.class);

src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityDataService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Profile;
89
import org.springframework.samples.petclinic.model.ClinicActivityLog;
910
import org.springframework.stereotype.Service;
1011
import org.springframework.transaction.PlatformTransactionManager;
@@ -35,6 +36,7 @@
3536
import java.util.HashMap;
3637

3738
@Service
39+
@Profile("postgres")
3840
public class ClinicActivityDataService {
3941

4042
private static final Logger logger = LoggerFactory.getLogger(ClinicActivityDataService.class);

src/main/java/org/springframework/samples/petclinic/config/DatabaseConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;
99
import org.springframework.context.annotation.Primary;
10+
import org.springframework.context.annotation.Profile;
1011
import org.springframework.jdbc.core.JdbcTemplate;
1112

1213
import javax.sql.DataSource;
1314

1415
@Configuration
16+
@Profile({"postgres", "mysql"})
1517
public class DatabaseConfig {
1618

1719
@Primary
1820
@Bean(name = "postgresDataSource")
1921
@ConfigurationProperties("app.datasource.postgres")
22+
@Profile("postgres")
2023
public DataSource postgresDataSource() {
2124
return DataSourceBuilder.create().type(HikariDataSource.class).build();
2225
}
@@ -29,6 +32,7 @@ public DataSource mysqlDataSource() {
2932

3033
@Primary
3134
@Bean(name = "postgresJdbcTemplate")
35+
@Profile("postgres")
3236
public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSource dataSource) {
3337
return new JdbcTemplate(dataSource);
3438
}

src/main/java/org/springframework/samples/petclinic/config/MySqlConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
78
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
89
import org.springframework.orm.jpa.JpaTransactionManager;
910
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -22,6 +23,7 @@
2223
transactionManagerRef = "mysqlTransactionManager",
2324
basePackages = {"org.springframework.samples.petclinic.patientrecords"}
2425
)
26+
@Profile({"postgres", "mysql"})
2527
public class MySqlConfig {
2628

2729
@Bean(name = "mysqlEntityManagerFactory")

src/main/java/org/springframework/samples/petclinic/config/PostgresConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.context.annotation.Primary;
8+
import org.springframework.context.annotation.Profile;
89
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
910
import org.springframework.orm.jpa.JpaTransactionManager;
1011
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -27,6 +28,7 @@
2728
"org.springframework.samples.petclinic.clinicactivity"
2829
}
2930
)
31+
@Profile("postgres")
3032
public class PostgresConfig {
3133

3234
@Primary

src/main/java/org/springframework/samples/petclinic/patientrecords/PatientRecordController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.beans.factory.InitializingBean;
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.beans.factory.annotation.Qualifier;
13+
import org.springframework.context.annotation.Profile;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.jdbc.core.JdbcTemplate;
@@ -21,6 +22,7 @@
2122

2223
@RestController
2324
@RequestMapping("/api/patient-records")
25+
@Profile({"postgres", "mysql"})
2426
public class PatientRecordController implements InitializingBean {
2527

2628
private static final Logger logger = LoggerFactory.getLogger(PatientRecordController.class);

src/main/java/org/springframework/samples/petclinic/patientrecords/PatientRecordDataService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Profile;
89
import org.springframework.samples.petclinic.model.PatientRecord;
910
import org.springframework.stereotype.Service;
1011
import org.springframework.transaction.PlatformTransactionManager;
@@ -23,6 +24,7 @@
2324
import java.util.concurrent.TimeUnit;
2425

2526
@Service
27+
@Profile({"postgres", "mysql"})
2628
public class PatientRecordDataService {
2729

2830
private static final Logger logger = LoggerFactory.getLogger(PatientRecordDataService.class);

src/main/java/org/springframework/samples/petclinic/patientrecords/PatientRecordUiController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.context.annotation.Profile;
56
import org.springframework.jdbc.core.JdbcTemplate;
67
import org.springframework.stereotype.Controller;
78
import org.springframework.ui.Model;
@@ -10,6 +11,7 @@
1011

1112
@Controller
1213
@RequestMapping("/patient-records")
14+
@Profile({"postgres", "mysql"})
1315
public class PatientRecordUiController {
1416

1517
private final JdbcTemplate mysqlJdbcTemplate;
Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
1-
# database init, supports mysql too
2-
database=mysql
3-
spring.datasource.url=${MYSQL_URL:jdbc:mysql://localhost/petclinic}
4-
spring.datasource.username=${MYSQL_USER:petclinic}
5-
spring.datasource.password=${MYSQL_PASS:petclinic}
6-
# SQL is written to be idempotent so this is safe
1+
database=h2
2+
3+
# MySQL Configuration for Patient Records ONLY
4+
# This profile enables MySQL ONLY for patient records
5+
# Main petclinic data (owners, pets, visits, vets) uses H2 database
6+
7+
# Primary DataSource - H2 (Main PetClinic Data)
8+
# H2 handles all petclinic functionality when using MySQL profile
9+
# spring.datasource settings are inherited from default application.properties
10+
11+
# Secondary DataSource - MySQL (Patient Records ONLY)
12+
app.datasource.mysql.jdbc-url=${MYSQL_URL:jdbc:mysql://localhost:3306/patient_records}
13+
app.datasource.mysql.username=${MYSQL_USER:patient_user}
14+
app.datasource.mysql.password=${MYSQL_PASS:patient_pass}
15+
app.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
16+
17+
# Connection Pool Settings for MySQL (Patient Records)
18+
app.datasource.mysql.maximum-pool-size=10
19+
app.datasource.mysql.minimum-idle=2
20+
app.datasource.mysql.connection-timeout=20000
21+
app.datasource.mysql.idle-timeout=300000
22+
app.datasource.mysql.max-lifetime=1200000
23+
24+
# Schema and Data Initialization - H2 ONLY (Main PetClinic)
725
spring.sql.init.mode=always
26+
spring.sql.init.continue-on-error=false
27+
spring.sql.init.separator=;
28+
29+
# MySQL Patient Records - NO AUTO INITIALIZATION
30+
# Patient records schema must be manually created when needed
31+
app.patient.records.auto-init=false
32+
app.patient.records.enabled=${PATIENT_RECORDS_ENABLED:true}
33+
34+
# JPA/Hibernate Settings - H2 (Main PetClinic Data)
35+
spring.jpa.hibernate.ddl-auto=none
36+
spring.jpa.show-sql=false
37+
spring.jpa.properties.hibernate.format_sql=false
38+
spring.jpa.properties.hibernate.jdbc.batch_size=25
39+
spring.jpa.properties.hibernate.order_inserts=true
40+
spring.jpa.properties.hibernate.order_updates=true
41+
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
42+
43+
# Application Architecture Settings
44+
app.database.primary=h2
45+
app.database.secondary=mysql
46+
app.database.mode=separated
47+
app.database.primary.purpose=petclinic_data
48+
app.database.secondary.purpose=patient_records_only

0 commit comments

Comments
 (0)