1

我有一个事件侦听器 Spring Boot 应用程序,它执行从 Azure 事件中心主题读取操作 -> 将事件持久保存到 DB中。我使用了 spring-cloud-azure-eventhubs-stream-binder(版本 - 1.2.1)的 Sink 来监听我的主题中的事件,它工作得非常好。但是,该事件不会保留在 DB 中。当我查看 JPA 创建的 sql 时,根本没有插入操作。我只能看到在 JPA 保存时执行的 Select 查询。

我只是按照链接eventthubs-binder-sample中的示例进行操作。

@StreamListener(Sink.INPUT)
public void handleMessage(String message, @Header(AzureHeaders.CHECKPOINTER) Checkpointer checkpointer) {
    System.out.println(String.format("New message received: '%s'", message));
    myRepository.save(message); // No Insert operation triggered
}

在 @StreamListener 注释的方法中执行的任何 JPA 保存操作都不会将数据插入到 DB 中。

任何提示都非常感谢..我想我必须对事务同步(KafkaTransactionManager + JPATransactionManager)做一些事情,但不确定..

4

1 回答 1

0

在花了一天的时间后让它工作了。创建了一个新的 JpaTransactionManager 并用它来创建一个 TransactionTemplate。

并在从 TransactionTemplate 创建的新事务中执行我的数据库操作。

TransactionTemplate template = new TransactionTemplate(platformTransactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult()
        {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status)
            {
                myRepository.save(message);
            }
        });
于 2020-02-18T07:35:09.313 回答