Skip to content

Commit

Permalink
Fix MySQL configuration and documentation #24
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Feb 28, 2019
1 parent a20992f commit 9a565ab
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 48 deletions.
28 changes: 23 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,32 @@ Our issue tracker is available here: https://github.com/spring-petclinic/spring-

## Database configuration

In its default configuration, Petclinic uses an in-memory database (HSQLDB) which
gets populated at startup with data. A similar setup is provided for MySql in case a persistent database configuration is needed.
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.
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data.
A similar setups is provided for MySql in case a persistent database configuration is needed.
To run petclinic locally using MySQL database, it is needed to change profile defined in application.properties file.

You may start a MySql database with docker:
For MySQL database, it is needed to switch profile. There is two ways:

1. Update application properties: open the `application.properties` file, then change the value `hsqldb` to `mysql`
2. Use a Spring Boot JVM parameter: simply start the JVM with the `-Dspring.profiles.active=mysql.prod` parameter.


Before do this, would be good to check properties defined in `application-mysql.properties` file.

```
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
spring.datasource.username=pc
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
```

You may also start a MySql database with docker:

```
docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
docker run --name mysql-petclinic -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7
```

## Working with Petclinic in Eclipse/STS
Expand Down
6 changes: 6 additions & 0 deletions spring-petclinic-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@
<artifactId>spring-petclinic-client</artifactId>
</dependency>

<!-- JDBC drivers -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# HSQLDB config start
#----------------------------------------------------------------

spring.datasource.schema=classpath:db/hsqldb/schema.sql
spring.datasource.data=classpath:db/hsqldb/data.sql

spring.datasource.url=jdbc:hsqldb:mem:petclinic
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database=HSQL
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.hibernate.ddl-auto=none
#----------------------------------------------------------------
# HSQLDB config end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Always initialize the datasource.
# Change the value if required (see DataSourceInitializationMode)
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:db/mysql/schema.sql
spring.datasource.data=classpath:db/mysql/data.sql

# MySQL config start
#----------------------------------------------------------------
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
spring.datasource.username=root
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
#----------------------------------------------------------------
# MySQL config end
21 changes: 15 additions & 6 deletions spring-petclinic-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# database init, supports mysql too
petclinic.database=hsqldb
spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql
spring.datasource.data=classpath*:db/${petclinic.database}/data.sql
# Active profiles config
# The application uses two active profiles
#
# one for select the database
# ------------------------------------------------
# When using HSQL, use: hsqldb (default one)
# When using MySQL, use: mysql
# ------------------------------------------------
#
# one for select dev / prod mode
# ------------------------------------------------
# To deploy the app in a production environment use: prod (default one)
# During development use: dev
# ------------------------------------------------
spring.profiles.active=hsqldb,prod

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

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

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

This file was deleted.

13 changes: 0 additions & 13 deletions spring-petclinic-server/src/main/resources/db_readme.txt

This file was deleted.

0 comments on commit 9a565ab

Please sign in to comment.