简单的问题我可以做这样的事情:
(我不知道另一种方法,因为 NHibernate 不支持嵌套事务)
public class GetNextSequence : IIdentifierGenerator
{
public object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
{
using (var nSession = SessionFactoryWrapper.SessionFactory.OpenSession())
{
using( var tran = nSession.BeginTransaction(System.Data.IsolationLevel.Serializable))
{
var update = nSession.CreateSQLQuery("update counter set last_one = last_one + 1 where item like :item");
update.SetParameter("item", "account_contact.account_contact_id");
update.ExecuteUpdate();
var query = nSession.CreateSQLQuery("SELECT last_one FROM counter WHERE item LIKE :item");
query.SetParameter("item", "account_contact.account_contact_id");
var lastOne = query.UniqueResult();
tran.Commit();
return lastOne;
}
}
}
}
或者这是我不应该做的事情,如果是这样,为什么不呢?