NHibernate connection.release_mode shift from auto to after_statement for lazy-loading sessions
0 reputation · 26 Jul 2021, 09:54 UTC
0 reputation · 26 Jul 2021, 09:54 UTC
NHibernate delegates connection checkout and return to the underlying ADO.NET provider pool. The connection.release_mode setting determines when a checked-out connection is handed back, with options on_close, after_transaction, after_statement, and auto (default). When sessions engage in lazy loading outside an explicit transaction, the connection may be held beyond the immediate operation, depending on the chosen release mode.
The accepted defaults and behavior of release_mode have shifted across NHibernate major versions, and the interaction with provider-specific pooling such as SqlClient connection lifetime or Npgsql session pooling is not uniformly documented. after_statement releases the connection after every statement, which can reduce pool hold time for lazy loads but introduces checkout return churn and may interact poorly with batching second-level cache or providers that penalize frequent checkouts. Conversely, retaining auto or after_transaction risks long-lived sessions accumulating connections under sustained load.
Given a workload where lazy loading occurs frequently outside explicit transactions, does transitioning connection.release_mode to after_statement demonstrably stabilize connection pool occupancy? What are the observed trade-offs with cache coherence and provider-specific pooling semantics? How does the chosen current_session_context_class influence the effectiveness of this configuration change across NHibernate versions?
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.