Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of container JNDI data source

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:

  1. 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.
  2. 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?).

like image 815
nikita Avatar asked Oct 23 '09 19:10

nikita


People also ask

What is JNDI data source?

A JNDI DataSource object is a file that contains the configuration details necessary to connect to a database. The DataSource object must be registered on a JNDI server, where it is identified using a JNDI name. You can register your DataSource object directly on your application server via its JNDI service.

What is JNDI DataSource in Tomcat?

Actual benefit of DataSource comes when we use it with a JNDI Context. For example, connection pool in a web application deployed in a servlet container. Most of the popular servlet containers provide built-in support for DataSource through Resource configuration and JNDI context.

What is the difference between JDBC and JNDI?

JDBC is Java Database Connectivity API, while JNDI is Java Naming and Directory Interface API. The main thing here is that in a JNDI directory you're actually storing a JDBC DataSource, so, you're simply using JDBC to obtain a Connection via JNDI lookup.

What is JNDI and how it works?

The Java Naming and Directory Interface™ (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java™ programming language. It is defined to be independent of any specific directory service implementation.


2 Answers

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:

<bean class="com.my.Persister">
    <property name="dataSource" ref="dataSource" />
</bean>

The data source can be defined using a JNDI-lookup:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource" /> 

In a test environment, you could inject the data source directly:

<bean id="dataSource" class="apache.db.PoolingDataSource">
    <!-- config goes here -->
</bean>
like image 195
oxbow_lakes Avatar answered Sep 18 '22 12:09

oxbow_lakes


These references are pretty old but may help to use jnpserver (JBoss Naming Service provider):

  • Working With JNDI In A J2SE Application
  • Standalone JNDI server using jnpserver.jar
like image 35
Pascal Thivent Avatar answered Sep 21 '22 12:09

Pascal Thivent