Interview Prep Zoneby CuriouserLabs

Question 1 of 5 · senior level

What do Spring's data-access abstractions (JdbcTemplate, DataAccessException) actually buy you?

🎤 Say this first

Two things: resource lifecycle discipline — JdbcTemplate acquires/releases connections, statements and result sets correctly in every path (the try-finally pyramid you can't get wrong anymore), participating automatically in Spring transactions; and exception translation — every vendor SQLState/error code becomes a consistent unchecked DataAccessException hierarchy (DuplicateKeyException, OptimisticLockingFailureException…), so business code can react to meaning without vendor coupling. JPA, JDBC and (partly) NoSQL modules all translate into the same hierarchy — that's the portability story.

The full picture

Meaningful exceptions, zero boilerplate
public void register(User u) {
    try {
        jdbc.update("INSERT INTO users(email, name) VALUES (?, ?)",
                    u.email(), u.name());
    } catch (DuplicateKeyException e) {              // translated from vendor code
        throw new EmailAlreadyRegisteredException(u.email(), e);
    }
}
// vs raw JDBC: getConnection/prepare/execute/close x3 in nested finally,
// then: if (e.getSQLState().startsWith("23")) … per vendor. All gone.
  • @Repository activates the translation post-processor for JPA repositories; JdbcTemplate translates natively via SQLErrorCodeSQLExceptionTranslator (sql-error-codes.xml per vendor).
  • Where each tool fits: JdbcTemplate/JdbcClient (3.2+) for explicit SQL, reporting, bulk ops; Spring Data JPA for aggregate CRUD; jOOQ/MyBatis when SQL is the domain language. Senior teams mix deliberately rather than forcing one.
  • The hierarchy is layered: transient (TransientDataAccessException — retry may work: deadlock loser, timeout) vs non-transient — which is exactly what your retry middleware should key on.

🔄 Likely follow-up questions

  • How does exception translation actually detect 'duplicate key' per vendor?
  • JdbcClient vs JdbcTemplate — what improved?
  • When would you drop from JPA to plain SQL in the same service?