Info Desk: Nhibernate exception - Transaction not connected, or was disconnected

Please refer to following link to create sample table and nihbernate app. To Create Basic Structure

Now as per my investigation above error "Transaction not connector or was disconnected" , this comes when transaction object is not disposed or null but connection is null same time.

Following is the code snap from AdoTransaction.cs from nhibernate repo. Original Content

transaction.png

Now how to reproduce it.

  • First create sample structure of application based on link specified above.

  • Now at SQL server level try to create trigger for table. ( Here Table NN is not exists so it will throw exception).

CREATE TRIGGER InsertTriggerForNewTable
   ON  NewTable
   AFTER INSERT
AS 
BEGIN    
    SET NOCOUNT ON;
    SELECT * FROM NN
END
GO
  • Sample code
static void Main(string[] args)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var transaction = session.BeginTransaction();
                try
                {
                    string data = string.Join("", Enumerable.Repeat(1, 2001).ToArray()); ;
                    Console.WriteLine(data.Length);
                    var customer = new NewTable
                    {
                        Text1 = data,
                        Test2 = "text"
                    };
                    session.Save(customer); // This line throw exception.
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    transaction.Rollback(); // This line throw "Transaction not connected, or was disconnected"
                }
                Console.ReadKey();
            }
        }

Following is the image. ( If you look non-public member during debug).

trnsaction image 2.png

It means that if you make a call to database using Nhibernate and there is some internal exception at DB end ( in above case there is table not exists , other case I came across when sub query expect to return one record but return two record in some case).

I hope above information will help to save some time.