0

既存のデータでスプリングブートアプリケーションをロードしようとしています。私は "システム"ユーザーとデータを入力したいと思います。私は傍受され、 "システム"ユーザを作成する以下のコンポーネント定義を持っています。問題はこのコンポーネントですH2に対してスプリングブートアプリケーションを実行するとトランザクションがデータベースにコミットされますが、アプリケーションがPostgreSQLデータソースで実行されている場合にははコミットされません。取引をコミットする方法を理解するのを手伝ってください。SpringBootアプリケーション - AuditorAwareトランザクションがPostgres DBにコミットされていません

getCurrentAuditorメソッドのオーバーライドされた実装は、アプリケーションのメインクラスは、注釈を以下た

@Transactional 
@Component("auditorProvider") 
public class UserAuditorAware implements AuditorAware<User> { 

    @Inject 
    private UserRepository userRepository; 

    private User _systemUser; 

    @Override 
    public User getCurrentAuditor() { 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if (authentication == null) { 
      if (_systemUser != null) { 
       return _systemUser; 
      } 
      Optional<User> systemUser = userRepository.findOneByUsername("system"); 
      if (!systemUser.isPresent()) { 
       // TODO: decide on which user to designate when entities are created by devices 
       User user = new User("system"); 
       user = userRepository.save(user); 
       userRepository.flush(); 
       systemUser = Optional.of(user); 
      } 
      _systemUser = systemUser.get(); 
      return _systemUser; 
     } 
     CurrentUser userDetails = (CurrentUser) authentication.getPrincipal(); 
     return userDetails.getUser(); 
    } 
} 

以下の通りです。

@ComponentScan 
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class, SecurityAutoConfiguration.class }) 
public class Application { 
... 

答えて

0

この問題を解決するには、REQUIRES_NEW伝播値が必要でした。新しいトランザクションを作成し、既存のトランザクションがあれば中断します。なぜデフォルトの伝播が機能していないのか分かりません。

@Transactional(propagation= Propagation.REQUIRES_NEW) 
@Component("auditorProvider") 
public class UserAuditorAware implements AuditorAware<User> { 

    @Inject 
    private UserRepository userRepository; 

    private User _systemUser; 

    @Override 
    public User getCurrentAuditor() { 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if (authentication == null) { 
      if (_systemUser != null) { 
       return _systemUser; 
      } 
      Optional<User> systemUser = userRepository.findOneByUsername("system"); 
      if (!systemUser.isPresent()) { 
       // TODO: decide on which user to designate when entities are created by devices 
       User user = new User("system"); 
       user = userRepository.save(user); 
       userRepository.flush(); 
       systemUser = Optional.of(user); 
      } 
      _systemUser = systemUser.get(); 
      return _systemUser; 
     } 
     CurrentUser userDetails = (CurrentUser) authentication.getPrincipal(); 
     return userDetails.getUser(); 
    } 
} 
関連する問題