Hibernate是否和akka actor一起工作? [英] does Hibernate work with akka actor?

查看:148
本文介绍了Hibernate是否和akka actor一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要关心的是 ThreadLocal
akka演员不绑定到特定的线程,所以任何线程本地存储的使用对于akka演员都是有问题的

Hibernate使用 ThreadLocal



在这种情况下它们可以共存吗?

解决方案

是。我使用Hibernate(通过JPA接口)和Akka actors。



我有两种方法可以处理多线程:


  1. 如果每个参与者更新不能与其他参与者冲突的信息(例如,表中的每一行都不会被多个参与者使用),则可以为每个参与者创建一个实体管理者并将其用于演员的生活。尽管如果事务失败,最好放弃实体管理器。

  2. 否则,在每次调用需要访问数据库的actor(通过它的receive函数)时,创建一个新的实体管理员,开始一个事务,访问数据库,提交事务,然后关闭实体管理器。 我有第一种方法运行在生产系统中。

    my main concern is on ThreadLocal. akka actors don't bind to specific thread, so any use of thread local storage will be problematic on akka actors

    does Hibernate use ThreadLocal?

    can they coexist in this case?

    解决方案

    Yes. I use Hibernate (via the JPA interface) together with Akka actors.

    I have two approaches to deal with multi-threading:

    1. If each actor updates information that cannot conflict with other actors (for example, each row in a table will never be used by multiple actors), you can create a single entity manager per actor and use that for the life of the actor. Although, if a transaction fails it may be best to discard the entity manager.
    2. Otherwise, in each invocation of the actor (via it's receive function) that needs database access, create a new entity manager, begin a transaction, access the database, commit the transaction, and then close the entity manager.

    I have the first approach running in production systems.

    这篇关于Hibernate是否和akka actor一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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