簡體   English   中英

使用Akka 2.X.X對actor類定義使用@Transactional?

[英]Using @Transactional on actors class definition with Akka 2.X.X?

我曾經在我的bean上使用Spring事務注釋。 然而,Akka的演員不願意。 這是我在Scala中的代碼片段:

@Service
@Transactional
@Scope("prototype")
class MyActor extends Actor with ActorLogging { 

   def receive = {
     //......  throwing a NotInTransaction (no matter what the calling lib is)
   }
}

但是,@ @Transactional適用於我所有其他非actor bean。

我添加了Java作為標記,因為它可能與Akka for Java有類似的問題。

我錯過了什么?

我指出我事先遵循這種技術(我在Scala中改編)讓我的演員創作意識到Spring: http//www.typesafe.com/activator/template/akka-java-spring

更新示例-----------

@Service("eventsListenerActor")
@Scope("prototype")
class EventsListenerActor @Autowired()(val eventRepository: EventRepository) extends Actor {

  def receive = {
    case RetrieveNewEvents =>
      val newEvents = eventRepository.findNewEvents(EventBatchSizeProperty)
  }

}

@Repository
class MyEventRepository extends EventRepository {

   @Transactional       //transactional annotation here
   def findNewEvents(batchSize: Int): List[Event] = {
    //................    code warning that transaction context is not present here !
  }
}

findNewEvents之外的findNewEvents任何調用findNewEvents構建事務上下文。

正如我在下面的評論中所提到的,它可能與演員及其線程方式有關嗎?

應用程序容器中的事務跟蹤基於ThreadLocal變量,因此它不適用於Actors。 沖突並非巧合,因為讓一個演員參與更大的交易違背了意圖:演員不得分享任何東西,只能使用消息進行交流。 在不違反此規則的情況下,兩個參與者不可能在內部保持一致。

遵循此規則的好處很多:正確的封裝,位置透明性,跨CPU或節點的無痛分布,理智的故障處理模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM