Castle ActiveRecord 在 CreateAndFlush 上从错误的表返回 ID

发布于 2024-12-29 14:34:29 字数 649 浏览 0 评论 0原文

我在仓库项目中使用 Castle ActiveRecord。我有几个经常更新的表:infeed、stack、stack_location。如果发生很多事情(添加输入、形成堆栈),有时调用 CreateAndFlush 后在对象上设置的 id 是来自另一个表的 ID。数据库是MySQL,ID列是int(11)而不是null auto_increment主键。我使用 PrimaryKeyType.Native 作为 ID 属性。

我还遇到了以下问题: 如何检索 Castle ActiveRecord 中的最后一个插入 ID?< /a> 但仅限于我的进料台(我通常有 4 堆/进料台)。我在这种情况下添加了一个捕获,即 5000 毫秒的睡眠和之后的 SaveAndFlush,这确保我在此时获得 ID。

我需要直接调用flush,因为我需要ID将其写入PLC,PLC稍后会将其交给我。我的应用程序是多线程的,但如果我是正确的,当所有这些都出错时,只有一个线程写入数据库。我没有任何标记为易失性的东西,也没有阻止多个线程访问数据库的锁,但我认为 Castle ActiveRecord 会在适当的地方进行锁定。

I'm using Castle ActiveRecord in a warehouse project. I have several tables that get frequently updated: infeed, stack, stack_location. If a lot is happening (infeed is added, stacks are formed) sometimes the id that is set on an object after calling CreateAndFlush is the ID from another table. The database is MySQL, the ID columns are int(11) not null auto_increment primary key. I use PrimaryKeyType.Native for the ID property.

I also suffered from the following issue:
How to retrieve the last insert ID in Castle ActiveRecord?
but only with my infeed table (and I normally have something like 4 stacks / infeed). I added a catch to that situation with a 5000ms sleep and a SaveAndFlush afterwards, which makes sure I get an ID at that point.

I need to call flush directly because I need the ID to write it to a PLC which will hand it to me at a later point. My application is multi-threaded, but if I'm correct there's just one thread writing to the database when all of this goes wrong. I don't have anything marked as volatile, there are no locks in place preventing database access from multiple threads, but I assume Castle ActiveRecord does locking where appropiate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2025-01-05 14:34:29

正如对我的问题的评论中已经提到的:

我讨厌回答自己的问题,但似乎我发现我做错了。显然,nHibernate 故意不支持线程感知。因此,我需要处理在不同线程内使用的 nHibernate 会话。对于 Castle ActiveRecord,解决方案似乎是在线程中使用“using(new SessionScope())”块来解决这个问题。如果有人有更好的建议或更清晰的解释,我显然很感兴趣。

我遇到的所有问题都没有再出现。

As already mentioned in a comment on my question:

I hate to answer my own questions, but seems I've found that I'm doing it all wrong. Apparently, nHibernate is intentionally not thread-aware. Because of that I need to take care of the nHibernate sessions that are used inside different threads. With Castle ActiveRecord the solution seems to be to use 'using(new SessionScope())' blocks in threads to cater for this issue. If anyone has a better suggestion or a more clear explanation I'm obviously interested.

All issues I had have not returned anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文