Fix Spring Boot DataSource URL Errors in 5 Minutes: 5 Root Causes
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver classIf that red stack trace has you panicking—"the code looks fine, why won't it start?"—this post is all you need. Bottom line: this error has exactly five causes. Pick your case, copy-paste, and you're done.
What the Error Message Actually Means
When Spring Boot finds JDBC-related libraries on the classpath, it kicks off DataSourceAutoConfiguration. Here's how that auto-config works:
- Look for a
spring.datasource.urlvalue. - If missing → try falling back to an embedded DB such as H2, HSQLDB, or Derby.
- If no embedded DB is on the classpath either → abort startup with "there is no DataSource I can create."
In other words, this exception is thrown only when both conditions hold: no explicit URL, and no embedded DB to fall back to. So the fix is one of two paths: "provide a URL" or "add an embedded DB."
Diagnose and Fix: 5 Causes
① spring.datasource.url is missing entirely
- Symptom: The most common case. The URL setting is completely missing from yml/properties.
- Check: Search for the
spring.datasource.urlkey insrc/main/resources/application.yml. - Fix: Add the DB-specific config from the copy-paste snippets below, as-is.
② The wrong profile is active (application-prod.yml not loaded)
- Symptom: The URL is in your local yml, but a different profile is active so that config never gets read.
- Check: Look for this line in the boot log.
The following 1 profile is active: "prod"If that doesn't match the profile you expected, that's your culprit.
- Fix: Explicitly specify the intended profile.
java -jar app.jar --spring.profiles.active=prod
# 또는 환경변수
export SPRING_PROFILES_ACTIVE=prodAlso verify that the application-prod.yml filename is exact and that it actually contains a URL.
③ JDBC driver dependency is missing
- Symptom: You did set a URL, but
Failed to determine a suitable driver classappears alongside it. - Check: Confirm the driver actually made it onto the classpath with a command like
./gradlew dependencies | grep -i mysql. - Fix: Add the driver that matches your DB (see the snippets below).
Tip on the message difference: If the driver is present but the URL is missing,
'url' attribute is not specifiedstands out. If the URL is present but the driver is missing,Failed to determine a suitable driver classis emphasized. Use which sentence is highlighted to tell ① from ③.
④ No embedded DB such as H2
- Symptom: You just wanted a quick test, didn't configure any DB, and didn't add H2 either.
- Fix: For test/local use, adding H2 alone is enough—auto-fallback will start the app immediately.
runtimeOnly 'com.h2database:h2'⑤ ${} placeholders are not resolved
- Symptom: You wrote
${DB_URL}in yml, but the env var is empty so an empty string gets injected. - Check: Verify the env var is actually set with
echo $DB_URL. - Fix: Inject it via IntelliJ run configuration Environment variables, Docker
environment:, or the JVM-DDB_URL=...option—or use default-value syntax.
spring:
datasource:
url: ${DB_URL:jdbc:h2:mem:testdb}
password: ${DB_PASSWORD:}With the ${DB_URL:default} form, even if the env var is missing it falls back to the value after the colon, so startup isn't blocked. If your team follows 12-factor config, make this default-value pattern the standard.
Copy-Paste Config Collection
MySQL (Spring Boot 3.x)
Starting with Spring Boot 3.x, the coordinates changed from
mysql-connector-javatomysql-connector-j. Plenty of people get stuck copy-pasting from old posts—watch out!
build.gradle
runtimeOnly 'com.mysql:mysql-connector-j'application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: ${DB_PASSWORD:root}
driver-class-name: com.mysql.cj.jdbc.Driverapplication.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=rootPostgreSQL
build.gradle
runtimeOnly 'org.postgresql:postgresql'application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: postgres
password: ${DB_PASSWORD:postgres}H2 (in-memory, for tests/local)
build.gradle
runtimeOnly 'com.h2database:h2'application.yml
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
enabled: trueMaven users (pom.xml)
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>runtimeOnly vs implementation
| Type | Exposed at compile time | Use for |
|---|---|---|
implementation | Yes | Libraries you import directly in code |
runtimeOnly | No | JDBC drivers and other runtime-only deps |
JDBC drivers are almost never imported in your own code, so runtimeOnly is the canonical choice. It also prevents you from accidentally hard-coding driver class names in source.
Bypass When You Don't Need a DB at All
If you're only building a web API and haven't wired up a DB yet, but this error still fires, just turn auto-config off.
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyApplication { ... }Or via properties:
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration⚠️ Warning: This is the right answer only when you truly won't use a DB. If you'll use even one JPA Repository, exclude is an anti-pattern that just kicks the problem down the road. In that case, put a proper URL in—H2 or a real DB.
A Note from Production Experience
For local development I almost always bring up MySQL/PostgreSQL via docker-compose.yml, and set the URL with a default-value pattern like ${DB_URL:jdbc:h2:mem:testdb}. That way a teammate who hasn't started Docker can clone and boot on H2 immediately, and once Docker is up it attaches to the real DB. That one default-value line is the cheapest way I've found to cut down on "it worked on my machine."
3-Minute Diagnostic Checklist
- Is the
spring.datasource.urlkey present in yml? - Does the active profile in the boot log match what you intended?
- Did the DB driver show up in
dependencies? - If this is for tests, did you add H2?
- Are
${}env vars actually being injected? (default-value syntax recommended)
FAQ
Q. I clearly set the URL and still get the same error.
A. Almost always a profile issue. Check The following profiles are active: in the boot log. If the yml where you put the URL isn't the one that's actually active, that config is ignored.
Q. I added mysql-connector-java and it still doesn't work.
A. In Spring Boot 3.x the coordinates changed to com.mysql:mysql-connector-j. The old coordinates are deprecated—switch to the new ones.
Q. How do I boot the server quickly with no DB?
A. A single runtimeOnly 'com.h2database:h2' line starts you on in-memory H2 immediately. If you'll never use a DB, exclude DataSourceAutoConfiguration.class.
Nodelog는 모든 콘텐츠의 내용과 출처를 공개 전에 검토합니다. 환경(OS·버전)에 따라 결과가 달라질 수 있는 기술 정보는 공식 문서와 함께 확인하며, 검토 기준과 정정 원칙은 편집 정책에서 안내합니다. 오류를 발견하시면 이메일로 제보해 주세요 — 확인 후 신속히 정정합니다.
Comments
Be the first to comment.