How to connect java application to database using JNDI?

10,699

Solution 1

The real difference between your question and the examples is that you're using a standalone Java application. Pretty-much every example assumes that you're running within a Java EE application container. In that case you define the database connection to the container, use JNDI to get the DataSource, and get the connection from that.

When you do a JNDI lookup to get your DataSource, the JNDI framework looks for an initial context factory (a class that implements InitialContextFactory). In your Java EE application, the container provides that. In your standalone Java application, the initial context is null, and no further lookups resolve correctly.

One of the ways you can resolve this is to create your own initial context factory:

public class MyContextFactory implements InitialContextFactory

and inject that into JNDI at startup:

System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "mypackag.MyContextFactory");

and then return a new ObjectFactory from the getInitialContext call, and then implement the ObjectFactory to return a DataSource (also probably custom) from the getConnection() call.

That would all work, but it's overkill. It would be easier to just use the normal JDBC connection string approach to get a connection directly in your application rather than trying to use a DataSource. Or use a framework like Spring to inject it for you or to separate the database connect information from the code (in which case you're using Spring configuration files rather than JNDI directly).

The one reason I'd advocate creating a custom context factory and data source approach is if you have common JPA code that you want to run both within a Java EE app and within a standalone app (it's otherwise non-trivial to configure the same code to do both). That doesn't sound like your case.

So, since you're standalone and not in a Java EE container, I think your real answer is that your use case is not appropriate for a DataSource (unless you move to a framework like Spring that would provide one).

Solution 2

There's a nice tutorial for that here and here

The database should be defined somewhere (LDAP)? There's a series of old articles using the directory service with JNDI (ultimately in your case to get the server information) here.

A nice introduction to naming services here

Share:
10,699
CuriousMind
Author by

CuriousMind

Updated on June 23, 2022

Comments

  • CuriousMind
    CuriousMind almost 2 years

    I am implementing JNDI concept to get a connection to Database. I googled to get the starting point, however didn't get it.

    The things that i want to do is have a simple java standalone application which used JNDI concept for getting connected to a database.

    Sample source that i have is:

    DataSource dataSource = null;
    
    Context context = null;
    
    
    
    
    
        try {       
    
            context = new InitialContext();
    
            dataSource = (DataSource) context.lookup("database_connection");
            }
    
        catch (NamingException e) {
    
                System.out.println("Got naming exception, details are -> " + e.getMessage());
    
            }
    

    Now, where we define database_connection? Is that defined in an xml file, and if so where do we specify that and what format of that is?

    If any pointers can be provided, that would be great.

    Thanks