Dropwizard how to configure a datasource?

4k views Asked by At

Im new with DropWizard and have this situation:

We had an web application that now will be split into 3 parts:

  • core: has all the business intelligence,
  • api: will be a dropwizard project that will call the core methods,clases and etc. ,
  • and client: that actually will be several clients; web, iphone, android, etc.

Im trying to create a small project first with the dropwizard, and call some small methods on the core. Example if a call: localhost:8080/NewApi/getUserName?id=12345;

have something on my code like this:

@GET
public String getUser(@Auth User user, @QueryParam("id") Optional<String> id) {

   String userName =  net.newapp.core.data.User.getUserName(id);
    return  userName;
}

However I haven't been able to configure the database properly. I have read the JDBI tutorial on the dropwizard, but the examples suggest to use DAOs and the JDBI SQL API, but I already have my queries that I need on the core, I dont want to give that job to the Dropwizard.

To test if the core was properly separated from the old project I created a small webapp using jetty and configuring a simple jndi datasource, and used the core as a simple library and worked just fine, and since Dropwizard use a Jetty I tought that I could configure that on the Dropwizard project, but from some posts I have been reading It looks that I can't do that, so my question is, how I configure a simple datasource that my clases on the core can use?.

Thanks in advance

1

There are 1 answers

1
Christopher Currie On

You have a separation of concerns problem in your code that Dropwizard is not going to solve.

String userName = net.newapp.core.data.User.getUserName(id);

Here you have a static method that's designed to provide you with a userName given an id. Where does the method get the data from? Who knows? It's a static method that provides no other inputs, so for all intents and purposes it's "magic."

From the description of your question, part of the "magic" involves a JNDI lookup. But JNDI lookups still need some "name" for the data source to be looked up. Unless there is more code that's not shown here, that name must be hard-coded in the core library. Hard-coded magic constants make migrations to new frameworks difficult, at best.

Dropwizard is unlikely to ever support JNDI because magic behavior makes systems hard to understand and debug. The use of Jetty as an http server is an implementation detail that is best left untouched. Luckily for you, you don't need it.

I'm guessing at this point, because you haven't really said anything about how you need JNDI configured, but I'm going to assume, since most tutorials on JNDI do it this way, that you need a DataSource object bound to a name that looks like java:comp/env/jdbc/mydatasource. If that is the case, and assuming your data source is configured as described in the Dropwizard documentation, you can add something like the following to your Application#run() method:

DataSourceFactory factory = config.getDataSourceFactory();
DataSource dataSource = factory.build(environment.metrics(), "mydatasource");

Context context = new InitialContext();
Context compCtx = (Context) context.lookup("java:comp");
Context envCtx = compCtx.createSubcontext("env");
Context jdbcCtx = envCtx.createSubcontext("jdbc");
jdbcCtx.bind("mydatasource", dataSource);

The first two lines are the Dropwizard way to make a DataSource that doesn't use JDBI. The rest initializes the JNDI context and registers the data source under the appropriate name.