I would like to configure a DataSource using JNDI in a Java SE app. What is the best way to do this?
So far, I've come across 2 projects:
- Apache Naming. The project page has a specific example for configuring a data source, but it looks like the project is super old and no longer active.
- JBossNS. It looks like it's easy to configure a local-only JNDI using
LocalOnlyContextFactory
, but I haven't found any docs on how to actually configure a data source.
If possible, I would like to also configure the data source with a JTA transaction manager (using JOTM?).
The Simple-JNDI version, referenced by klenkes74, is not under active development any more. Because I encountered some issues with it I forked it, did some bug fixes and implemented some new features. I already used the old version not only for testing but in production too because I prefer a Service Locator pattern over Dependency Injection although the latter one is more trendy nowadays.
You can easily use Simple-JNDI to define a DataSource or a connection pool declaratively and get it bound to a JNDI Context.
Define a jndi.properties file in your classpath:
The property file looks like:
Now you can access your DataSource from inside your code this way:
For more information see https://github.com/h-thurow/Simple-JNDI
These references are pretty old but may help to use jnpserver (JBoss Naming Service provider):
Why are you using JNDI for this? It's not that it's a bad solution if you have a provider but there are alternatives such as dependency injection (IoC: via Spring or Guice).
The Spring JDBC data access is described here. The great thing is that you can use Spring to inject a
DataSource
into your code:The data source can be defined using a JNDI-lookup:
In a test environment, you could inject the data source directly:
A very easy to use solution for stand-alone JNDI is simple-jndi. It works like a charm as long as you only need it within a single JVM, since it's a library no network server.