Startup hangs when using Spring Data repo inside CDI bean

北慕城南 提交于 2019-12-24 00:42:30

问题


I am facing the following issue - my application hangs on startup (no error is given), when trying to access the Spring Data repository from inside the CDI bean. This is my repo:

import org.springframework.data.jpa.repository.JpaRepository;

public interface ConfigValueRepository extends JpaRepository<ConfigValue, ConfigValueKey> {
}

where ConfigValueKey:

import java.io.Serializable;

class ConfigValueKey implements Serializable {
    private Long keyId;
    private Long node;
}

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import node.HierarchyNode;

@ApplicationScoped
public class ConfigurationSetter {

    private ConfigValueRepository configValueRepository;

    @Inject
    public ConfigurationSetter(ConfigValueRepository configValueRepository) {
        this.configValueRepository = configValueRepository;
    }

    public void init(@Observes @Initialized(ApplicationScoped.class) Object o) {
        String exportLocationVar = System.getProperty("EXPORT_LOCATION");
        if (StringUtils.isNotBlank(exportLocationVar)) {
            configValueRepository.findById(ConfigValueKey.of(3050578512872244649L, HierarchyNode.DATACENTER_ID))
                    .ifPresent(v -> {
                        v.setValue(exportLocationVar);
                        configValueRepository.save(v);
                    });
        }
    }
}

Any ideas? I run the application on JBoss.


回答1:


Currently Spring Data Jpa is a little tricky to configure with CDI. It's not covering all typical scenarios. Last month, I pushed a little pull request to the Spring Data project about CDI configuration and I had opened another to improve Spring Data composition but I haven't news until now with last one. I am filling another issue in the Spring Data project to discuss about support for a better configuration and CDI integration and I made a sample project for that solving some typical integration problems.

If you want to use Spring Data Jpa with CDI, I really recommend you to see my github project. It's an experiment but I wish to send this changes to Spring Data project if they accept them.

Take a look at (https://github.com/arielcarrera/cdi-spring-data-jpa-test).

This project covers Spring Data Jpa with CDI (Weld), JTA (Narayana) and JPA (Hibernate) integration. It has a big spread of tests and it is working good as one expects.

In another way, I suggest you to take care about some points:

  • CDI has lazy initialization by default and uses to be at startup in Spring. So you need to check to put @Eager to your repositories to force initialization at startup.
  • It is important to take care about entity manager scope. Only @Dependent pseudo-scope will work at this moment with the default Spring Data Jpa implementation because it uses entity manager at startup eg. to discover metadata, queries and build the repositories. This point will limit your architecture design, transaction management and sharing of the persistence context.
  • If you want to use declarative transactional demarcation (@Transactional) you can take a look to my project to check how to configure a custom Tx Interceptor.
  • If you want to have a transactional service layer over repository layer, you should take a look to my changes to Spring Data JPA. A pseudo dependent scoped bean has some limitations and you will need to take in consideration a @RequestScoped or @TransactionalScoped entity manager bean in place to share a persistence context between repositories instances (and inside too). Default Spring Data JPA implementation (using @Eager annotation) uses an entitymanager at startup, so a RequestScoped does not work. So I resolved it changing spring data code in my experiment.
  • I don't suggest you to use @RequestScoped at this moment with JTA(Narayana) and CDI. It has a transactional issue discovered in https://developer.jboss.org/message/990406 and @TransactionalScoped works better at this moment.
  • Check that your logging configuration is working and set logging to debug level.

I hope to be helpful.

Regards.



来源:https://stackoverflow.com/questions/57389016/startup-hangs-when-using-spring-data-repo-inside-cdi-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!