简体   繁体   中英

NHibernate: Read uncommited columns

Is it possible to read (ie select) columns in the same transaction where they are written to the db in?

My code looks like this:

using (IGenericTransaction trans = m_GenericSession.BeginTransaction())
{
    //persist a column to table tExample with Id = 1

    WriteSubItems();
    trans.Commit();
}

public void WriteSubItems()
{
    IDbTransaction trans = GetTransaction();
    //SELECT column from tExample with Id = 1 
}

where WriteSubItems performs some logic which is based on reading the columns written in the transaction before.

I tried:

 public IDbTransaction GetTransaction()
    {
        IDbCommand cmd = Session.Connection.CreateCommand())
        cmd .Connection = Session.Connection;
        Session.Transaction.Enlist(cmd );

        return command.Transaction;            
    }

But no success. Trying to read tExample always returns empty result set. When I call Session.Transaction.Commit() all the rows are added to db, but still transaction is not correctly passed to command.Transaction or it is not used there

If you are wanting to read updates you have made within a transaction these should already be available to you in memory if you are using the same session that is doing the updates. If you however want to read uncommitted rows via another session you will probably have to use a different isolation level in your transaction.

ITransaction transaction = session.BeginTransaction(System.Data.IsolationLevel.<insert correct isolation level here>);

Read more about isolation levels here: https://msdn.microsoft.com/en-us/library/system.data.isolationlevel%28v=vs.110%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM