我有一個問題,我的插入與NHibernate插入靜默失敗與NHibernate
交易始好, 我的選擇正確, 我從序列中選擇下一個值也 並提交事務, 但是,沒有插入出現在我的NHprofiler中,並且沒有錯誤出現。 我使用Session.OpenSession(ReadCommited)&交易 任何想法會發生什麼?
代碼
class NHUnitOfWok : INHibernateUnitOfWork
{
private readonly ISession _session;
private bool _isDisposed;
private IsolationLevel _isolationLevel;
public NHUnitOfWok(ISession session)
{
_session = session;
_session.FlushMode = FlushMode.Never;
_isolationLevel = IsolationLevel.ReadCommitted;
}
internal ISession Session
{
get { return _session; }
}
public void SaveChanges()
{
Session.Flush();
}
public void CancelChanges()
{
Session.Clear();
}
public void Commit()
{
Session.Transaction.Commit();
}
public void Rollback()
{
Session.Transaction.Rollback();
}
public void WithinNewSession(Action<ISession> actionToExecute, IsolationLevel? isolationLevel = null)
{
using (var tempSession = Session.SessionFactory.OpenSession())
{
using (var transaction = tempSession.BeginTransaction(isolationLevel ?? _isolationLevel))
{
actionToExecute(tempSession);
transaction.Commit();
}
}
}
public void WithinTransaction(Action action, IsolationLevel? isolationLevel = null)
{
Enforce.NotNull(action, "action");
WithinTransaction<object>(() =>
{
action();
return null;
});
}
public T WithinTransaction<T>(Func<T> func, IsolationLevel? isolationLevel = null)
{
Enforce.NotNull(func, "func");
if (Session.Transaction != null && Session.Transaction.IsActive)
{
return func.Invoke();
}
using (var localTran = Session.BeginTransaction(isolationLevel ?? _isolationLevel))
{
try
{
var funcRes = func.Invoke();
localTran.Commit();
return funcRes;
}
catch (TransactionException ex)
{
throw new DataException(Resource.TransactionException, ex);
}
catch (Exception ex)
{
if (Session.Transaction.IsActive)
localTran.Rollback();
throw new DataException(Resource.TransactionException, ex);
}
}
}
public bool IsStarted()
{
return Session.Transaction != null && Session.Transaction.IsActive;
}
public void Start()
{
if (Session.Transaction == null || !Session.Transaction.IsActive)
{
Session.BeginTransaction(_isolationLevel);
}
}
private void Dispose(bool disposing)
{
if (!disposing || _isDisposed)
{
return;
}
_isDisposed = true;
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
請發送郵編。 – Claudius
您用於添加實體的代碼也是必需的,但目前未顯示。 –
它從序列中選擇我的下一個val,但在我的交易中沒有插入,非常奇怪 – Arnaud