Sub topic 3
Configuring the database connection
This chapter assumes you can read a Spring Boot application.properties file and know what JDBC is. You should also know basic SQL and have the Maven project from the previous chapter. By the end, the application will connect to PostgreSQL with credentials kept out of the source code.
How Boot finds your database
Spring Boot auto-configuration creates a DataSource bean when it finds a JDBC driver on the classpath and connection settings in your properties. By default the DataSource is a HikariCP connection pool. Hibernate, the JPA provider, borrows connections from this pool for each transaction.
Adding the driver
The starters from the previous chapter include H2 only. If you set no spring.datasource.url, Boot starts an embedded in-memory H2 database. That is convenient for a first run, but the data disappears when the application stops. For PostgreSQL, add the driver. Boot's parent manages its version.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>The connection properties
Put these settings in src/main/resources/application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/books
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.open-in-view=falseEach line does one job:
spring.datasource.urlis the JDBC URL: driver type, host, port, and database name. Boot picks the driver class from the URL.usernameandpassworduse${...}placeholders. Spring fills them from environment variables at startup, so secrets never enter version control.ddl-auto=validatemakes Hibernate compare your entities with the existing tables at startup and fail if they differ. It never changes the schema.open-in-view=falseturns off a web filter that keeps the persistence session open until the response is written. Without it, database work stays inside your service's transactions, and the connection returns to the pool sooner.
Set the variables before you start the app:
export DB_USER=books_app
export DB_PASSWORD=change-me
./mvnw spring-boot:runChoosing a `ddl-auto` value
| Value | Behavior | Typical use |
|---|---|---|
| `none` | Hibernate does nothing | Production with migrations |
| `validate` | Checks that tables match entities | Production and staging |
| `update` | Adds missing tables and columns | Quick prototypes only |
| `create-drop` | Builds the schema at start, drops it at shutdown | Tests with embedded databases |
Common errors
Could not resolve placeholder 'DB_USER': the environment variable is not set in the shell or IDE run configuration that launched the app.Connection refused: PostgreSQL is not running, or the host and port in the URL are wrong.password authentication failed: the credentials do not match a database role. Check the role and its password.Schema-validation: missing table [book]: the table is missing, or its name differs from what Hibernate expects. Hibernate maps theBookclass tobookby default.
Exercise
You start the application and it fails with "Schema-validation: missing table [book]". The PostgreSQL connection itself works. Why does this happen, and how do you fix it without changing ddl-auto?
Show answer
ddl-auto=validate only checks the schema and never creates tables. The book table does not exist yet, so validation fails. Create the table before startup, for example with a Flyway migration or a one-time SQL script, with columns that match the entity (id, title, author).