While creating a repository by using NHibetnate(or, whatever), should I keep explicit BeginTransaction(), Commit(), Rollback() like the following?
public class NHibernateRepository<T> : IRepository<T>
{
private NHibernate.ISession _session;
public NHibernateRepository()
{
_session = NHibernateSessionManager.Instance.GetSession();
_session.BeginTransaction();
}
public void Save(T obj)
{
_session.Save(obj);
}
... ... ... ... ... ... ...
public void Commit()
{
if (_session.Transaction.IsActive)
{
_session.Transaction.Commit();
}
}
public void Rollback()
{
if (_session.Transaction.IsActive)
{
_session.Transaction.Rollback();
_session.Clear();
}
}
public void BeginTransaction()
{
Rollback();
_session.BeginTransaction();
}
}
Or, should I write my persistence methods like this?
public class Repository<T> : IRepository<T>
{
ISession _session;
public Repository()
{
_session = SessionFactoryManager.SessionFactory.OpenSession();
}
private void Commit()
{
if (_session.Transaction.IsActive)
{
_session.Transaction.Commit();
}
}
private void Rollback()
{
if (_session.Transaction.IsActive)
{
_session.Transaction.Rollback();
_session.Clear();
}
}
private void BeginTransaction()
{
_session.BeginTransaction();
}
void IRepository<T>.Save(T obj)
{
try
{
this.BeginTransaction();
_session.Save(obj);
this.Commit();
}
catch (Exception ex)
{
this.Rollback();
throw ex;
}
}
... ... ... ... ... ... ...
}
}
Any suggestion? Can anyone compare these two approaches?