Minor addition to the MobileFirst Foundation Java SQL Adapter tutorial.

I really don’t want to be hardcoding Database configuration in my adapters. Or repeating it for multiple adapters that share the same Database access. (Not to mention not wanting to require the referenced Apache BasicDataSource class and libraries either.)

And since MobileFirst Foundation runs on a Java EE server anyway, why would I not use DataSources configured as server Resources?

(Here’s the WebSphere Liberty way to do this: Configuring relational database connectivity in Liberty)

So I use javax.sql.DataSource and a couple of javax.naming JNDI classes.

I’ll still put the JNDI name itself in Adapter configuration parameters:

    <property name="dataSource.jndiName" displayName="DataSource JNDI Name" defaultValue="jdbc/myDataSource"/>

Now, in a “DAO” class that I instantiate (as a singleton) from my Resource class (or could do from the Application class), I use the following simple code to obtain that DataSource:

        try {  
            javax.naming.InitialContext ctx = new javax.naming.InitialContext();  
            this.dataSource = (javax.sql.DataSource) ctx.lookup(dataSourceJndiName);  
        }  
        catch (javax.naming.NamingException e) {  
            throw new RuntimeException("Unable to locate specified DataSource: " + dataSourceJndiName);  
        }

Where dataSourceJndiName is obtained from the ConfigurationAPI:

configApi.getPropertyValue("dataSource.jndiName")