使用内存数据库而不是 ThreadLocal 的优点和缺点是什么
到目前为止,我们一直在使用 ThreadLocal 来携带一些数据,以免使 API 混乱。然而,以下是使用线程本地的一些问题,我不喜欢这些问题
1)多年来,线程本地中携带的数据项有所增加 2)自从我们开始使用线程(用于一些轻量级处理)以来,我们还将这些数据迁移到池中的线程并再次将它们复制回来,
我正在考虑为这些数据使用内存数据库(我们不想添加这个)到 API)。我想知道这个方法好不好。有什么优点和缺点。
好的,这是一个简单的场景,
- 用户登录并提交请求,
- 系统为整个请求建立上下文,其中包括 - 此请求的唯一 ID - 用户名 - 系统登录(用户可以登录多个系统) - 一些域事件供以后使用
- 请求通过多个逻辑层(表示、业务域、 数据
- 在集成层中,我们借用很少的线程来并行地从池中提取 多个合作伙伴。每个拉取都需要一些先前存储在线程本地的数据,因此在从合作伙伴接收到所有数据后,我们将这些数据迁移到池化线程
- 我们将子线程中积累的新线程本地数据迁移回主线程
- ,最后 我们将 DOMAIN 事件持久保存到数据库的交互
we have been using ThreadLocal so far to carry some data so as to not clutter the API. However below are some of issues of using thread local that which I dont like
1) over the years the data items being carried in thread local has increased
2) Since we started using threads (for some light weight processing), we have also migrating these data to the threads in the pool and copying them back again
I am thinking of using an in memory DB for these (we doesnt want to add this to the API). I wondering if this approach is good. What r the pros and cons.
ok here is a simple scenario
- user logs in and submits a request
- system establishes context for this entire request which include
- unique id for this request
- username
- system looged in (user can log into multiple systems)
- some DOMAIN EVENTS for later use - the request passes through multiple logical layers (presentation, business domain,
rules, integration) etc - in the integration layer, we borrow few threads from pool to parallel pull data from
multiple partners. each of the pulls need some data stored earlier in thread local, so we migrate those to the pooled threads - after all data is received from partners, we migrate back the new thread local data accumulated in the child threads to the main thread
- at the end of the interaction we persist the DOMAIN events to DB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想引入请求上下文:http://www.corej2eepatterns.com/Patterns2ndEd/ContextObject。 htm
如果您使用的是 WebContainer,则可以在 Filter 中处理此类对象的创建/销毁;如果您使用的是 ApplicationServer,则可以在 Interceptor 中处理此类对象的创建/销毁。
you may want to introduce a request context: http://www.corej2eepatterns.com/Patterns2ndEd/ContextObject.htm
you can handle creation/destruction of such an object in a Filter if you're using a WebContainer or an Interceptor if you're using an ApplicationServer.