Hi Fabio,
I am still a bit confused. Now I am using the following test code:
[TestMethod]
public void CanRollbackTransaction() {
long medewerkerId = 2;
string voornaam;
using (var session = NHibernateSession.Current) // session will be
opened
using (NHibernate.ITransaction trans = session.BeginTransaction()) {
using (var ts = new TransactionScope()) {
Medewerker m = session.Get<Medewerker>(medewerkerId);
voornaam = m.Voornaam;
m.Voornaam = DateTime.Now.ToString();
session.Update(m);
//NB: scope is not completed
}
trans.Commit(); // This throws NHibernate.TransactionException:
Transaction not connected, or was disconnected.
}
using (var session = NHibernateSession.Current) {
Medewerker mCheck = session.Get<Medewerker>(medewerkerId);
Assert.AreEqual(voornaam, mCheck.Voornaam);
}
}
The above code does not succeed when commiting the NHibernate
Transaction I receive the following (obvious) error:
NHibernate.TransactionException: Transaction not connected, or was
disconnected.
Next test looked like this:
[TestMethod]
public void CanRollbackTransaction() {
long medewerkerId = 2;
string voornaam;
string voornaamCheck;
using (var session = NHibernateSession.Current) {
using (var ts = new TransactionScope()) {
using (NHibernate.ITransaction trans =
session.BeginTransaction())
{
Medewerker m =
session.Get<Medewerker>(medewerkerId);
voornaam = m.Voornaam;
m.Voornaam = DateTime.Now.ToString();
session.Update(m);
trans.Commit();
}
//NB: scope is not completed
}
using (NHibernate.ITransaction trans =
session.BeginTransaction()) {
Medewerker mCheck =
session.Get<Medewerker>(medewerkerId);
voornaamCheck = mCheck.Voornaam;
trans.Commit();
}
Assert.AreEqual(voornaam, voornaamCheck);
}
}
So NHTransaction nested in TS and check on equalness in same session
(not same transaction). Again no succes. The Session.Get gives me the
"wrong" value.
My main issue is that the NHibernate action are only part of the
solution, so when writing a "Transaction" ActionFilterAttribute for
the controllers of my MVC classes I want to make sure that all actions
in the controller succeed or do not succeed. So I must use TS as the
main transaction handler and I want NHibernate transactions to be part
of the "main transaction" that is the TS transaction.
Any suggestions?
Henk
On 15 jan, 00:35, Fabio Maulo <[email protected]> wrote:
> you can commit the NH's transaction and leave the real action to TS
> (Abort/Complete).
> Why ?...
> Well... you should manage the NH's always and occasionally the NH's will
> work in an Ambient-Transaction.
> The code where the NH's transaction is managed shouldn't not be aware about
> if it is working in Ambient-Transaction or not.
> For those using AOP this is pretty normal.
>
> btw, have you tried using the NH's transaction in your tests ?
>
> 2010/1/14 HH <[email protected]>
>
>
>
>
>
> > So you are suggesting that I should use the following:
>
> > OnActionExecuting:
> > 1) Open Session (if not open)
> > 2) CreateTransactionScope
> > 3) Start NHibernate Transaction
>
> > OnActionExecuted:
> > if ([errorsOccured])
> > Rollback NHibernate transaction
> > else
> > Commit NHiberate transaction
> > TransactionScope.Complete()
>
> > Session will be closed on end request.
>
> > This makes me have to manage both the TransactionScope and NHibernate
> > transaction.
>
> > Why?
>
> > Henk
>
> > On 14 jan, 22:49, Fabio Maulo <[email protected]> wrote:
> > > 2010/1/14 HH <[email protected]>
>
> > > > Do you have any ideas on how to make this work?
>
> > > with an appropriate ActionFilter to manage NH's session, TS and NH's
> > > transaction
>
> > > Fabio Maulo
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> Fabio Maulo- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.