Skip to content

Commit 9a565ab

Browse files
committed
Fix MySQL configuration and documentation #24
1 parent a20992f commit 9a565ab

File tree

7 files changed

+75
-48
lines changed

7 files changed

+75
-48
lines changed

readme.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,32 @@ Our issue tracker is available here: https://github.com/spring-petclinic/spring-
2121

2222
## Database configuration
2323

24-
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which
25-
gets populated at startup with data. A similar setup is provided for MySql in case a persistent database configuration is needed.
26-
Note that whenever the database type is changed, the data-access.properties file needs to be updated and the mysql-connector-java artifact from the pom.xml needs to be uncommented.
24+
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data.
25+
A similar setups is provided for MySql in case a persistent database configuration is needed.
26+
To run petclinic locally using MySQL database, it is needed to change profile defined in application.properties file.
2727

28-
You may start a MySql database with docker:
28+
For MySQL database, it is needed to switch profile. There is two ways:
29+
30+
1. Update application properties: open the `application.properties` file, then change the value `hsqldb` to `mysql`
31+
2. Use a Spring Boot JVM parameter: simply start the JVM with the `-Dspring.profiles.active=mysql.prod` parameter.
32+
33+
34+
Before do this, would be good to check properties defined in `application-mysql.properties` file.
35+
36+
```
37+
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
38+
spring.datasource.username=pc
39+
spring.datasource.password=petclinic
40+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
41+
spring.jpa.database=MYSQL
42+
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
43+
spring.jpa.hibernate.ddl-auto=none
44+
```
45+
46+
You may also start a MySql database with docker:
2947

3048
```
31-
docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
49+
docker run --name mysql-petclinic -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7
3250
```
3351

3452
## Working with Petclinic in Eclipse/STS

spring-petclinic-server/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@
5858
<artifactId>spring-petclinic-client</artifactId>
5959
</dependency>
6060

61+
<!-- JDBC drivers -->
6162
<dependency>
6263
<groupId>org.hsqldb</groupId>
6364
<artifactId>hsqldb</artifactId>
6465
<scope>runtime</scope>
6566
</dependency>
67+
<dependency>
68+
<groupId>mysql</groupId>
69+
<artifactId>mysql-connector-java</artifactId>
70+
<scope>runtime</scope>
71+
</dependency>
6672

6773
<dependency>
6874
<groupId>org.springframework.boot</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# HSQLDB config start
2+
#----------------------------------------------------------------
3+
4+
spring.datasource.schema=classpath:db/hsqldb/schema.sql
5+
spring.datasource.data=classpath:db/hsqldb/data.sql
6+
7+
spring.datasource.url=jdbc:hsqldb:mem:petclinic
8+
spring.datasource.username=sa
9+
spring.datasource.password=
10+
spring.jpa.database=HSQL
11+
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
12+
spring.jpa.hibernate.ddl-auto=none
13+
#----------------------------------------------------------------
14+
# HSQLDB config end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Always initialize the datasource.
2+
# Change the value if required (see DataSourceInitializationMode)
3+
spring.datasource.initialization-mode=always
4+
spring.datasource.schema=classpath:db/mysql/schema.sql
5+
spring.datasource.data=classpath:db/mysql/data.sql
6+
7+
# MySQL config start
8+
#----------------------------------------------------------------
9+
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
10+
spring.datasource.username=root
11+
spring.datasource.password=petclinic
12+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
13+
spring.jpa.database=MYSQL
14+
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
15+
spring.jpa.hibernate.ddl-auto=none
16+
#----------------------------------------------------------------
17+
# MySQL config end

spring-petclinic-server/src/main/resources/application.properties

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
# database init, supports mysql too
2-
petclinic.database=hsqldb
3-
spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql
4-
spring.datasource.data=classpath*:db/${petclinic.database}/data.sql
1+
# Active profiles config
2+
# The application uses two active profiles
3+
#
4+
# one for select the database
5+
# ------------------------------------------------
6+
# When using HSQL, use: hsqldb (default one)
7+
# When using MySQL, use: mysql
8+
# ------------------------------------------------
9+
#
10+
# one for select dev / prod mode
11+
# ------------------------------------------------
12+
# To deploy the app in a production environment use: prod (default one)
13+
# During development use: dev
14+
# ------------------------------------------------
15+
spring.profiles.active=hsqldb,prod
516

617
# JPA
718
spring.jpa.hibernate.ddl-auto=none
@@ -20,8 +31,6 @@ management.endpoints.web.exposure.include=*
2031
# Logging
2132
logging.level.org.springframework=INFO
2233

23-
# Default Spring profile. In development mode, use dev profile.
24-
spring.profiles.active=prod
2534

2635
server.compression.enabled=true
2736
server.compression.mime-types=application/json,text/css,application/javascript

spring-petclinic-server/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt

-24
This file was deleted.

spring-petclinic-server/src/main/resources/db_readme.txt

-13
This file was deleted.

0 commit comments

Comments
 (0)