Nhibernate session.BeginTransaction() vs. transaction.Begin()

雨燕双飞 提交于 2020-01-01 10:00:09

问题


My question is related to use of nhibernate transactions

Is the transaction.Begin() below a problem? or just redundant?

using (var transaction = session.BeginTransaction())
{
    transaction.Begin();
    action();
    transaction.Commit();
}

回答1:


After checking the source, transaction.Begin() is in fact redundant - a harmless no-op.

link to source




回答2:


beginTransaction as it states begins a transaction,

on the other hand

session.Transaction will return the current active transaction if null will create one.

The NHibernate session tracks the current active (not committed or rolled back) transaction.

If you call ISession.Transaction when no transaction has yet been created yet during the life time of the session, the session will create a new transaction object at that point in time, but won't begin it yet. When you call ISession.BeginTransaction, the session will see if their is already a transaction object that has been created before, but not yet completed. If so, it will return this transaction object. If not, it will create a new transaction object, begin it and store a reference to this new object.

On transaction completion, the transaction object notifies the session to which it belongs that it has completed, on which the session will release its reference to the transaction object. Any following call to ISession.Transaction or ISession.BeginTransaction will then cause the creation of a new transaction object.

NHibernate does not support reuse of transaction objects for more than one transaction (this behaviour may be different from Hibernate, which does seem to support reuse of transaction objects).

Refer this document.



来源:https://stackoverflow.com/questions/16856499/nhibernate-session-begintransaction-vs-transaction-begin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!