-->
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.  [ 3 posts ] 
Author Message
 Post subject: TransactionScope and nhibernate transactions
PostPosted: Wed Nov 08, 2006 2:33 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 10:48 am
Posts: 33
Hi, finally I’ve found the reason of my problem with transactions.
I’m using System.Transactions.TransactionScope to create a transaction and then within this transaction I save different objects using NHibernate .
if I don’t use the nhibernate session before the System.Transactions.TransactionScope is created then the transactions created using session.BeginTransaction() are promoted to DTC transaction and all my code works in a transactional way.
But If I use the nhibernate session before the system.Transactions.TransactionScope is created then the transactions created using session.BeginTransaction() aren´t promoted to DTC transaction.

Anybody has any idea why it is happening?

Thanks a lot, Martin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 3:23 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It's happening because when NHibernate starts a database transaction, ADO.NET checks to see whether a distributed transaction is going on, and enlists the database transaction in it. But it doesn't happen the other way around - when a distributed transaction is started, it doesn't know about the already running database transactions.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 2:13 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 10:48 am
Posts: 33
Sergey, thanks for your replay.
Maybe I didn’t explain well yesterday.
I’m going to give you an example because it is easier to understand

1) In the following example the database transaction is promoted to DTC

Try
Using trScope As TransactionScope = New TransactionScope(TransactionScopeOption.Required)
For i As Integer = 4 To 6
Dim miPais As New CO.Pais()
miPais.Codigo = "O" + i.ToString
miPais.Descripcion = "Test trans " + i.ToString
miPais.Id = 0

DA.PaisDA.grabar(session, miPais)
Next
trScope.Complete()
End Using
Catch eX As Exception
End Try

In the this example PaisDA.Grabar executes the following code

Public Shared Sub grabar(ByVal session As ISession, ByVal miPais As CO.Pais)
Dim Transaction As ITransaction
Transaction = Nothing
If Not session Is Nothing Then
Try
Transaction = session.BeginTransaction()
session.SaveOrUpdate(miPais)
Transaction.Commit()
Exit Try
Catch
If Not Transaction Is Nothing Then Transaction.Rollback()
session.Clear()
Throw
Exit Try
End Try
End If
End Sub

2) Now, I’m going to write an example where the database transaction isn’t promoted to DTC

Try

DA.PaisDA.consultar(session)

Using trScope As TransactionScope = New TransactionScope(TransactionScopeOption.Required)
For i As Integer = 4 To 6
Dim miPais As New CO.Pais()
miPais.Codigo = "O" + i.ToString
miPais.Descripcion = "Test trans " + i.ToString
miPais.Id = 0

DA.PaisDA.grabar(session, miPais)
Next
trScope.Complete()
End Using
Catch eX As Exception
End Try

In this case I’m using the nhibernate session to get the Provincias as follows

Public Shared Sub consultar(ByVal session As ISession)

Dim query As String
query = "from CO.Provincias as Provincias "
session.Find(query)

End Sub


Well, in the last example you can see that I’m not starting a database transaction before starting a DTC transaction I’m just using the nhibernate session to get Provincias without creating a new transaction, but after that, when I start a new transaction within a DTC transaction it isn’t promoted to DTC transaction.

Do you know why it happens?

Thanks, Martin


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