totally confused about transactions and session flush

8 views
Skip to first unread message

ajaishankar

unread,
Feb 16, 2010, 4:43:49 PM2/16/10
to nhusers
Hi

I have always heard a NHibernate transaction != DB transaction.

So what exactly is it?

This is the behavior I am seeing:

Say I have a FlushMode of Never, when I begin a transaction, make some
changes and then commit - the updates do NOT get saved to the DB.

But when I have a FlushMode of Auto, it is saved to DB when I commit
the transaction.

Also the following link says:

NH 2.1 https://www.hibernate.org/407.html#A15

"ISession.Transaction is always active - Unlike previous NHibernate
versions, as soon as this transaction is committed, a new transaction
is associated with the session"

What does this mean?

Somebody please prevent me from going insane! :-)

Ajai


Lothan

unread,
Feb 16, 2010, 6:09:03 PM2/16/10
to nhu...@googlegroups.com
Someone please correct me if I'm wrong, but as far I am aware you need to
explicitly call session.Flush() to flush the session if you're using
FlushMode.Never.

The way I look at it (and this may be technically incorrect) is that calling
session.Save(...) basically marks the entity (or entity graph) for
persistence. When you call transaction.Commit(), NHibernate decides whether
or not to automatically flush persistence changes to the database based on
flush mode. If you set FlushMode.Never, you're telling NHibernate that you
are taking explicit responsibility to tell NHibernate when to flush the
cache.

--------------------------------------------------
From: "ajaishankar" <ajai.s...@gmail.com>
Sent: Tuesday, February 16, 2010 4:43 PM
To: "nhusers" <nhu...@googlegroups.com>
Subject: [nhusers] totally confused about transactions and session flush

> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>

ajaishankar

unread,
Feb 17, 2010, 8:04:06 AM2/17/10
to nhusers
Thanks Lothan!

NH examples code I think follow this pattern:

using( var session = sessionFactory.CreateSession() )
using( var tx = session.BeginTransaction() ) {
var entity = new { Name = "John };
session.Save(entity)
session.Flush()
}

Behavior wise is the following any different (apart from it being
unreadable)

using( var session = sessionFactory.CreateSession() )
var entity = new { Name = "John }
session.Save(entity)
entity.Name = "Jane"

using( var tx = session.BeginTransaction() ) {
session.Flush()
tx.Commit()
}
}

Is a NH transaction same as a DB transaction?

Ajai

On Feb 16, 5:09 pm, "Lothan" <lot...@newsguy.com> wrote:
> Someone please correct me if I'm wrong, but as far I am aware you need to
> explicitly call session.Flush() to flush the session if you're using
> FlushMode.Never.
>
> The way I look at it (and this may be technically incorrect) is that calling
> session.Save(...) basically marks the entity (or entity graph) for
> persistence. When you call transaction.Commit(), NHibernate decides whether
> or not to automatically flush persistence changes to the database based on
> flush mode. If you set FlushMode.Never, you're telling NHibernate that you
> are taking explicit responsibility to tell NHibernate when to flush the
> cache.
>
> --------------------------------------------------

> From: "ajaishankar" <ajai.shan...@gmail.com>


> Sent: Tuesday, February 16, 2010 4:43 PM
> To: "nhusers" <nhu...@googlegroups.com>
> Subject: [nhusers] totally confused about transactions and session flush
>
> > Hi
>
> > I have always heard a NHibernate transaction != DB transaction.
>
> > So what exactly is it?
>
> > This is the behavior I am seeing:
>
> > Say I have a FlushMode of Never, when I begin a transaction, make some
> > changes and then commit - the updates do NOT get saved to the DB.
>
> > But when I have a FlushMode of Auto, it is saved to DB when I commit
> > the transaction.
>
> > Also the following link says:
>

> > NH 2.1https://www.hibernate.org/407.html#A15

Jason Meckley

unread,
Feb 17, 2010, 8:58:01 AM2/17/10
to nhusers
I tend to treat an NH transaction as a DB transaction, but I'm not
100% sure about this. I know there are hooks in the NH transaction
that are connected to the 2nd level cache. i'm not sure what else is
connected to the NH transaction. As of 1.2 a transaction is required
for NH to persist entities. it's also required for getting consistent
reads from the database. I think this ties back to cache.

I have found that managing the transaction is not usually preformed
within the same code block as reading data. for code samples they
appear next to each other, but not in robust applications. using a
transaction (which is basically required by NH) with a flush mode of
Auto (default) is the cleanest implementation. I consider anything
else an edge case.

Robert Rudduck

unread,
Feb 17, 2010, 10:10:07 AM2/17/10
to nhu...@googlegroups.com
This link might help out w/r/t Transactions and the Second Level Cache:
 
Reply all
Reply to author
Forward
0 new messages