Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 16 additions & 50 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,24 @@ services:
timeout: 5s
retries: 5

app:
build: ./
environment:
OTEL_SERVICE_NAME: "PetClinic"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://host.docker.internal:5050"
# Logs are disabled by default
OTEL_LOGS_EXPORTER: "otlp"
# Digma entries
CODE_PACKAGE_PREFIXES: "org.springframework.samples.petclinic"
DEPLOYMENT_ENV: "SAMPLE_ENV"
SPRING_PROFILES_ACTIVE: postgres
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/petclinic
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: postgres
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:9753/" ]
interval: 20s
timeout: 10s
retries: 4
start_period: 5s
mysql:
image: mysql:8.0
container_name: petclinic-mysql
ports:
- "9753:9753"
entrypoint: java -jar -javaagent:/opentelemetry-javaagent.jar -Dotel.javaagent.extensions=/digma-otel-agent-extension.jar app.jar
depends_on:
postgres:
condition: service_healthy
extra_hosts:
- "host.docker.internal:host-gateway"

tester:
build: ./
- "3306:3306"
environment:
OTEL_SERVICE_NAME: "PetClinicTester"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://host.docker.internal:5050"
# Logs are disabled by default
OTEL_LOGS_EXPORTER: "otlp"
PETSHOP_URL: "http://app:9753"
# Digma entries
CODE_PACKAGE_PREFIXES: "org.springframework.samples.petclinic"
DEPLOYMENT_ENV: "SAMPLE_ENV"
SPRING_PROFILES_ACTIVE: postgres
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/petclinic
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: postgres
entrypoint: java -cp app.jar -Dloader.main=petclinic.client.ClientTester org.springframework.boot.loader.PropertiesLauncher
depends_on:
app:
condition: service_healthy
postgres:
condition: service_healthy
extra_hosts:
- "host.docker.internal:host-gateway"
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: patient_records
MYSQL_USER: patient_user
MYSQL_PASSWORD: patient_pass
volumes:
- mysql_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5

volumes:
postgres_data:
mysql_data:
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -22,6 +23,7 @@

@RestController
@RequestMapping("/api/clinic-activity")
@Profile({"postgres", "mysql"})
public class ClinicActivityController implements InitializingBean {

private static final Logger logger = LoggerFactory.getLogger(ClinicActivityController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.samples.petclinic.model.ClinicActivityLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
Expand Down Expand Up @@ -35,6 +36,7 @@
import java.util.HashMap;

@Service
@Profile({"postgres", "mysql"})
public class ClinicActivityDataService {

private static final Logger logger = LoggerFactory.getLogger(ClinicActivityDataService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
@Profile({"postgres", "mysql"})
public class DatabaseConfig {

@Primary
Expand All @@ -23,6 +25,7 @@ public DataSource postgresDataSource() {

@Bean(name = "mysqlDataSource")
@ConfigurationProperties("app.datasource.mysql")
@Profile("mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
Expand All @@ -34,6 +37,7 @@ public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSo
}

@Bean(name = "mysqlJdbcTemplate")
@Profile("mysql")
public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
Expand All @@ -22,6 +23,7 @@
transactionManagerRef = "mysqlTransactionManager",
basePackages = {"org.springframework.samples.petclinic.patientrecords"}
)
@Profile("mysql")
public class MySqlConfig {

@Bean(name = "mysqlEntityManagerFactory")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
Expand All @@ -27,6 +28,7 @@
"org.springframework.samples.petclinic.clinicactivity"
}
)
@Profile({"postgres", "mysql"})
public class PostgresConfig {

@Primary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -21,6 +22,7 @@

@RestController
@RequestMapping("/api/patient-records")
@Profile("mysql")
public class PatientRecordController implements InitializingBean {

private static final Logger logger = LoggerFactory.getLogger(PatientRecordController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.samples.petclinic.model.PatientRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
Expand All @@ -14,6 +16,7 @@
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.jdbc.core.JdbcTemplate;

import jakarta.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
Expand All @@ -23,6 +26,7 @@
import java.util.concurrent.TimeUnit;

@Service
@Profile("mysql")
public class PatientRecordDataService {

private static final Logger logger = LoggerFactory.getLogger(PatientRecordDataService.class);
Expand All @@ -32,6 +36,9 @@ public class PatientRecordDataService {
private final JdbcTemplate mysqlJdbcTemplate;
private final PlatformTransactionManager mysqlTransactionManager;

@Value("${app.patient.records.auto-init:false}")
private boolean autoInitEnabled;

// List of veterinary treatment types
private static final List<String> TREATMENT_TYPES = List.of(
"Annual Wellness Exam", "Vaccination (Rabies)", "Vaccination (DHPP)", "Vaccination (FVRCP)",
Expand All @@ -53,6 +60,35 @@ public PatientRecordDataService(PatientRecordRepository repository,
this.mysqlTransactionManager = mysqlTransactionManager;
}

@PostConstruct
public void initializeSchema() {
if (autoInitEnabled) {
logger.info("Auto-initialization enabled - creating patient_records table if it doesn't exist");
try {
String createTableSql =
"CREATE TABLE IF NOT EXISTS patient_records (" +
"id INT AUTO_INCREMENT PRIMARY KEY," +
"treatment_type VARCHAR(255) NOT NULL," +
"patient_weight INT NOT NULL," +
"visit_date TIMESTAMP NOT NULL," +
"treatment_completed BOOLEAN NOT NULL," +
"medical_notes TEXT," +
"INDEX idx_treatment_type (treatment_type)," +
"INDEX idx_visit_date (visit_date)," +
"INDEX idx_treatment_completed (treatment_completed)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

mysqlJdbcTemplate.execute(createTableSql);
logger.info("Patient records table initialized successfully");
} catch (Exception e) {
logger.error("Failed to initialize patient records schema", e);
throw new RuntimeException("Failed to initialize patient records schema: " + e.getMessage(), e);
}
} else {
logger.info("Auto-initialization disabled - patient_records table must be created manually");
}
}

@Transactional("mysqlTransactionManager")
public void cleanupPatientRecords() {
logger.info("Received request to clean up all patient records.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -10,6 +11,7 @@

@Controller
@RequestMapping("/patient-records")
@Profile("mysql")
public class PatientRecordUiController {

private final JdbcTemplate mysqlJdbcTemplate;
Expand Down
60 changes: 54 additions & 6 deletions src/main/resources/application-mysql.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
# database init, supports mysql too
database=mysql
spring.datasource.url=${MYSQL_URL:jdbc:mysql://localhost/petclinic}
spring.datasource.username=${MYSQL_USER:petclinic}
spring.datasource.password=${MYSQL_PASS:petclinic}
# SQL is written to be idempotent so this is safe
database=postgres

# Multi-Database Configuration - PostgreSQL + MySQL
# This is the ONLY profile that enables both databases
# PostgreSQL: Main petclinic data (owners, pets, visits, vets) + clinic activity
# MySQL: Patient records ONLY (separate medical records system)

# Primary DataSource - PostgreSQL (Main PetClinic Data + Clinic Activity)
app.datasource.postgres.jdbcUrl=jdbc:postgresql://localhost:5442/petclinic
app.datasource.postgres.username=${POSTGRES_USER:postgres}
app.datasource.postgres.password=${POSTGRES_PASS:postgres}
app.datasource.postgres.driverClassName=org.postgresql.Driver
app.datasource.postgres.maximumPoolSize=20
app.datasource.postgres.minimumIdle=5
app.datasource.postgres.connectionTimeout=20000
app.datasource.postgres.idleTimeout=300000
app.datasource.postgres.maxLifetime=1200000

# Secondary DataSource - MySQL (Patient Records ONLY)
app.datasource.mysql.jdbcUrl=${MYSQL_URL:jdbc:mysql://localhost:3306/patient_records}
app.datasource.mysql.username=${MYSQL_USER:patient_user}
app.datasource.mysql.password=${MYSQL_PASS:patient_pass}
app.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
app.datasource.mysql.maximumPoolSize=10
app.datasource.mysql.minimumIdle=2
app.datasource.mysql.connectionTimeout=20000
app.datasource.mysql.idleTimeout=300000
app.datasource.mysql.maxLifetime=1200000

# Schema and Data Initialization - PostgreSQL (Main PetClinic)
spring.sql.init.mode=always
spring.sql.init.continue-on-error=false
spring.sql.init.separator=;

# MySQL Patient Records - AUTO INITIALIZATION ENABLED
# Patient records schema will be created automatically on startup
app.patient.records.auto-init=true
app.patient.records.enabled=${PATIENT_RECORDS_ENABLED:true}

# JPA/Hibernate Settings - PostgreSQL (Main PetClinic Data)
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.jdbc.batch_size=25
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true

# Application Architecture Settings
app.database.primary=postgres
app.database.secondary=mysql
app.database.mode=multi_database
app.database.primary.purpose=petclinic_data_and_clinic_activity
app.database.secondary.purpose=patient_records_only
40 changes: 36 additions & 4 deletions src/main/resources/application-postgres.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
database=postgres
spring.datasource.url=jdbc:postgresql://localhost:5442/petclinic
spring.datasource.username=${POSTGRES_USER:postgres}
spring.datasource.password=${POSTGRES_PASS:postgres}
# SQL is written to be idempotent so this is safe

# PostgreSQL DataSource Configuration (Primary - for main petclinic data)
app.datasource.postgres.jdbcUrl=jdbc:postgresql://localhost:5442/petclinic
app.datasource.postgres.username=${POSTGRES_USER:postgres}
app.datasource.postgres.password=${POSTGRES_PASS:postgres}
app.datasource.postgres.driverClassName=org.postgresql.Driver
app.datasource.postgres.maximumPoolSize=20
app.datasource.postgres.minimumIdle=5
app.datasource.postgres.connectionTimeout=20000
app.datasource.postgres.idleTimeout=300000
app.datasource.postgres.maxLifetime=1200000

# MySQL DataSource Configuration (Secondary - for patient records only)
app.datasource.mysql.jdbcUrl=${MYSQL_URL:jdbc:mysql://localhost:3306/patient_records}
app.datasource.mysql.username=${MYSQL_USER:patient_user}
app.datasource.mysql.password=${MYSQL_PASS:patient_pass}
app.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
app.datasource.mysql.maximumPoolSize=10
app.datasource.mysql.minimumIdle=2
app.datasource.mysql.connectionTimeout=20000
app.datasource.mysql.idleTimeout=300000
app.datasource.mysql.maxLifetime=1200000

# Schema and Data Initialization - ALWAYS run to ensure schema exists
spring.sql.init.mode=always
spring.sql.init.continue-on-error=false
spring.sql.init.separator=;

# JPA/Hibernate Settings for PostgreSQL
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.jdbc.batch_size=25
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
Loading