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
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.
>
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
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.