RadhaPublication
HomeInterviewLearning PathsRole TracksMockBooksPricing
Login / Sign UpLog in

© 2026 Radha Publication. All rights reserved.

Shop on AmazonDonateContactPrivacyTerms
Spring Boot questions
MediumSpring Boot#267 min readJul 11, 2026

How is DataSource created automatically?

spring-boot
datasource
auto-configuration
Practice modeTest yourself instead of reading straight through

Hook: Think of Spring Boot as a hotel concierge: if you give it the right room key and address, it quietly sets up the database desk for you.

Question: How is DataSource created automatically?

Answer: Spring Boot creates a DataSource through auto-configuration. If the right JDBC classes are on the classpath and you provide spring.datasource.* settings, Boot binds them into DataSourceProperties and builds a pooled connection source for you. If you do not set a URL but an embedded database like H2 is available, Boot can create an in-memory DataSource instead. If you define your own DataSource bean, Boot steps aside and uses yours.

Interview-Ready Answer: In Spring Boot, DataSource is created by auto-configuration, mainly through DataSourceAutoConfiguration. Boot checks the classpath, binds spring.datasource.* properties into DataSourceProperties, and then creates a pooled implementation such as HikariDataSource by default; if no explicit URL is set but an embedded database is available, it can create that instead. The key point is that Boot is conditional: it creates the bean only when needed, and it backs off immediately if I define my own DataSource bean.

🧠 Memory Map
Memory map — visual summary of this topic

What Boot is actually creating

DataSource is just the contract for getting database connections. In real apps, Boot usually creates a pool implementation, which is a reusable cache of open connections. A pool matters because opening a database connection is slow compared with reusing one.

Under the hood: the mental replay

  1. Spring Boot starts the application context and imports auto-configuration classes.
  2. DataSourceAutoConfiguration becomes eligible only when JDBC is present and Boot does not already find a user-defined DataSource bean.
  3. Boot binds configuration values from spring.datasource.* into DataSourceProperties. This is simple property binding: text values in properties or YAML are mapped onto Java fields.
  4. If you set spring.datasource.url, Boot treats this as an external database setup and builds a pooled DataSource using the pool library on the classpath.
  5. If HikariCP is present, Boot prefers HikariDataSource. If not, it can fall back to Tomcat JDBC pool or Apache DBCP2 depending on what is available.
  6. If no URL is provided and Boot detects an embedded database driver such as H2, HSQLDB, or Derby, it creates an embedded DataSource for test and local use.
  7. Once the bean exists, the rest of the stack, such as JdbcTemplate, Spring Data JDBC, or JPA, can auto-wire it and start using it.

Why Boot chooses this design

Boot is trying to make the common case zero-work: one dependency, a few properties, and a working connection pool. The pool choice is hidden because most teams should depend on the DataSource interface, not a specific vendor class. That keeps your code portable and lets Boot optimize the default.

Auto-config vs manual config

ApproachWho creates itGood forGotcha
Auto-configBootFast startupBacks off if bean exists
Manual beanYouSpecial tuningMore code to maintain
Embedded DBBootTests, demosNot production-grade

Performance and defaults

Bean creation itself is effectively O(1) from your code’s point of view; the expensive part is establishing the first real DB connection. With HikariCP, a common default is a maximum pool size of 10 connections and a connection timeout of 30000 ms (30 seconds). Hikari is popular because it is lightweight and fast to borrow a connection from.

Important edge cases

  • If you define your own DataSource bean, Boot will not create another one.
  • If you have multiple pools on the classpath and do not set a type, Boot picks its preferred one, usually Hikari.
  • If there is no driver and no embedded database available, startup fails with a message like “Failed to determine a suitable driver class”.
  • If the spring.datasource.url is wrong, the bean may be created, but the first connection attempt fails later with a connection error.

Memory model: Boot does not “magically invent” a database connection. It simply reads your settings, chooses a pool, and registers a bean when the conditions are right.

Real-world story

Imagine a checkout service in an e-commerce system. In local development, the team uses H2 so they can start the app quickly. In production, the same code reads spring.datasource.url, username, and password from secrets, and Boot creates a Hikari pool automatically.

One day an engineer removes the JDBC starter from the build, assuming JPA will still be enough. The service starts failing at boot with messages about not finding a suitable driver or not being able to configure a DataSource. Customers see 500 errors on checkout, health checks turn red, and the logs show startup failures before the app even finishes initialization.

A different outage is subtler: someone adds a custom DataSource bean for reporting, but forgets to mark the right bean as primary. Now the app has two candidates, and Spring cannot decide which one to inject. The symptom becomes NoUniqueBeanDefinitionException, and the app fails before serving traffic. This is why interviewers care: a tiny misunderstanding of auto-configuration can take a production system down at startup.

Spring Boot
// Requires: spring-boot-starter-jdbc and com.h2database:h2 on the classpath.
// Boot will auto-create the DataSource because we provide spring.datasource properties.
// The code also shows a failure path: the DataSource exists, but a missing table still fails.

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.Map;

