Custom Id Generator and "error performing isolated work"

3,156 views
Skip to first unread message

Chris Nicola

unread,
Aug 18, 2009, 7:45:29 PM8/18/09
to nhu...@googlegroups.com
Ok this is a bit strange.  I am still having some issues with this custom ID generator.  So far everything has been working well up until now, but today I decided to fix the test fixture base class I was using to start nHibernate.  It was using SessionSource from Fluent instead of SessionFactory() and I didn't like that.

Here is the test Setup():
    [SetUp]
    public void SetupContext() {
      var cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory()
                    .ProxyFactoryFactory(
                    "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
                    .ShowSql())
        .Mappings(m => m.FluentMappings.AddFromAssembly(typeof (InvestSpecMap).Assembly)).BuildConfiguration();
      new SchemaExport(cfg).Execute(true, true, false);
      SessionFactory = cfg.BuildSessionFactory();
    }

and here is a simple test:
    [Test]
    public void TestIdGeneratorOnMultipleInsert() {
      using (var ses = SessionFactory.OpenSession()) {
        using (var tx = ses.BeginTransaction())
        foreach (var spec in Securities) {
          ses.Save(spec);
        }
      }
      Session.Flush();
      Assert.AreEqual(1048577 + Securities.Length, Securities[Securities.Length -1].Id);
    }

Here is the output of the test:
    drop table if exists InvestSpec

    drop table if exists InvestPrice

    drop table if exists zSysCounters

    create table InvestSpec (
        InvestSpecId INTEGER not null,
       UpdatedById INTEGER,
       UpdateDate DATETIME,
       AnnlzdDiv NUMERIC,
       AssetClass TEXT,
       CurrencyType INTEGER,
       CusipNum TEXT,
       DivFrequency TEXT,
       FullName TEXT,
       MaturityDt DATETIME,
       PriceAsOfDt DATETIME,
       AbbrName TEXT,
       TaxTreatment TEXT,
       TickerSymbol TEXT,
       Type TEXT,
       UnitPrice NUMERIC,
       primary key (InvestSpecId)
    )

    create table InvestPrice (
        InvestPriceId INTEGER not null,
       UpdatedById INTEGER,
       UpdateDate DATETIME,
       MarketPrice NUMERIC,
       EffectiveDt DATETIME,
       InvestSpecId INTEGER,
       primary key (InvestPriceId)
    )

    create table zSysCounters (
         InvestSpec INTEGER,
        InvestPrice INTEGER 
    )

    insert into zSysCounters values ( 1, 1 )
NHibernate: select InvestSpec from zSysCounters

And finally the trace:

System.NullReferenceException: Object reference not set to an instance of an object.
at NHibernate.Exceptions.ADOExceptionHelper.Convert(ISQLExceptionConverter converterException sqlExceptionString messageSqlString sql)
at NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connectionIDbTransaction transaction)
at NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor sessionIIsolatedWork workBoolean transacted)
NHibernate.HibernateException: error performing isolated work
at NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor sessionIIsolatedWork workBoolean transacted)
at NHibernate.Transaction.AdoNetWithDistrubtedTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor sessionIIsolatedWork workBoolean transacted)
at NHibernate.Engine.Transaction.Isolater.DoIsolatedWork(IIsolatedWork workISessionImplementor session)
at NHibernate.Engine.TransactionHelper.DoWorkInNewTransaction(ISessionImplementor session)
at FDPDatabase.DataAccess.FDPGenerator.FdpSequenceGen.Generate(ISessionImplementor sessionObject obj) in FDPSequenceGen.cs: line 137
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entityString entityNameObject anythingIEventSource sourceBoolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at ISMDataAdapter.Tests.FDPServiceTests.NHibernateTests.CrudTests.TestPriceCrud() in CrudTests.cs: line 36


If I run it using the debugger I get this exception thrown:

SQLite error
no such table: zSysCounters

Any help would be greatly appreciated.  I am very confused as to why I am getting this behavior.

Chris Nicola

unread,
Aug 18, 2009, 7:48:28 PM8/18/09
to nhu...@googlegroups.com
I should have noted I have tried using normal HiLo instead and I get the same error.  Clearly I am not setting up my tests or configuration correctly.

Thanks,
Chris

Chris Nicola

unread,
Aug 18, 2009, 8:07:09 PM8/18/09
to nhu...@googlegroups.com
Bah I hate it when I solve the problem 5 minutes after I ask. 

So it appears that SQLite in memory databases can only support one session and after that they are done.  Interesting.  Sorry to have been a bother ;-).

spiralni

unread,
Aug 20, 2009, 12:57:47 PM8/20/09
to nhusers
I am having the same problem, using hilo wiht MsSql2005 express..

How can I solve it?

On Aug 18, 6:07 pm, Chris Nicola <chnic...@gmail.com> wrote:
> Bah I hate it when I solve the problem 5 minutes after I ask.
>
> So it appears that SQLite in memory databases can only support one session
> and after that they are done.  Interesting.  Sorry to have been a bother
> ;-).
>

Chris Nicola

unread,
Aug 20, 2009, 4:46:30 PM8/20/09
to nhu...@googlegroups.com
You shouldn't have this problem with Sql express.  I was able to make it work. 

There were two issues, the first is that Sqlite appears to lose the database if you dispose of the session.  The second is that even if you save the database to a file Sqlite only allows one transaction at a time. Sinc, Hilo and TableGenerator want create a second transaction to get the Id and this isn't possible. 

As a result nHibernate notices that it can't do this and just handles the Id queries on the same transaction.  This is fine, but if you do a RollBack() you will actually roll back the Id too (which is somewhat undesirable).

I am pretty sure Sql express supports multiple transactions fine though.  I ran my tests with Sql express afterward and that is how I figured this out, so SqlExpress worked for me.  Are you sure you are not using SqlCE?

Chris
Reply all
Reply to author
Forward
0 new messages