开发者

NHibernate and Raw ADO.NET usage

Background: I am using Nhibernate in an ASP.NET MVC application with an open-session-in-view pattern and I need to use raw ADO.NET to execute some performance-critical database operations.

I'm somewhat confused about how I should be getting my connection instance as I've seen two different methods in numerous blog posts.

Do I want to use:

var connection = Session.Connection;

Or:

var connection = ((ISessionFactoryImplementor)sessionFac开发者_JAVA百科tory).ConnectionProvider.GetConnection();

I can't seem to find a conclusive answer anywhere and I'm hoping that someone with some extensive NHibernate experience can chime in here.


If you already have a session, use the connection from it.

That will also allow you to share the transaction (if one is open) by enlisting your commands on it.


i'm using something in the lines of (also uses the underlying already-open transaction)

SqlCommand command = new SqlCommand(updateString, (SqlConnection)NHibernateSession.Connection);
command.Parameters.AddRange(parameters.ToArray());

try
{
    ITransaction tx = NHibernateSession.Transaction;
    tx.Enlist(command);
    command.ExecuteNonQuery();
}
catch (SqlException)
{
    NHibernateSessionManager.Instance.RollbackTransaction();
    throw;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