2

We currently have a very weird problem with a web application running inside Open Liberty 18.0.0.4. We are injecting a Mongo database connection into a JAX-RS request handler. The producer for the injected value simply returns a member variable that can never get null once its initialized. The service runs fine for many hours, sometimes even days and then suddenly any access to the injected Mongo database throws a NPE. Here's a rough sketch of the code:

@ApplicationScoped
public class BackingServiceConnectionFactory {
    private MongoDatabase m_mongoDatabase = ...;

    @Produces
    public MongoDatabase getMongoDatabase() {
        return m_mongoDatabase;
    }
} 


@Path("repository")
public class RepositoryImpl {
    @Inject
    private MongoDatabase m_database;

    @GET
    public Response foo() {
      MongoCollection<Document> workflows = m_database.getCollection("workflows"); // <== NPE
    }
}

Once the injected variable is null all subsequent injections will also lead to null rendering the service unusable. Any idea what the problem could be or how to debug it?

sithmein
  • 437
  • 3
  • 11
  • Your producer is `@Dependent` scoped (default when no scope is given) which means it can return `null` - that's what CDI specification defines. So if I were, I would debug *exactly how* you initialize the field `m_mongoDatabase` inside `BackingServiceConnectionFactory` and if that gets called before the producer. Also note that weld is lazy in bean creation and won't create the beans unless really needed which may have some impact on your expectations of `m_mongoDatabase` being initialized. – Siliarus Sep 09 '19 at 10:15
  • We solved the problem meanwhile: a @PostConstruct method was throwing an uncaught (and unlogged) RuntimeException. This one exception then broke every subsequent dependency injection resulting in the fields being null. – sithmein Sep 10 '19 at 11:38

0 Answers0