-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: session.Flush() resets connection, SQLite in memory
PostPosted: Sun Oct 12, 2008 5:22 am 
Newbie

Joined: Sat Oct 11, 2008 1:37 pm
Posts: 2
I have tried to set up SQLite as an inmemory database so that I can perform unit tests on my data access layer

(http://ayende.com/Blog/archive/2006/10/ ... ecord.aspx)

The problem occures when I call session.Flush(). It seems my database connection is reset. This seems to reset the schema that I have created and I get an error message telling me "SQLite error no such table: tblPerson"

Do you have any solutions for this problem ?

How do I insert multiple items to the inmemory database, and then query the database ?

Code:
using (ISession session = factory.OpenSession())
                {
                    IDbConnection connection = session.Connection;
                    new SchemaExport(cfg).Execute(false, true, false, true, connection, null);

                    List<Person> list = new List<Person>{
                        new Person{Name="John Rambo", Id=0},
                        new Person{Name="Sylvester Stallone", Id=1}
                    };

                    foreach (Person p in list)
                        session.Save(p);
                 
                    session.Flush();
                   
                    if (session.Connection != connection)
                    {
                        Console.WriteLine("Connection changed");
                    }
                    else
                    {
                        //I never come this far
                        foreach (Person p in list)
                            session.Evict(p);

                        ICriteria criteria = session.CreateCriteria(typeof(Person));
                        criteria.Add(Expression.Eq("Name", "John Rambo"));

                        var result = criteria.List();

                        foreach (var o in result)
                            Console.WriteLine((o as Person).Name);
                    }

                    Console.Read();
                }



Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="SqliteNHibernate"
                   namespace="SqliteNHibernate">

  <class name="Person" table="tblPerson">
    <id name="Id" column="Id">
      <generator class="assigned"/>
    </id>

    <property name="Name" column="Name"/>
   
  </class>
 
</hibernate-mapping>



Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
      <property name="connection.connection_string">Data Source=:memory:;Version=3;New=True;</property>
     
    </session-factory>
  </hibernate-configuration>


Top
 Profile  
 
 Post subject: And the solution is:
PostPosted: Wed Oct 15, 2008 4:47 am 
Newbie

Joined: Sat Oct 11, 2008 1:37 pm
Posts: 2
You need to set
"connection.release_mode" to "on_close"


Top
 Profile  
 
 Post subject: The release mode isn't fixing my problem ...
PostPosted: Tue Dec 16, 2008 6:01 pm 
Newbie

Joined: Tue Dec 16, 2008 5:49 pm
Posts: 2
Hi,

I have encountered the same problem with in-memory SQLite databases. Did you ever resolve this problem?? I do have my release_mode set to on_close. Here is my code:

Code:
            var config = new Configuration();

            //if (File.Exists("c:\\temp\\sqlitedb.db"))
            //    File.Delete("c:\\temp\\sqlitedb.db");

            //new SQLiteConfiguration().UsingFile("c:\\temp\\sqlitedb.db").DoNot.ShowSql().ConfigureProperties(config);
            new SQLiteConfiguration().InMemory().ConfigureProperties(config);
            config.Properties["connection.release_mode"] = "on_close";
            AutoPersistenceModel.MapEntitiesFromAssemblyOf<Build>().Where(t => t.Namespace == "DomainModel.Entities").WithConvention(c => c.GetForeignKeyName = p => p.Name + "Id").Configure(config);
            new SchemaExport(config).Execute(true, true, false, true);

            var session = config.BuildSessionFactory().OpenSession();

            var build = new Build { BuildNumber = 7066, Major = 1, Minor = 0, Product = "My Product" };
            session.Save(build);


When using the file configuration (commented out) it works like a charm. When trying the in memory implementation, it fails with a "no such table: Build" error.

Any ideas??

Thanks,

Jereme


Top
 Profile  
 
 Post subject: Figured it out ...
PostPosted: Tue Dec 16, 2008 6:23 pm 
Newbie

Joined: Tue Dec 16, 2008 5:49 pm
Posts: 2
I was doing things in the wrong order; if I open the session before doing the SchemaExport and use the session.Connection for the export, it works.

Hope this helps somebody!

Jereme


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.