NHibernate会话管理? [英] NHibernate session management?

查看:355
本文介绍了NHibernate会话管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我简单地描述一下这个场景。我写一个简单的游戏,几乎所有的工作都是在服务器端用一个瘦客户端为玩家访问它。玩家登录或创建帐户,然后可以通过围绕网格移动来与游戏交互。当他们进入一个单元格时,他们应该被告知该单元格中的其他玩家,类似地,该单元格中的其他玩家将被告知该玩家进入它。有很多其他的互动和行动可以发生,但它不值得去细节上,因为它只是更多的相同。当播放器注销然后退回,或者如果服务器关闭并恢复,所有的游戏状态应该保持,虽然如果服务器崩溃,那么如果我失去10分钟左右的更改没有关系。 p>

我决定使用NHibernate和一个SQLite数据库,所以我一直在阅读很多关于NHibernate,下面的教程和编写一些示例应用程序,并彻底困惑为我应该怎么走这个!



我有的问题是:什么是最好的方式来管理我的会议?从我理解的少量,所有这些可能性跳出我:




  • 有一个会话,总是打开所有客户端

  • 为每个连接并定期刷新的客户端分配一个会话

  • 每次我必须使用任何持久实体时打开会话并在更新,插入,删除或查询完成后立即关闭

  • 为每个客户端建立一个会话,但保持断开连接,只有在需要使用时才重新连接

  • 与上述相同,但保持连接,并在一段时间不活动后才断开连接

  • 保持实体分离并仅每10分钟连接一次,比如提交更改



我应该使用什么样的策略来获得不错的性能,因为可能会有很多更新,



另一个较小的问题:我应该如何使用事务处理有效的方式?是否适合每一个变化都在自己的事务中,或者是当我有数百个客户端都试图改变网格中的单元格时执行不好?我应该尝试找出如何批量聚集类似的更新,并将它们放在一个单一的事务,或者是那将是太复杂?

解决方案

我将使用每个请求的会话到服务器,一个事务每个会话。



回答您的解决方案:




  • 所有客户端都使用一个会话始终打开的会话:此处会出现性能问题,因为会话不是线程安全的,您必须锁定会话的所有调用。

  • 为每个连接并定期刷新的客户端建立一个会话:此处会出现性能问题,因为客户端使用的所有数据都将被缓存。

  • 每次我必须使用任何持久实体时打开会话,并在更新,插入,删除或删除后立即关闭它。查询完成:这里不会有任何性能问题。缺点是可能的并发或损坏的数据问题,因为相关的sql语句不在同一个事务中执行。

  • 为每个客户端建立一个会话,但保持断开连接,只在需要时重新连接使用它:NHibernate已经具有内置的连接管理,并且已经非常优化。

  • 与上述相同,但保持连接,并且在一段时间不活动后仅断开连接:会导致问题,因为sql连接数量有限,并且还会限制用户数量

  • 保持实体分离,并且只有每10分钟附加一次,例如提交更改:由于分离的实体中的陈旧数据,将导致问题。你必须自己跟踪更改,这使得你最终得到一段看起来像会话本身的代码。



没有用,现在进入更多的细节,因为我只是重复手册/教程/书。当您每个请求使用会话时,您可能不会在您描述的应用程序的99%中出现问题(也可能不会有问题)。 Session是一个轻量级不是线程安全的类,就是要生活得很短。当你想知道会话/连接/缓存/事务管理的工作原理时,我建议先阅读一个手册,而不是问一些关于不清楚的主题的更详细的问题。


Firstly, let me give a brief description of the scenario. I'm writing a simple game where pretty much all of the work is done on the server side with a thin client for players to access it. A player logs in or creates an account and can then interact with the game by moving around a grid. When they enter a cell, they should be informed of other players in that cell and similarly, other players in that cell will be informed of that player entering it. There are lots of other interactions and actions that can take place but it's not worth going in to detail on them as it's just more of the same. When a player logs out then back in or if the server goes down and comes back up, all of the game state should persist, although if the server crashes, it doesn't matter if I lose 10 minutes or so of changes.

I've decided to use NHibernate and a SQLite database, so I've been reading up a lot on NHibernate, following tutorials and writing some sample applications, and am thoroughly confused as to how I should go about this!

The question I have is: what's the best way to manage my sessions? Just from the small amount that I do understand, all these possibilities jump out at me:

  • Have a single session that's always opened that all clients use
  • Have a single session for each client that connects and periodically flush it
  • 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
  • Have a session for each client, but keep it disconnected and only reconnect it when I need to use it
  • Same as above, but keep it connected and only disconnect it after a certain period of inactivity
  • Keep the entities detached and only attach them every 10 minutes, say, to commit the changes

What kind of strategy should I use to get decent performance given that there could be many updates, inserts, deletes and queries per second from possibly hundreds of clients all at once, and they all have to be consistent with each other?

Another smaller question: how should I use transactions in an efficient manner? Is it fine for every single change to be in its own transaction, or is that going to perform badly when I have hundreds of clients all trying to alter cells in the grid? Should I try to figure out how to bulk together similar updates and place them within a single transaction, or is that going to be too complicated? Do I even need transactions for most of it?

解决方案

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: NHibernate 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.

这篇关于NHibernate会话管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