nhibernate connection release modes: why does the documentation recommened using "after_transaction"?
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 aft开发者_运维知识库er_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?
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.
精彩评论