JDBC and Spring JDBC
32 questions in 9 subtopics.
JDBC API
What is the auto-commit mode of a JDBC Connection, and how does Spring's transaction management interact with it?Write plain JDBC code that reads all orders for a customer using aDataSource, and explain which resources must be closed and how.A report query on PostgreSQL that returns 5 million rows makes the service run out of memory even though the code processes rows one by one with a ResultSet loop and setFetchSize(1000). Why?JDBC has been the base of Java database access since 1997 and every ORM sits on it. What does its longevity teach about choosing foundational APIs and abstractions for a platform?PreparedStatement and SQL injection
Find and fix the bug in this repository method:jdbcTemplate.query("select * from users where name = '" + name + "' order by " + sortColumn, mapper).Besides preventing SQL injection, what performance benefits can PreparedStatement bring, and what driver settings affect them?A security scan reports SQL injection in a search feature that builds dynamic WHERE clauses with optional filters, LIKE patterns and an IN list. How do you build such queries safely in Spring?SQL injection keeps appearing in your code audits years after it became a well-known risk. What does that say about relying on developer knowledge, and how would you remove the class of bug at organisation level?JdbcTemplate and JdbcClient
What doesJdbcTemplate do for you compared with plain JDBC, and what exception does queryForObject throw when no row matches?Using Spring's JdbcClient (Spring Framework 6.1+), write code that loads one Customer by id as an Optional and inserts a customer returning the generated key.A team uses JPA everywhere but has slow reporting queries and complex upserts. When would you mix JdbcTemplate or JdbcClient into a JPA service, and what pitfalls must you watch?Some teams want to use only JdbcClient and plain SQL instead of JPA for all new services. How would you evaluate allowing this as an approved option?Batch updates
Insert 100,000 rows efficiently usingJdbcTemplate. Show the API and explain the driver setting that matters for MySQL and PostgreSQL.A batch insert of 50,000 rows fails with a BatchUpdateException at row 30,000 because of a duplicate key. What state is the database in, and how would you design the job to handle such failures?Nightly bulk loads of hundreds of millions of rows into PostgreSQL are taking longer each month and now overlap with business hours. What options would you evaluate?Connection pooling with HikariCP
Why does a Spring Boot application use a connection pool, which pool is the default, and what domaximumPoolSize, connectionTimeout and maxLifetime control?Your service logs 'HikariPool-1 - Connection is not available, request timed out after 30000ms' under load. List the likely causes and how you would confirm each.After a database failover, a service keeps failing with broken-connection errors for several minutes before recovering. How do pool settings influence recovery, and what would you change?You have 80 services, each with 20 instances and a pool of 10 connections, connecting to one PostgreSQL cluster with max_connections set to 500. What problems do you foresee and what architecture would you propose?Your company is moving databases to a managed cloud service and considering serverless database offerings that scale connections and compute automatically. What long-term questions would you ask about cost, connection management and exit?Pool sizing
How do you size a HikariCP pool for a service? Discuss the formula from the HikariCP documentation, Little's law and why a bigger pool can make things slower.A service deadlocks under load: all threads wait for connections, and none are released. Each request starts a transaction and then calls a method annotated with@Transactional(propagation = REQUIRES_NEW). The pool size is 10. Explain and fix.Teams keep raising database pool sizes to fix timeouts, and the shared database's performance is degrading. What policy and tooling would you introduce?Your database estate has grown to 400 databases with no shared view of capacity, connection budgets or cost. What long-term capacity management approach would you set?DataSource configuration and multiple DataSources
A Spring Boot service needs a secondDataSource for a reporting database next to the main one. Show the configuration, and explain what auto-configuration stops doing once you define your own DataSource bean.A service with two DataSources and JPA on the primary one writes to the secondary database inside an @Transactional method, but those writes are not rolled back when the method throws. Explain and fix.Many services read from replicas and write to a primary. Design read/write splitting at the DataSource level in Spring, including the risks.Embedded H2 database
Spring Boot starts an in-memory H2 database when it is on the classpath and no URL is set. How does this work, how do you load a schema, and why is H2 a poor substitute for PostgreSQL in tests?A Spring Boot 3 test suite uses H2 and a Flyway migration with PostgreSQL-specific syntax: ALTER TABLE orders ADD COLUMN data jsonb. Tests fail. A teammate suggests a separate H2 migration folder. What do you recommend?MyBatis SQL mapping
Write a MyBatis mapper interface in Spring Boot that selects orders by customer id with an optional status filter, mapping snake_case columns to a Java record. Explain #{} versus ${}.A team with complex reporting SQL is choosing between MyBatis,JdbcClient and jOOQ for a new service. Compare them for type safety, dynamic SQL, testing and long-term maintenance.You inherit 200 MyBatis XML mapper files where many queries use ${} for filters and sort columns, and a penetration test found SQL injection. How do you fix and prevent this at scale?No question matches these choices.