Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nhibernate connection release modes: why does the documentation recommened using "after_transaction"?

Tags:

nhibernate

The hibernate documentation states the following:

The configuration parameter hibernate.connection.release_mode is used to specify which release mode to use. The possible values: *auto (the default) - equivalent to after_transaction in the current release. It is rarely a good idea to change this default behavior as failures due to the value of this setting tend to indicate bugs and/or invalid assumptions in user code. *on_close - says to use ConnectionReleaseMode.OnClose. This setting is left for backwards compatibility, but its use is highly discouraged. ...

I have created an integration test that provokes a StaleObjectException by opening two sessions at the same time and manipulating the same entity. To ensure that the test rolls-back everything upon completion, the test content is placed within a TransactionScope; this causes distributed transactions to kick in because two sessions will open a db connection against the same ambient transaction. I want to change the default ConnectionReleaseMode setting to "OnClose", but as stated above, the documentation does not recommend this. Can anyone explain why its not a good idea to change the default behaviour?

like image 600
Marius Avatar asked Jan 01 '10 17:01

Marius


1 Answers

Ok since no one else is bothering I'll try to answer this myself :-) If you use a pattern where transactions are commited when the session is disposed (one transaction=one session) you might as well use the "OnClose" release mode. If you use patterns where your session spans multiple transactions (for example conversation per business transaction http://dotnetchris.wordpress.com/2009/01/27/conversation-per-business-transaction-using-postsharp-and-ioc/), using "OnClose" would hog uneccesary resources because you are not releasing your connections to the connection pool when commiting your transactions. The default "after_transaction" release mode will release your connection when commiting the transaction, thus freeing up your precious database connections.

like image 157
Marius Avatar answered Nov 06 '22 14:11

Marius