Skip to content

Instantly share code, notes, and snippets.

@FabianoCampos
Created March 20, 2020 12:11
Show Gist options
  • Save FabianoCampos/e14b956299b696632049b84f2be5016b to your computer and use it in GitHub Desktop.
Save FabianoCampos/e14b956299b696632049b84f2be5016b to your computer and use it in GitHub Desktop.
Configurações para NHibernate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
namespace ConfiguracaoNHibernate
{
public class Configuracao
{
private ISession sessao;
public Configuracao()
{
CriarSessao();
}
public ISession Sessao
{
get
{
if (sessao.Connection.State == System.Data.ConnectionState.Closed)
{
RenovarSession();
}
return sessao;
}
}
public bool TransacaoEstaAtiva
{
get
{
return Sessao.Transaction != null && Sessao.Transaction.IsActive;
}
}
public void RenovarSession()
{
Dispose();
CriarSessao();
}
/// <summary>
/// Inicia uma transação no BD.
/// </summary>
public void BeginTransaction()
{
Sessao.Transaction.Begin(System.Data.IsolationLevel.ReadCommitted);
}
/// <summary>
/// <see cref="Comita"/> a transação corrente.
/// </summary>
public void Commit()
{
Sessao.Flush();
if (Sessao.Transaction.WasCommitted == false)
{
Sessao.Transaction.Commit();
}
Sessao.Clear();
}
/// <summary>
/// Cancela a transação corrente.
/// </summary>
public void Rollback()
{
Sessao.Transaction.Rollback();
Sessao.Clear();
}
/// <summary>
/// Método para atender a interface <see cref="IDisposable"/>.
/// </summary>
public void Dispose()
{
sessao.Close();
sessao.Dispose();
}
/// <summary>
/// Abertura da sessão.
/// </summary>
private void CriarSessao()
{
sessao = NHibernateHelper.OpenSession();
////Sessao.FlushMode = FlushMode.Commit;
sessao.FlushMode = FlushMode.Commit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment