Improve MySQL and PosrgreSQL switch

This commit is contained in:
Antoine Rey 2016-10-15 18:55:19 +02:00
parent bae94cf327
commit 7acdd19c5c
7 changed files with 108 additions and 108 deletions

46
pom.xml
View file

@ -29,17 +29,10 @@
<!-- Others --> <!-- Others -->
<mysql-driver.version>5.1.36</mysql-driver.version> <mysql-driver.version>5.1.36</mysql-driver.version>
<postgresql-driver.version>9.4.1211.jre7</postgresql-driver.version> <postgresql-driver.version>9.4.1211.jre7</postgresql-driver.version>
<cobertura.version>2.7</cobertura.version> <cobertura.version>2.7</cobertura.version>
<jpa.database>HSQL</jpa.database>
<jdbc.driverClassName>org.hsqldb.jdbcDriver</jdbc.driverClassName>
<jdbc.url>jdbc:hsqldb:mem:petclinic</jdbc.url>
<jdbc.username>sa</jdbc.username>
<jdbc.password></jdbc.password>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -164,13 +157,6 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- Databases - Uses HSQL by default -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<!-- HIBERNATE --> <!-- HIBERNATE -->
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
@ -429,8 +415,30 @@
</build> </build>
</profile> </profile>
<profile> <profile>
<id>MYSQL</id> <id>HSQLDB</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties> <properties>
<db.script>hsqldb</db.script>
<jpa.database>HSQL</jpa.database>
<jdbc.driverClassName>org.hsqldb.jdbcDriver</jdbc.driverClassName>
<jdbc.url>jdbc:hsqldb:mem:petclinic</jdbc.url>
<jdbc.username>sa</jdbc.username>
<jdbc.password/>
</properties>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>MySQL</id>
<properties>
<db.script>mysql</db.script>
<jpa.database>MYSQL</jpa.database> <jpa.database>MYSQL</jpa.database>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/petclinic?useUnicode=true</jdbc.url> <jdbc.url>jdbc:mysql://localhost:3306/petclinic?useUnicode=true</jdbc.url>
@ -442,14 +450,15 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version> <version>${mysql-driver.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</profile> </profile>
<profile> <profile>
<id>POSTGRESQL</id> <id>PostgreSQL</id>
<properties> <properties>
<db.script>postgresql</db.script>
<jpa.database>POSTGRESQL</jpa.database> <jpa.database>POSTGRESQL</jpa.database>
<jpa.databasePlatform>org.hibernate.dialect.PostgreSQLDialect</jpa.databasePlatform>
<jdbc.driverClassName>org.postgresql.Driver</jdbc.driverClassName> <jdbc.driverClassName>org.postgresql.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:postgresql://localhost:5432/petclinic</jdbc.url> <jdbc.url>jdbc:postgresql://localhost:5432/petclinic</jdbc.url>
<jdbc.username>postgres</jdbc.username> <jdbc.username>postgres</jdbc.username>
@ -460,6 +469,7 @@
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<version>${postgresql-driver.version}</version> <version>${postgresql-driver.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</profile> </profile>

View file

@ -23,13 +23,13 @@ gets populated at startup with data.
A similar setups is provided for MySql and PostgreSQL in case a persistent database configuration is needed. A similar setups is provided for MySql and PostgreSQL in case a persistent database configuration is needed.
To run petclinic locally using persistent database, it is needed to run with profile defined in main pom.xml file. To run petclinic locally using persistent database, it is needed to run with profile defined in main pom.xml file.
For MySQL database, it is needed to run with 'MYSQL' profile defined in main pom.xml file. For MySQL database, it is needed to run with 'MySQL' profile defined in main pom.xml file.
``` ```
./mvnw tomcat7:run -P MYSQL ./mvnw tomcat7:run -P MySQL
``` ```
Before do this, would be good to check properties defined in MYSQL profile inside pom.xml file. Before do this, would be good to check properties defined in MySQL profile inside pom.xml file.
``` ```
<properties> <properties>
@ -47,13 +47,13 @@ You may 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 -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
``` ```
For PostgreSQL database, it is needed to run with 'POSTGRESQL' profile defined in main pom.xml file. For PostgreSQL database, it is needed to run with 'PostgreSQL' profile defined in main pom.xml file.
``` ```
./mvnw tomcat7:run -P POSTGRESQL ./mvnw tomcat7:run -P PostgreSQL
``` ```
Before do this, would be good to check properties defined in POSTGRESQL profile inside pom.xml file. Before do this, would be good to check properties defined in PostgreSQL profile inside pom.xml file.
``` ```
<properties> <properties>

View file

@ -7,18 +7,17 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x), 1) Download and install the MySQL database (e.g., MySQL Community Server 5.7.x),
which can be found here: http://dev.mysql.com/downloads/ which can be found here: http://dev.mysql.com/downloads/mysql/
Alternatively, you may use the official MySQL docker image. Refer to the
README.md for the Docker command line.
2) Download Connector/J, the MySQL JDBC driver (e.g., Connector/J 5.1.x), which 2) Dependency for Connector/J, the MySQL JDBC driver (e.g., Connector/J 5.1.x) is
can be found here: http://dev.mysql.com/downloads/connector/j/ included in Petclinic pom.xml file. See the MySQL maven profile.
Copy the Connector/J JAR file (e.g., mysql-connector-java-5.1.5-bin.jar) into Alternatively, download Connector/J JDBC driver from here:
the db/mysql directory. Alternatively, uncomment the mysql-connector from the http://dev.mysql.com/downloads/connector/j/
Petclinic pom. and copy the PostgreSQL JDBC driver JAR file (e.g., postgresql-9.4.1211.jre7.jar).
3) Create the PetClinic database and user by executing the "db/mysql/initDB.sql" 3) Change connection parameters into the MySQL profile of the pom.xml.
script. Build and starth the web app with the MySQL profile:
./mvnw tomcat7:run -P MySQL
4) Open "src/main/resources/spring/data-access.properties"; comment out all properties in the
"HSQL Settings" section; uncomment all properties in the "MySQL Settings"
section.