@SpringBootApplication
public class DataSourceAutoConfigDemoApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DataSourceAutoConfigDemoApplication.class)
                .properties(Map.of(
                        "spring.datasource.url", "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
                        "spring.datasource.username", "sa",
                        "spring.datasource.password", "",
                        "spring.datasource.driver-class-name", "org.h2.Driver"
                ))
                .run(args);
    }

    @Bean
    ApplicationRunner runner(DataSource dataSource, JdbcTemplate jdbcTemplate) {
        return args -> {
            System.out.println("Auto-created DataSource bean: " + dataSource.getClass().getName());

            if (dataSource instanceof HikariDataSource hikari) {
                System.out.println("Hikari max pool size (default is 10 unless overridden): " + hikari.getMaximumPoolSize());
            }

            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name VARCHAR(50))");
            jdbcTemplate.update("INSERT INTO users (id, name) VALUES (?, ?)", 1, "Asha");

            String name = jdbcTemplate.queryForObject("SELECT name FROM users WHERE id = ?", String.class, 1);
            System.out.println("Query result: " + name);

            try {
                jdbcTemplate.queryForObject("SELECT COUNT(*) FROM missing_table", Integer.class);
            } catch (DataAccessException ex) {
                System.out.println("Edge case: the DataSource is fine, but the schema is not: " + ex.getClass().getSimpleName());
            }
        };
    }
}

Follow-up & Tricky Questions:

  • How does Boot choose Hikari over other pools? Boot checks what is on the classpath and prefers HikariCP when it is available. If Hikari is absent, it can fall back to other supported pools such as Tomcat JDBC or DBCP2.
  • What happens if I define my own DataSource bean? Boot backs off because the auto-config is conditional on @ConditionalOnMissingBean. Your bean becomes the one used by JdbcTemplate, JPA, and the rest of the app.
  • How does Boot know which URL, username, and password to use? It binds values from spring.datasource.* through DataSourceProperties, which reads application properties, YAML, environment variables, and command-line arguments.
  • Can Boot create a DataSource without any database dependency? No. It needs a JDBC driver or an embedded database implementation on the classpath; otherwise startup fails because it cannot determine how to connect.
  • How do you configure multiple DataSources? You create them manually, mark one as @Primary if needed, and wire each one into the beans that should use it. Auto-configuration is usually disabled for that setup.
  • Does auto-created mean no pool? No. In real Boot apps, auto-created usually means a pooled DataSource, not a raw DriverManagerDataSource.
  • Is DataSource the same as the database? No. It is only the Java object that opens connections to the database; the actual database is a separate server or embedded engine.
  • Tricky: If the app starts, does that prove the database is healthy? Not always. The bean may exist even when the network, credentials, or schema are wrong; real connectivity is proven only when the app borrows a connection or runs a query.
  • Tricky: Does Boot always create an embedded database in tests? No. It does that only when an embedded driver is available and the configuration allows it. If you point tests at a real database URL, Boot will use that instead.

Common Mistakes:

  • Thinking Boot creates a DataSource from nothing. Correction: it needs the right classes and usually a URL or embedded driver.
  • Depending on HikariDataSource everywhere. Correction: code against DataSource; the pool implementation is an internal choice.
  • Forgetting that a user-defined bean disables auto-config. Correction: if you write your own @Bean DataSource, Boot will back off.
  • Assuming startup success means connection success. Correction: the app can start and still fail on the first query if the database is down or the schema is missing.

Memory Hook: “Give Boot a driver and an address, and it builds the checkout desk.” Driver plus address means connection pool; no address plus embedded DB means local sandbox.

Cheat Sheet:

  • DataSourceAutoConfiguration is the main auto-config class.
  • Boot binds spring.datasource.* into DataSourceProperties.
  • HikariCP is the default pool when present.
  • If no URL exists and an embedded DB is present, Boot can create one.
  • User-defined DataSource beans make Boot back off.
  • Common failure: missing driver, wrong URL, or multiple beans.

Practice Tasks:

  • Run a small Spring Boot app with H2 and print the actual DataSource class name.
  • Remove the H2 dependency and observe the startup failure message.
  • Add your own @Bean DataSource and confirm that Boot stops auto-creating one.
Previous
Back to Questions←→to navigate
Next

Recommended Resources

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

// Requires: spring-boot-starter-jdbc and com.h2database:h2 on the classpath. // Boot will auto-create the DataSource because we provide spring.datasource properties. // The code also shows a failure path: the DataSource exists, but a missing table still fails. import com.zaxxer.hikari.HikariDataSource; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; import java.util.Map; @SpringBootApplication public class DataSourceAutoConfigDemoApplication { public static void main(String[] args) { new SpringApplicationBuilder(DataSourceAutoConfigDemoApplication.class) .properties(Map.of( "spring.datasource.url", "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "spring.datasource.username", "sa", "spring.datasource.password", "", "spring.datasource.driver-class-name", "org.h2.Driver" )) .run(args); } @Bean ApplicationRunner runner(DataSource dataSource, JdbcTemplate jdbcTemplate) { return args -> { System.out.println("Auto-created DataSource bean: " + dataSource.getClass().getName()); if (dataSource instanceof HikariDataSource hikari) { System.out.println("Hikari max pool size (default is 10 unless overridden): " + hikari.getMaximumPoolSize()); } jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name VARCHAR(50))"); jdbcTemplate.update("INSERT INTO users (id, name) VALUES (?, ?)", 1, "Asha"); String name = jdbcTemplate.queryForObject("SELECT name FROM users WHERE id = ?", String.class, 1); System.out.println("Query result: " + name); try { jdbcTemplate.queryForObject("SELECT COUNT(*) FROM missing_table", Integer.class); } catch (DataAccessException ex) { System.out.println("Edge case: the DataSource is fine, but the schema is not: " + ex.getClass().getSimpleName()); } }; } }