Few Spring Boot startup errors are more cryptic than this one:
***************************
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 class.
You added spring-boot-starter-data-jpa to your project, hit Run, and immediately hit this wall — before a single line of your own code even executed. The error message sounds technical but the fix is almost always straightforward once you understand what Spring Boot is actually trying to do.
When Spring Boot sees JPA on the classpath, it expects a database to connect to. If it can’t find one — no URL in application.properties, no embedded database dependency, no datasource bean — it fails fast and loud. This article covers every common cause and its exact fix.
Common Causes of “Failed to Configure a DataSource”
1. You Added JPA But Didn’t Configure a Database URL
The most common cause. You added spring-boot-starter-data-jpa to your pom.xml (maybe following a tutorial), but didn’t add any database connection properties. Spring Boot has no idea which database to connect to.
2. The Database Driver Dependency Is Missing
You specified spring.datasource.url in your properties file, but didn’t add the matching JDBC driver jar to your pom.xml. Spring Boot can read the URL but has no driver to actually connect with.
3. H2 Is Missing When Using an In-Memory Database
If you intended to use H2 for local development or testing, but forgot to add the H2 dependency, Spring Boot finds JPA with no embedded database and fails immediately.
4. The Properties File Has Typos or Wrong Key Names
Properties like spring.datasource.url are case and format sensitive. A common mistake is writing spring.datasource.jdbc-url (which is for HikariCP-specific config) instead of spring.datasource.url.
5. You’re Running a Test That Doesn’t Need a Database
Sometimes this error appears only during tests — a unit test class loads the full Spring context (via @SpringBootTest) but the test database isn’t configured in the test profile.
How to Fix It — All Scenarios
Fix 1: Configure a Real Database Connection (MySQL / PostgreSQL)
If you’re connecting to a real database, add all three required properties. Choose whichever config format your project uses:
application.properties (MySQL):
spring.datasource.url=jdbc:mysql://localhost:3306/yourdb
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
application.yml (MySQL):
spring:
datasource:
url: jdbc:mysql://localhost:3306/yourdb
username: root
password: yourpassword
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: update
application.properties (PostgreSQL):
spring.datasource.url=jdbc:postgresql://localhost:5432/yourdb
spring.datasource.username=postgres
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
application.yml (PostgreSQL):
spring:
datasource:
url: jdbc:postgresql://localhost:5432/yourdb
username: postgres
password: yourpassword
driver-class-name: org.postgresql.Driver
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: update
And add the driver dependency to pom.xml:
<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
Fix 2: Use H2 In-Memory Database for Local Development
If you’re in early development and don’t need a real database yet, H2 is the fastest path. Add the dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Spring Boot auto-configures H2 automatically with no additional properties required. Optionally enable the H2 browser console for easy inspection:
application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
application.yml:
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
path: /h2-console
Then open http://localhost:8080/h2-console in your browser after startup.
Fix 3: Exclude DataSource Auto-Configuration (When You Don’t Need a DB)
If you added JPA by mistake or you’re building a module that doesn’t use a database at all, the cleanest fix is to tell Spring Boot not to auto-configure one:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Or via config files — choose your format:
application.properties:
spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
application.yml:
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
Fix 4: Isolate the Issue in Tests With @DataJpaTest or Mocking
If the error only appears in unit tests, don’t load the full context. Use @DataJpaTest for repository tests (it auto-configures an embedded H2 database), or mock your datasource layer:
// For repository/JPA tests — uses embedded H2 automatically
@DataJpaTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
void shouldSaveUser() {
User user = new User("Sree");
userRepository.save(user);
assertThat(userRepository.findAll()).hasSize(1);
}
}
// For service/unit tests — no database needed at all
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
void shouldReturnUser() {
when(userRepository.findById(1L)).thenReturn(Optional.of(new User("Sree")));
User result = userService.getUser(1L);
assertThat(result.getName()).isEqualTo("Sree");
}
}
How to Verify the Fix Worked
After applying your fix, look for these two lines in the startup log — they confirm Spring Boot successfully connected to the database:
HikariPool-1 - Starting...
HikariPool-1 - Start completed.
You can also hit the Actuator health endpoint (if you have spring-boot-starter-actuator on the classpath) to confirm datasource health:
curl http://localhost:8080/actuator/health
A healthy response looks like:
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
}
}
}
}
Quick Diagnosis Reference
| Scenario | Fix |
|---|---|
| Using MySQL / PostgreSQL but no URL configured | Add spring.datasource.url + driver dependency |
| Want a quick local DB with no setup | Add H2 dependency (scope: runtime) |
| App doesn’t need a database at all | Exclude DataSourceAutoConfiguration |
| Only fails in unit tests | Use @DataJpaTest or @MockBean to avoid full context load |
| Properties look correct but still failing | Check for typos — use spring.datasource.url, not jdbc-url |
Conclusion
“Failed to configure a DataSource” is Spring Boot doing its job — it found JPA on the classpath and refused to start without a valid database configuration. The fix is always one of five things: add the database URL, add the driver dependency, add H2 for local dev, exclude DataSource auto-configuration, or fix your test setup. Work through the table above and you’ll be running in minutes.
