First of all, allow me to provide a description from the scenario. I am writing an easy game where virtually all the jobs are done around the server affiliate with a skinny client for gamers to gain access to it. A person logs in or produces a free account and may then communicate with the overall game by getting around a power grid. Once they enter a cell, they must be informed of other gamers for the reason that cell together with, other gamers for the reason that cell is going to be informed of this player entering it. You will find all interactions and actions that may occur but it is not worth moving in to detail in it as it is just a lot of same. Whenever a player logs out then in or maybe the server goes lower and returns up, all the overall game condition should persist, although when the server crashes, it does not matter basically lose ten minutes approximately of changes.

I have made the decision to make use of NHibernate along with a SQLite database, so I have been reading through up a great deal on NHibernate, following lessons and writing some sample programs, and am completely confused regarding the way i should build a storage shed!

The question I've is: what's the easiest method to manage my periods? Just in the bit which i are evident, each one of these options jump out at me:

  • Possess a single session that's always opened up that clients use
  • Possess a single session for every client that connects and periodically flush it
  • Open a session each time I must use the endured organizations and close it the moment the update, place, remove or totally complete
  • Possess a session for every client, but ensure that it stays disconnected and just reunite it when I have to utilize it
  • Just like above, but ensure that it stays connected and just disconnect it following a certain duration of lack of exercise
  • Keep your organizations detached and just attach them every ten minutes, say, to commit the alterations

What type of strategy must i use to obtain decent performance considering that there might be many updates, card inserts, removes and queries per second from possibly 100s of clients all at one time, and every one has to become in line with one another?

Another more compact question: how must i use transactions within an joyful manner? Could it be fine for every change to stay in its very own transaction, or perhaps is that likely to perform badly after i have 100s of clients all attempting to alter cells within the power grid? Must I attempt to learn how to bulk together similar updates and put them inside a single transaction, or perhaps is that likely to be too complicated? Will I even need transactions for many from it?

I'd make use of a session per request towards the server, and something transaction per session. I would not optimize for performance prior to the application is mature.

Response to your solutions:

  • Possess a single session that's always opened up that clients use: You'll have performance issues here since the session isn't thread safe and you'll have to lock each and every call towards the session.
  • Possess a single session for every client that connects and periodically flush it: You'll have performance issues here because all data utilized by the customer is going to be cached. Additionally, you will see issues with stale data in the cache.
  • Open a session each time I must use the endured organizations and close it the moment the update, place, remove or totally complete: You will not have performance problems here. A drawback are possible concurrency or corrupt data problems because related sql claims aren't performed within the same transaction.
  • Possess a session for every client, but ensure that it stays disconnected and just reunite it when I have to utilize it: NHibernate already has build-in connection management which has already been very enhanced.
  • Just like above, but ensure that it stays connected and just disconnect it following a certain duration of lack of exercise: May cause problems because the quantity of sql connections is restricted as well as limit the quantity of customers of the application.
  • Keep your organizations detached and just attach them every ten minutes, say, to commit the alterations: May cause problems due to stale data within the detached organizations. You'll have to track changes yourself, making you finish track of a bit of code that appears such as the session itself.

It might be useless to enter more detail now, because I'd just repeat the manuals/lessons/book. If you use a session per request, you most likely will not have problems in 99% from the application you describe (and perhaps not whatsoever). Session is really a lightweight not threadsafe class, that to reside a really short. When you'd like to learn how the session/connection/caching/transaction management works, I suggest to see a manual first, and than request more detailed questions regarding the unclear subjects.