2

We have developed an application in Spring Boot + spring data (backend) + MongoDB and used IBM Websphere Liberty as application Server. We were used "Application Managed DB Connection" in an yml file and enjoyed the benefit of Spring Boot autoconfiguration.

Due to policy changes, we would need to manage our DB Connection in Liberty Server(using mongo feature), in Server.xml. I spent whole day in finding out an good example to do this, but dont find any example in Spring with "Container Managed MongoDB Connection" in IBM Websphere Liberty Server.

Can someone please support here?

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
omega
  • 592
  • 2
  • 5
  • 21

2 Answers2

0

Check out this other stackoverflow solution. The following is an extension of how you would use that in your Spring Boot app.

You should be able to inject your datasource the same way. You could even inject it into your configuration and wrap it in a Spring DelegatingDataSource.

@Configuration
public class DataSourceConfiguration {

    // This is the last code section from that link above
    @Resource(lookup = "jdbc/oracle")
    DataSource ds;

    @Bean
    public DataSource mySpringManagedDS() {
        return new DelegatingDataSource(ds);
    }

}

Then you should be able to inject the mySpringManagedDS DataSource into your Component, Service, etc.

Dovmo
  • 8,121
  • 3
  • 30
  • 44
0

In the past Liberty had a dedicated mongodb-2.0 feature for the server.xml, however this feature provided pretty minimal benefit, since you still needed to bring your own MongoDB libraries. Also, over time MongoDB made significant breaking changes to their API, including how MongoDB gets configured.

Since the MongoDB API is changing so drastically between releases, we found it better to not provide any new MongoDB features in Liberty and instead suggest that users simply use a CDI producer like this:

CDI producer (holds any configuration too):

@ApplicationScoped
public class MongoProducer {

    @Produces
    public MongoClient createMongo() {
        return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build());
    }

    @Produces
    public MongoDatabase createDB(MongoClient client) {
        return client.getDatabase("testdb");
    }

    public void close(@Disposes MongoClient toClose) {
        toClose.close();
    }
}

Example usage:

@Inject
MongoDatabase db;

@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
public void add(CrewMember crewMember) {
    MongoCollection<Document> crew = db.getCollection("Crew");
    Document newCrewMember = new Document();
    newCrewMember.put("Name",crewMember.getName());
    newCrewMember.put("Rank",crewMember.getRank());
    newCrewMember.put("CrewID",crewMember.getCrewID());
    crew.insertOne(newCrewMember);
}

This is just the basics, but the following blog post goes into much greater detail along with code examples: https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61