View file

@ -1,9 +1,3 @@
CREATE DATABASE petclinic
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
CREATE TABLE IF NOT EXISTS vets ( CREATE TABLE IF NOT EXISTS vets (
id SERIAL, id SERIAL,
first_name VARCHAR(30), first_name VARCHAR(30),
@ -11,7 +5,7 @@ CREATE TABLE IF NOT EXISTS vets (
CONSTRAINT pk_vets PRIMARY KEY (id) CONSTRAINT pk_vets PRIMARY KEY (id)
); );
CREATE INDEX idx_vets_last_name ON vets (last_name); CREATE INDEX IF NOT EXISTS idx_vets_last_name ON vets (last_name);
ALTER SEQUENCE vets_id_seq RESTART WITH 100; ALTER SEQUENCE vets_id_seq RESTART WITH 100;
@ -22,7 +16,7 @@ CREATE TABLE IF NOT EXISTS specialties (
CONSTRAINT pk_specialties PRIMARY KEY (id) CONSTRAINT pk_specialties PRIMARY KEY (id)
); );
CREATE INDEX idx_specialties_name ON specialties (name); CREATE INDEX IF NOT EXISTS idx_specialties_name ON specialties (name);
ALTER SEQUENCE specialties_id_seq RESTART WITH 100; ALTER SEQUENCE specialties_id_seq RESTART WITH 100;
@ -43,7 +37,7 @@ CREATE TABLE IF NOT EXISTS types (
CONSTRAINT pk_types PRIMARY KEY (id) CONSTRAINT pk_types PRIMARY KEY (id)
); );
CREATE INDEX idx_types_name ON types (name); CREATE INDEX IF NOT EXISTS idx_types_name ON types (name);
ALTER SEQUENCE types_id_seq RESTART WITH 100; ALTER SEQUENCE types_id_seq RESTART WITH 100;
@ -57,7 +51,7 @@ CREATE TABLE IF NOT EXISTS owners (
CONSTRAINT pk_owners PRIMARY KEY (id) CONSTRAINT pk_owners PRIMARY KEY (id)
); );
CREATE INDEX idx_owners_last_name ON owners (last_name); CREATE INDEX IF NOT EXISTS idx_owners_last_name ON owners (last_name);
ALTER SEQUENCE owners_id_seq RESTART WITH 100; ALTER SEQUENCE owners_id_seq RESTART WITH 100;
@ -73,7 +67,7 @@ CREATE TABLE IF NOT EXISTS pets (
CONSTRAINT pk_pets PRIMARY KEY (id) CONSTRAINT pk_pets PRIMARY KEY (id)
); );
CREATE INDEX idx_pets_name ON pets (name); CREATE INDEX IF NOT EXISTS idx_pets_name ON pets (name);
ALTER SEQUENCE pets_id_seq RESTART WITH 100; ALTER SEQUENCE pets_id_seq RESTART WITH 100;

View file

@ -1,5 +1,5 @@
================================================================================ ================================================================================
=== Spring PetClinic sample application - PostgreSQL Configuration === === Spring PetClinic sample application - PostgreSQL Configuration ===
================================================================================ ================================================================================
@author Sam Brannen @author Sam Brannen
@ -11,22 +11,19 @@
which can be found here: https://www.postgresql.org/download/ which can be found here: https://www.postgresql.org/download/
2) Dependency for postgresql-driver included in Petclinic pom.xml file. 2) Dependency for postgresql-driver included in Petclinic pom.xml file.
Alternatively, download PostgreSQL JDBC driver from here: https://jdbc.postgresql.org/download.html See the PostgreSQL maven profile.
Alternatively, download PostgreSQL JDBC driver from here: https://jdbc.postgresql.org/download.html
and copy the PostgreSQL JDBC driver JAR file (e.g., postgresql-9.4.1211.jre7.jar) into and copy the PostgreSQL JDBC driver JAR file (e.g., postgresql-9.4.1211.jre7.jar) into
the db/postgesql directory. the db/postgesql directory.
3) Create the PetClinic database 3) Open psql and create the PetClinic database:
CREATE DATABASE petclinic CREATE DATABASE petclinic
WITH OWNER = postgres WITH OWNER = postgres
ENCODING = 'UTF8' ENCODING = 'UTF8'
TABLESPACE = pg_default TABLESPACE = pg_default
CONNECTION LIMIT = -1; CONNECTION LIMIT = -1;
and execute the "db/postgresql/initDB.sql" script, then execute "db/postgresql/populateDB.sql"
4) Open "src/main/resources/spring/datasource-config.xml"; comment block 4) If required, change connection parameters into the PostgreSQL profile of the pom.xml.
<jdbc:initialize-database data-source="dataSource"> Build and starth the web app with the PostgreSQL profile:
<jdbc:script location="${jdbc.initLocation}"/> ./mvnw tomcat7:run -P PostgreSQL
<jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>

View file

