Scrivio Interview library

JDBC and Spring JDBC

32 questions in 9 subtopics.

JDBC API

ConceptFoundations7 min read
What is the auto-commit mode of a JDBC Connection, and how does Spring's transaction management interact with it?
CodingFoundations7 min read
Write plain JDBC code that reads all orders for a customer using a DataSource, and explain which resources must be closed and how.
DebuggingIntermediate7 min read
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?
LeadershipPrincipal6 min read
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

CodingFoundations6 min read
Find and fix the bug in this repository method: jdbcTemplate.query("select * from users where name = '" + name + "' order by " + sortColumn, mapper).
InternalsIntermediate7 min read
Besides preventing SQL injection, what performance benefits can PreparedStatement bring, and what driver settings affect them?
ScenarioIntermediate7 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
What does JdbcTemplate do for you compared with plain JDBC, and what exception does queryForObject throw when no row matches?
CodingFoundations6 min read
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.
DesignIntermediate7 min read
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?
LeadershipAdvanced6 min read
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

CodingFoundations7 min read
Insert 100,000 rows efficiently using JdbcTemplate. Show the API and explain the driver setting that matters for MySQL and PostgreSQL.
DebuggingIntermediate7 min read
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?
ScenarioAdvanced7 min read
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

ConceptFoundations7 min read
Why does a Spring Boot application use a connection pool, which pool is the default, and what do maximumPoolSize, connectionTimeout and maxLifetime control?
DebuggingFoundations8 min read
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.
DebuggingIntermediate7 min read
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?
DesignAdvanced7 min read
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?
LeadershipPrincipal7 min read
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

ConceptIntermediate7 min read
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.
DebuggingIntermediate7 min read
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.
LeadershipAdvanced7 min read
Teams keep raising database pool sizes to fix timeouts, and the shared database's performance is degrading. What policy and tooling would you introduce?
DesignPrincipal7 min read
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

CodingFoundations7 min read
A Spring Boot service needs a second DataSource 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.
DebuggingIntermediate7 min read
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.
DesignAdvanced7 min read
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

ConceptFoundations7 min read
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?
DebuggingIntermediate6 min read
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

CodingFoundations7 min read
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 ${}.
DesignIntermediate7 min read
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.
ScenarioAdvanced7 min read
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?