NHibernate 1.0.3.0
MySQL 5
I am attempting to write a generic insert function for a datalayer object. Everything work great but i cannot get the transaction to rollback on an exception.
Code:
Public Function SaveObjectsWithTransaction(ByVal o1 As Object, ByVal o2 As Object) As DataLayerResult
Dim session As ISession = GetSession()
Dim dbTrnx As ITransaction = session.BeginTransaction()
Try
session.Save(o1)
session.Save(o2)
dbTrnx.Commit()
Return DataLayerResult.Success
Catch e1 As StaleObjectStateException
dbTrnx.Rollback()
Return DataLayerResult.StaleObjectFailure
Catch e2 As HibernateException
dbTrnx.Rollback()
Return DataLayerResult.ServiceFailure
Finally
session.Close()
End Try
End Function
To test this i force a HibernateException via an invalid foreign key constraint on object 2 (o2). So while debugging object 1(o1) gets inserted into the database even though the transaction has not been committed, then an exception is thrown form session.Save(o2), which is caught, then dbTrnx.rollback - which has no effect and close the session.
After reading the documentation is seems that the problem may lie in my mapping file id being native. The id for o1 is listed below:
<id name="Id" column="ACCOUNT_ID" type="int">
<generator class="native" />
</id>
If this is the case can anyone offer a reasonable alternative to using the native id or any other solution.
Thanks in advance