NHibernate session management?

I would use a session per request to the server, and one transaction per session. I wouldn't optimize for performance before the app is mature.

I would use a session per request to the server, and one transaction per session. I wouldn't optimize for performance before the app is mature. Answer to your solutions: Have a single session that's always opened that all clients use: You will have performance issues here because the session is not thread safe and you will have to lock all calls to the session.

Have a single session for each client that connects and periodically flush it: You will have performance issues here because all data used by the client will be cached. You will also see problems with stale data from the cache. Open a session every time I have to use any of the persisted entities and close it as soon as the update, insert, delete or query is complete: You won't have any performance problems here.

A disadvantage are possible concurrency or corrupt data problems because related sql statements are not executed in the same transaction. Have a session for each client, but keep it disconnected and only reconnect it when I need to use it: Nbernate already has build-in connection management and that is already very optimized. Same as above, but keep it connected and only disconnect it after a certain period of inactivity: Will cause problems because the amount of sql connections is limited and will also limit the amount of users of your application.

Keep the entities detached and only attach them every 10 minutes, say, to commit the changes: Will cause problems because of stale data in the detached entities. You will have to track changes yourself, which makes you end up with a piece of code that looks like the session itself. It would be useless to go into more detail now, because I would just repeat the manuals/tutorials/book.

When you use a session per request, you probably won't have problems in 99% of the application you describe (and maybe not at all). Session is a lightweight not threadsafe class, that to live a very short. When you want to know exactly how the session/connection/caching/transaction management works, I recommend to read a manual first, and than ask some more detailed questions about the unclear subjects.

I'm not trying to optimize it; I'm just trying to figure out what the different methods are of doing it, what they're generally good or not good for, how well they scale and perform etc. Before commiting to one. It's about making sure I don't do anything stupid and choose a reasonably sensible solution. Not performance tuning.

So, as for your answer, what do you mean by "request" - what "request"? You mean each time a player does something? – IRBMe Jul 19 '09 at 20:40 Yes with request I mean "each time a player does something" or "each call from the client to the server" – Paco Jul 20 '09 at 21:08 Having read the edits you added in later, I have one question about something you said.

Regarding having a single session per client, I thought that Nbernate guarantees that methods like ISession. Find won't ever return stale data. Secondly, you seemed to be discouraging using Reconnect/Disconnect because "Nbernate already has build-in connection management" yet the manual uses that in a concurrency example called "Long session with automatic versioning".

– IRBMe Jul 21 '09 at 16:47 1. You can prevent to return stale data on several ways, but you can't take advantage of the caching then. You shouldn't use ISession.

Find in normal scenario's. 2. The connection management in the manual there is not about database connections, but sessionconnections to manage detached objects with a living session by not detaching them, but recreating instead.

3. I don't understand why you consider long living sessions in the game scenario in the first place. – Paco Jul 21 '09 at 19:33.

Read the 'ISessionFactory' on this page of N ISessions are meant to be single-threaded (i.e. , not thread-safe) which probably means that you shouldn't be sharing it across users. ISessionFactory should be created once by your application and ISessions should be created for each unit of work.

Remember that creating an ISessions does not necessarily result in opening a database connection. That depends on how your SessionFactory's connection pooling strategy is configured. You may also want to look at bernate's Documentation on Session and Transaction.

I would aim to keep everything in memory, and either journal changes or take periodic offline snapshots.

I already thought about this solution and it's going to be far more complicated, essentially duplicating a lot of the functionality for reliability that a database gives me. That's why I decided to go with a database in the first place, rather than just writing all the data to flat files or something. – IRBMe Jul 19 '09 at 20:41.

Have a read through Nbernate Best Practices with ASP. NET, there are some very good tips in here for a start. As mentioned already be very careful with an ISession as it is NOT threadsafe, so just keep that in mind.

If you require something a little more complex then take a look into the NBurrow contrib project. It states something like "the real power Burrow provides is that a Burrow conversation can span over multiple http requests".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions