2V0-72.22PSE · Question #33
When does the JdbcTemplate class acquire a database connection?
The correct answer is D. None of the above. Option D is correct because none of the listed choices accurately describe JdbcTemplate's connection behavior: it acquires a connection lazily, on a per-operation basis directly from the configured DataSource (via DataSourceUtils.getConnection()), and immediately releases it…
Question
When does the JdbcTemplate class acquire a database connection?
Options
- AWhen one of its execute method is called
- BDuring JdbcTemplate instantiation
- CDuring application startup
- DNone of the above.
How the community answered
(29 responses)- A7% (2)
- B3% (1)
- C14% (4)
- D76% (22)
Explanation
Option D is correct because none of the listed choices accurately describe JdbcTemplate's connection behavior: it acquires a connection lazily, on a per-operation basis directly from the configured DataSource (via DataSourceUtils.getConnection()), and immediately releases it back to the pool after each operation completes.
Why the distractors are wrong:
- A is the closest but misleading - connection acquisition isn't limited to methods named
execute; it also happens throughquery(),update(),batchUpdate(), and others. No single method name captures this. - B is wrong because JdbcTemplate's constructor only stores a reference to the
DataSource; no connection is opened yet. - C is wrong because Spring does not eagerly open database connections at startup for JdbcTemplate; this would waste pool resources.
Memory tip: Think of JdbcTemplate as a frugal borrower - it only checks out a connection from the DataSource the moment it needs to run SQL, then immediately returns it. It never holds a connection open between operations.
Topics
Community Discussion
No community discussion yet for this question.