小编Fab*_*ien的帖子

在 Spring Rest CompletableFuture 结果上使用 @Transactional

在我当前的 Web 应用程序中,我对所有服务使用 @RestController 和 CompletableFuture 结果。

数据库操作是异步的(CompletableFuture 方法),但我只想在发送结果之前提交操作

我想在--save--异步结束后提交数据库修改(--save--是未来业务的列表)

@RestController
public class MyController {
...

  @RequestMappping(...)
  public CompletableFuture<ResponseEntity<AnyResource>> service(...){

   CompletableFuture ...
   .thenCompose(--check--)
   .thenAsync(--save--)
   ...ect
   .thenApply(
      return ResponseEntity.ok().body(theResource);
    );
  }

}
Run Code Online (Sandbox Code Playgroud)

-> 我试过@Transactional,但它不起作用(在方法结束时提交但异步方法部分或未执行

-> 程序化的其他方式:

@RequestMappping(...)
public CompletableFuture<ResponseEntity<AnyResource>> service(...){

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// explicitly setting the transaction name is something that can only be done programmatically
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

TransactionStatus status = this.platformTransactionManager.getTransaction(def);

CompletableFuture ...
   .thenCompose(--check--)
   .thenAsync(--save--)
   ...ect
   .thenApply(

this.platformTransactionManager.commit(status)

      return ResponseEntity.ok().body(theResource);
    );

}
Run Code Online (Sandbox Code Playgroud)

发生错误“无法停用事务同步 - 未激活”,推测是因为不是同一线程。

有没有正确的方法,将事务性与 CompletableFuture …

spring spring-transactions spring-data-jpa spring-web

5
推荐指数
0
解决办法
1249
查看次数

WebSocketMessageBrokerStats - 如何设置loggingPeriod

如何在WebSocketMessageBrokerStats中设置loggingPeriod以减小值(默认为30')

WebSocketMessageBrkerConfigurationSupport中的@Bean加载WebSocketMessageBrokerStats

版本:Spring 4.2.0.RELEASE

我目前的配置:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class AppWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(final MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
    }

    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        registry.addEndpoint("/entry")
                .setAllowedOrigins("*")
                .withSockJS()
                .setDisconnectDelay(10000);
    }

}
Run Code Online (Sandbox Code Playgroud)

spring spring-websocket

2
推荐指数
1
解决办法
1653
查看次数