@ -1,53 +1,53 @@
INSERT INTO vets VALUES (1, 'James', 'Carter'); INSERT INTO vets VALUES (1, 'James', 'Carter') ON CONFLICT DO NOTHING;
INSERT INTO vets VALUES (2, 'Helen', 'Leary'); INSERT INTO vets VALUES (2, 'Helen', 'Leary') ON CONFLICT DO NOTHING;
INSERT INTO vets VALUES (3, 'Linda', 'Douglas'); INSERT INTO vets VALUES (3, 'Linda', 'Douglas') ON CONFLICT DO NOTHING;
INSERT INTO vets VALUES (4, 'Rafael', 'Ortega'); INSERT INTO vets VALUES (4, 'Rafael', 'Ortega') ON CONFLICT DO NOTHING;
INSERT INTO vets VALUES (5, 'Henry', 'Stevens'); INSERT INTO vets VALUES (5, 'Henry', 'Stevens') ON CONFLICT DO NOTHING;
INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins'); INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins') ON CONFLICT DO NOTHING;
INSERT INTO specialties VALUES (1, 'radiology'); INSERT INTO specialties VALUES (1, 'radiology') ON CONFLICT DO NOTHING;
INSERT INTO specialties VALUES (2, 'surgery'); INSERT INTO specialties VALUES (2, 'surgery') ON CONFLICT DO NOTHING;
INSERT INTO specialties VALUES (3, 'dentistry'); INSERT INTO specialties VALUES (3, 'dentistry') ON CONFLICT DO NOTHING;
INSERT INTO vet_specialties VALUES (2, 1); INSERT INTO vet_specialties VALUES (2, 1) ON CONFLICT DO NOTHING;
INSERT INTO vet_specialties VALUES (3, 2); INSERT INTO vet_specialties VALUES (3, 2) ON CONFLICT DO NOTHING;
INSERT INTO vet_specialties VALUES (3, 3); INSERT INTO vet_specialties VALUES (3, 3) ON CONFLICT DO NOTHING;
INSERT INTO vet_specialties VALUES (4, 2); INSERT INTO vet_specialties VALUES (4, 2) ON CONFLICT DO NOTHING;
INSERT INTO vet_specialties VALUES (5, 1); INSERT INTO vet_specialties VALUES (5, 1) ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (1, 'cat'); INSERT INTO types VALUES (1, 'cat') ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (2, 'dog'); INSERT INTO types VALUES (2, 'dog') ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (3, 'lizard'); INSERT INTO types VALUES (3, 'lizard') ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (4, 'snake'); INSERT INTO types VALUES (4, 'snake') ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (5, 'bird'); INSERT INTO types VALUES (5, 'bird') ON CONFLICT DO NOTHING;
INSERT INTO types VALUES (6, 'hamster'); INSERT INTO types VALUES (6, 'hamster') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023'); INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749'); INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763'); INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198'); INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765'); INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654'); INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387'); INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683'); INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435') ON CONFLICT DO NOTHING;
INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487') ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (1, 'Leo', '2000-09-07', 1, 1); INSERT INTO pets VALUES (1, 'Leo', '2000-09-07', 1, 1) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (2, 'Basil', '2002-08-06', 6, 2); INSERT INTO pets VALUES (2, 'Basil', '2002-08-06', 6, 2) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (3, 'Rosy', '2001-04-17', 2, 3); INSERT INTO pets VALUES (3, 'Rosy', '2001-04-17', 2, 3) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (4, 'Jewel', '2000-03-07', 2, 3); INSERT INTO pets VALUES (4, 'Jewel', '2000-03-07', 2, 3) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (5, 'Iggy', '2000-11-30', 3, 4); INSERT INTO pets VALUES (5, 'Iggy', '2000-11-30', 3, 4) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (6, 'George', '2000-01-20', 4, 5); INSERT INTO pets VALUES (6, 'George', '2000-01-20', 4, 5) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (7, 'Samantha', '1995-09-04', 1, 6); INSERT INTO pets VALUES (7, 'Samantha', '1995-09-04', 1, 6) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (8, 'Max', '1995-09-04', 1, 6); INSERT INTO pets VALUES (8, 'Max', '1995-09-04', 1, 6) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (9, 'Lucky', '1999-08-06', 5, 7); INSERT INTO pets VALUES (9, 'Lucky', '1999-08-06', 5, 7) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (10, 'Mulligan', '1997-02-24', 2, 8); INSERT INTO pets VALUES (10, 'Mulligan', '1997-02-24', 2, 8) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (11, 'Freddy', '2000-03-09', 5, 9); INSERT INTO pets VALUES (11, 'Freddy', '2000-03-09', 5, 9) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (12, 'Lucky', '2000-06-24', 2, 10); INSERT INTO pets VALUES (12, 'Lucky', '2000-06-24', 2, 10) ON CONFLICT DO NOTHING;
INSERT INTO pets VALUES (13, 'Sly', '2002-06-08', 1, 10); INSERT INTO pets VALUES (13, 'Sly', '2002-06-08', 1, 10) ON CONFLICT DO NOTHING;
INSERT INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot'); INSERT INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot') ON CONFLICT DO NOTHING;
INSERT INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot'); INSERT INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot') ON CONFLICT DO NOTHING;
INSERT INTO visits VALUES (3, 8, '2009-06-04', 'neutered'); INSERT INTO visits VALUES (3, 8, '2009-06-04', 'neutered') ON CONFLICT DO NOTHING;
INSERT INTO visits VALUES (4, 7, '2008-09-04', 'spayed'); INSERT INTO visits VALUES (4, 7, '2008-09-04', 'spayed') ON CONFLICT DO NOTHING;

View file

@ -5,8 +5,8 @@
# Targeted at system administrators, to avoid touching the context XML files. # Targeted at system administrators, to avoid touching the context XML files.
# Properties that control the population of schema and data for a new data source # Properties that control the population of schema and data for a new data source
jdbc.initLocation=classpath:db/hsqldb/initDB.sql jdbc.initLocation=classpath:db/${db.script}/initDB.sql
jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql jdbc.dataLocation=classpath:db/${db.script}/populateDB.sql
jpa.showSql=true jpa.showSql=true