Datasource Configuration

H2 In-Memory (Development)

Example

Zero setup: the database lives in memory and disappears on restart. The H2 console is handy for looking at tables.

application.yml

spring:
  datasource:
    url: jdbc:h2:mem:shopease
    username: sa
    password:
  h2:
    console:
      enabled: true          # http://localhost:8080/h2-console
  jpa:
    hibernate:
      ddl-auto: create-drop  # create tables at startup, drop at shutdown
    show-sql: true

Example output

INFO  HikariPool-1 - Starting...
INFO  HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:shopease user=SA
INFO  HikariPool-1 - Start completed.
INFO  H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:shopease'
Hibernate: create table product (price numeric(10,2) not null, stock integer not null, id bigint generated by default as identity, ...)

MySQL and PostgreSQL

# MySQL 8
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shopease
    username: shopease
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: update
    open-in-view: false

---
# PostgreSQL 16
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/shopease
    username: shopease
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: validate
    open-in-view: false

Example

Start a local MySQL quickly with Docker (see the Docker material):

docker run -d --name shopease-mysql -p 3306:3306 -e MYSQL_DATABASE=shopease -e MYSQL_USER=shopease -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=root mysql:8.4

Example output

3f9c2a71d0b4e8c5a6f1...
ddl-autoWhat Hibernate doesUse
create-dropCreate tables at start, drop at stopTests, H2
updateAdd missing tables/columnsLocal development only
validateCheck tables match entities, change nothingProduction (with Flyway/Liquibase)
noneNothingProduction

Tip: Set spring.jpa.open-in-view=false. Otherwise Spring keeps a database connection open for the whole HTTP request and logs a warning about it at startup.