如何使我的 pojo 具有事务性?
我有一个多线程、多服务器 Web 应用程序,使用 hibernate 和 spring 使用 AOP 管理事务。问题是,我需要维护内存中的数据并使其与数据库的状态保持同步......本质上我正在实现内存中的缓存。
现在,有什么方法可以使我的内存中 pojo 事务性达到与 hibernate/spring 事务性相同的级别吗?我的服务调用分层了 30 个类,而且我永远不知道提交将发生在哪里。我有什么选择?
I have a multi-threaded, multi-server web application with hibernate and spring managing transactions with AOP. The problem is, I need to maintain in-memory data and keep it current with the state of the database...essentially I'm implementing an in-memory cache.
Now, is there any way to make my in-memory pojos transactional on the same level as the hibernate/spring transactionality? I have service calls layered 30 classes deep, and I never know where the commit will occur. What are my options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我很理解你的需求,那么在某些情况下你会问一些微妙的问题。让我尝试一个示例来检查我是否理解正确:
可见这是一个复杂的问题,不要小看它。
在理论层面上已经有很多问题需要解决(当你编码时你会遇到更多问题......)。
如果您想成功使用它,您的架构将需要非常清晰。
如果没有,你就会冒着精神理智的风险;-)
首先,你需要确保你真的想要这样的东西!
如果你真的想要那样..
我建议使用技术代码(AOP, ThreadLocal)来隐藏功能代码中的这种棘手的复杂性。
很可能你的提交/回滚已经通过AOP完成,所以这应该没有问题。
很可能
隐藏 P 实例的检索(有时是 Stored 缓存中的实例,有时是副本):使用名为 Store 的类来存储缓存中的值,有一个 Store 类型的 ThreadLocal 变量。我会为当前线程使用 ThreadLocal 变量:
If I understand well your need, you are asking something delicate, in some contexts. Let me try an example, to check if I understand correctly:
You can see that this is a complex issue, don't underestimate it.
There are many issues to be solved, on the theoretical level already (and you will have more when you code...).
Your architecture will need to be really clear, if you want to use it successfully.
If not, you risk your mental sanity ;-)
First, You need to make sure you really want something like that!
If you really want that ..
I suggest using technical code (AOP, ThreadLocal) to hide this tricky complexity from your functional code.
It is probable that your commit/rollback is already done via AOP, so this should be no problem.
To hide the retrieving of instances of P (sometimes the instance in the Stored cache, sometimes a copy) : Use a class called Store to store the values in the cache, have a ThreadLocal variable of type Store. I would use a ThreadLocal variable for your current thread:
是否使用 Hibernate 的 二级缓存 不能满足您的要求? JBoss Cache 提供程序既是分布式的又是事务性的。
如果这对你不起作用,你就必须解释为什么以及你真正想要实现的目标是什么,以获得有意义的回应。
Does using Hibernate's 2nd level cache not satisfy your requirements? JBoss Cache provider is both distributed and transactional.
If that doesn't work for you, you'll have to explain why and what is it you're really trying to achieve in order to get meaningful responses.