possible bug in AR - explicit flush?

0 views
Skip to first unread message

Vladimir Okhotnikov

unread,
Jan 18, 2008, 2:32:33 AM1/18/08
to castle-pro...@googlegroups.com
Hi,

I have a following test case, which fails at the last line:

[Test]
public void TestExplicitFlush() {
Entity e1 = new Entity("test1");
Entity e2 = new Entity("test2");
using (new SessionScope(FlushAction.Never)) {
e1.Create();
e2.Create();
}

using (new SessionScope(FlushAction.Never)) {
using (TransactionScope tx = new
TransactionScope(OnDispose.Rollback)) {
Entity e2a = Entity.Find(e2.Id);
e2a.Name = "changed";
tx.VoteCommit();
}

using (new TransactionScope(OnDispose.Rollback)) {
Entity[] changedEntities =
Entity.FindAllByProperty("Name", "changed");
Assert.AreEqual(1, changedEntities.Length);
Entity e2a = changedEntities[0];
e2a.Delete();

SessionScope.Current.Flush();

Assert.AreEqual(0, Entity.FindAllByProperty("Name",
"changed").Length);
}
}
}


the problem is that SessionScope.Current.Flush() seem to have no effect
in this case. The test passes if I comment out the first transaction
that changes the entity name (and adjust the FindAll parameter of
course). Is this a bug or am I missing something?

Oh, and the Entity class is just a pair of Id and Name.

Regards,
Vladimir Okhotnikov

Hamilton Verissimo

unread,
Jan 21, 2008, 9:07:17 AM1/21/08
to castle-pro...@googlegroups.com
Indeed. Can you change the transactions to use IsolationLevel.ReadUncommitted?


--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

Vladimir Okhotnikov

unread,
Jan 22, 2008, 6:54:57 AM1/22/08
to castle-pro...@googlegroups.com
Using ReadUncommitted did not change anything. After spending some time with debugger, it is likely that that the problem is caused by the fact the following difference:

- if a transaction scope is first in a session scope, the transaction scope has one NHibernate session in its key2Session disctionary
- if the same transaction scope is second in a session scope, its key2Session dictionary is empty, hence Flush does not find any sessions to flush

I'm going to look into this a bit more.

Regards,
Vladimir Okhotnikov

>                     Assert.AreEqual (0, Entity.FindAllByProperty("Name",
> "changed").Length);
>                 }
>             }
>         }
>
>
> the problem is that SessionScope.Current.Flush () seem to have no effect

Vladimir Okhotnikov

unread,
Jan 22, 2008, 8:39:21 AM1/22/08
to castle-pro...@googlegroups.com
So, the problem is caused by the fact that TransactionScope overrides
IsKeyKnown and GetSession to use HNibernate sessions of parent scopes if
any. Unfortunately, it does not override Flush to also flush parent
scope sessions. Making AbstractScope.Flush visrual and overriding it
like this:

public override void Flush()
{
if (mode == TransactionMode.Inherits &&
parentTransactionScope != null)
{
parentTransactionScope.Flush();
}

if (parentSimpleScope != null)
{
parentSimpleScope.Flush();
}

base.Flush();
}

solves the problem (and the other AR tests still pass), but I'm not sure
this is the best approach - maybe it is better to register sessions in
key2Session dictionary on TransactionScope.GetSession call?

Regards,
Vladimir Okhotnikov

Vladimir Okhotnikov wrote on 1/22/2008 1:54 PM:
> Using ReadUncommitted did not change anything. After spending some
> time with debugger, it is likely that that the problem is caused by
> the fact the following difference:
>
> - if a transaction scope is first in a session scope, the transaction
> scope has one NHibernate session in its key2Session disctionary
> - if the same transaction scope is second in a session scope, its
> key2Session dictionary is empty, hence Flush does not find any
> sessions to flush
>
> I'm going to look into this a bit more.
>
> Regards,
> Vladimir Okhotnikov
>

> On 21/01/2008, *Hamilton Verissimo* <ham...@castlestronghold.com

> <mailto:ham...@castlestronghold.com>> wrote:
>
>
> Indeed. Can you change the transactions to use
> IsolationLevel.ReadUncommitted?
>
> On 1/18/08, Vladimir Okhotnikov < vokho...@gmail.com

> ham...@castlestronghold.com <mailto:ham...@castlestronghold.com>
> http://www.castlestronghold.com/
>
> >
>

Hamilton Verissimo

unread,
Jan 22, 2008, 9:48:11 AM1/22/08
to castle-pro...@googlegroups.com
I think it makes sense. Can you provide a patch file? Thanks

http://www.castlestronghold.com/

Vladimir Okhotnikov

unread,
Jan 22, 2008, 11:40:40 AM1/22/08
to castle-pro...@googlegroups.com
Patch for trunk/ActiveRecord attached.

Regards,
Vladimir Okhotnikov

ExplicitFlushInTransactionBug.patch.zip

Vladimir Okhotnikov

unread,
Jan 31, 2008, 7:03:06 PM1/31/08
to castle-pro...@googlegroups.com
Hi

any chance of the patch to be applied?

Regards,
Vladimir Okhotnikov

Hamilton Verissimo

unread,
Feb 2, 2008, 2:08:25 PM2/2/08
to castle-pro...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages