-->
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.  [ 2 posts ] 
Author Message
 Post subject: Session problems with session-per-conversation pattern
PostPosted: Fri Aug 10, 2007 7:14 am 
Newbie

Joined: Fri Aug 10, 2007 5:42 am
Posts: 3
Hello everybody,

i have a problem or perhaps just a missunderstanding of session "cache" and the session-per-conversation pattern.

First the facts:

Hibernate version:
1.2.0 GA

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Benutzerverwaltung"
namespace="Benutzerverwaltung.Domain" >

<class name="Mandant">
<id name="Id">
<generator class="hilo"/>
</id>
<version name="Version"/>

<property name="Kennung" not-null="true" unique="true"/>
<property name="Beschreibung"/>
<property name="Praefix" not-null="true" unique="true"/>


<bag name="funktionsrechte" inverse="true" cascade="all-delete-orphan" access="field">
<key column="Mandant_Id"/>
<one-to-many class="Funktionsrecht"/>
</bag>

<bag name="benutzergruppen" inverse="true" cascade="all-delete-orphan" access="field">
<key column="Mandant_Id"/>
<one-to-many class="Benutzergruppe"/>
</bag>
</class>

</hibernate-mapping>


Test Code:
[Test]
public void BusinessObjectJustSavedShouldBeFound() {
ISession session = pHelper.OpenSession();
session.FlushMode = FlushMode.Never;

Mandant m = new Mandant("key", "text", "text");
session.Save(m);

IQuery query = session.CreateQuery("FROM Mandant m WHERE m.Kennung= 'key'");
IList<Mandant> list = query.List<Mandant>();
Assert.AreEqual(1, list.Count);
Mandant mLoaded = list[0];

Assert.AreEqual(m, mLoaded);
}


Name and version of the database you are using:
Microsoft SQL Server Express Edition (Version 9.00.1399.06)


As you can see in the NUnit-Test I expect that the just saved business object of the type Mandant would be found by the query. But the test fails.
(Well, I know that when i call session.Flush() after the session.Save it would be possible, but I don´t want to call it -> see the reason described in the story.)

The story:
I have a GUI where the user can change the values of business objects, delete them or create new ones. The user can also use a cancel button or a save button to "rollback" or "commit" changes.
In my first idea I realised this with transactions, but I found out that it is not a good idea to keep transactions open for a long time (user think time) because they might lock complete db-tables.

So I decided to use the Session-per-Conversation-Pattern.
Now it is possible to write changes to the session and when the user clicks "save" session.Flush() is called. When the user wants to cancel I call session.Clear(). (So you can see now why I can´t call Flush() before the user clicks "save" because otherwise a rollback wouldn´t be possible any more.)
So far it´s ok.

My problem now is, that any changes I write to the "unflushed" session are not reachable by queries. As in the test case above, I would expect that NHibernate knows that I deleted the object before in this session context and would not return it by a query called in the same session context.

I now have to manage differences between changes written to the session and the actual state of the database on my own. Well, I always thought that such a management is one of the key features of Hibernate.

Is there any possibility to reach the features I described with on-board features of Hibernate an not to implement them on my own?

I realy like NHibernate :-) - please help me that I don´t have to give up using it.

Thank you very much for your help!!!

Greetings
hiaso


Top
 Profile  
 
 Post subject: Re: Session problems with session-per-conversation pattern
PostPosted: Fri Aug 10, 2007 11:54 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
hiaso wrote:
My problem now is, that any changes I write to the "unflushed" session are not reachable by queries.
...
I now have to manage differences between changes written to the session and the actual state of the database on my own. Well, I always thought that such a management is one of the key features of Hibernate.


It is, but you can't query into those changes without first flushing to the database. NH only translates HQL and Criteria into DB-specific SQL, it does not also query its own internal state.

As for having to "give up" on NH, it seems that you could build your own conversation abstraction on top of using NH as your mapping and unit-of-work provider.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.