Hi,

It's difficult to say for sure with just a small method like this taken out
of context. The problem is likely in the interaction between different
parts of your application.

I will say however that I'm very suspicious of your Save() method. I mean
no offence, but to me it looks like a collection of antipatterns.

For example - the automatic retrieval of a new session if there isn't
already one suggests to me that the application has no clear rule for when
a session should exist or not and we cannot tell from the code when the
session is closed.

A session must be immediately discarded if there is an exception while it
is open otherwise it may contain corrupt state that can lead to later
problems (including corrupting data in the database).

Also, a transaction is meant to contain a logical unit of work.
Open/closing sessions and transactions should be done top-level when
starting a logical operation - not inside a low-level "save-method".



Den mån 21 okt. 2019 kl 15:08 skrev rakesh singh <rakesh.best...@gmail.com>:

> below method not able to handle concurrency and throwing exception
> (NHibernate.StaleObjectStateException: Row was updated or deleted by
> another transaction)
>
> In hibernate xml i am using <version name="DBVersion" column="DBVersion"
> type="Int64" generated="never" unsaved-value="0"/>
>
>         public void Save(Object o)
>         {
>             try
>             {
>                 // Check to make sure a session has been started, if not open 
> a new session
>                 if (_session == null)
>                     _session = GetSession();
>
>                 // Connect to the DB if necessary
>                 ReconnectDBConnection();
>                 // Start a transaction
>                 BeginTransaction();
>                 // save
>                 _session.SaveOrUpdate(o);
>                 // commit
>                 CommitTransaction();
>             }
>             catch (NHibernate.StaleStateException e)
>             {
>                 string msg = "DB Stale data error.  Reason: " + e.ToString();
>             //show error message
>                 throw new ExceptionDatabaseStaleData(msg, e);
>             }
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nhusers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nhusers/36bc7658-1965-4429-95a4-b8ed9b9451ab%40googlegroups.com
> <https://groups.google.com/d/msgid/nhusers/36bc7658-1965-4429-95a4-b8ed9b9451ab%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nhusers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nhusers/CAHOuc7ME%3DmtOQK2ZDW2Q3mESOJh%3D6FLJqUo_Cqn1ChAgbJLFUw%40mail.gmail.com.

Reply via email to