CS FundamentalsVery hardProduct-company placement

A dependency-injection container registers ReportCache as a singleton, and ReportCache takes a DbSession in its constructor; DbSession is registered as scoped (one per HTTP request). What goes wrong?

  1. A. Each request gets a fresh ReportCache
  2. B. The container rejects all singletons that have dependencies
  3. C. The singleton captures the first request's DbSession and keeps using it for every later request, after that request's scope ended
  4. D. Nothing; the container substitutes a new DbSession for every method call

Correct answer: C. The singleton captures the first request's DbSession and keeps using it for every later request, after that request's scope ended

A longer-lived object holding a shorter-lived dependency keeps it beyond its intended lifetime (a captive dependency), giving stale or disposed sessions and cross-request data leaks. Inject a factory or make the consumer scoped; some containers detect this at start-up.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic