【问题标题】:SpringBoot application - AuditorAware transaction is not committed with Postgres DBSpringBoot 应用程序 - 未使用 Postgres DB 提交 AuditorAware 事务
【发布时间】:2017-01-27 18:57:25
【问题描述】:

我正在尝试使用预先存在的数据加载 Spring Boot 应用程序。我想用“系统”用户填充数据。我有以下组件定义被拦截并创建一个“系统”用户。问题是这个组件在针对 H2 运行 Spring Boot 应用程序时事务被提交到数据库,但它是当应用程序使用 PostgreSQL 数据源运行时它没有被提交。请帮我弄清楚如何提交交易?

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 {
...

【问题讨论】:

    标签: java spring-security spring-boot spring-data spring-data-jpa


    【解决方案1】:

    我不得不 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2016-10-21
      • 2011-07-02
      • 1970-01-01
      相关资源
      最近更新 更多