1

I'm using Apache's commons DBCP 1.4 version of Jar. And I'm following JNDI example on binding BasicDataSource through fscontex. As shown in code provided by the below link http://commons.apache.org/proper/commons-dbcp/guide/jndi-howto.html.

I have written similar standalone code, but I'm getting the below error "javax.naming.OperationNotSupportedException: Can only bind References or Referenceable objects" . As per my kmowledge, any objects that wants to be referenced must implement javax.naming.Referenceable interface and define getReference method. I'm not sure if BasicDataSource is doing that or not ?

Here is the code that I have used.

import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

public class DBCPTest {

public static void main(String[] args) {
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        System.setProperty(Context.PROVIDER_URL, "file:///F:/JNDI/");
        InitialContext ic = new InitialContext();

        // Construct BasicDataSource
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName("org.apache.commons.dbcp2.TesterDriver");
        bds.setUrl("jdbc:apache:commons:testdriver");
        bds.setUsername("username");
        bds.setPassword("password");

        ic.rebind("jdbc/basic", bds);

        // Use
        InitialContext ic2 = new InitialContext();
        DataSource ds = (DataSource) ic2.lookup("jdbc/basic");

        Connection conn = ds.getConnection();
        conn.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

And here are the list of JAR's that I'm having in my classpath

  • commons-dbcp-1.4.jar
  • commons-ppol-1.6.jar
  • fscontext.jar
  • providerutil.jar
  • jndi.jar

Any insight on resolving this issue or pointing out what am I doing incorrectly is appreciated.

Thanks CM

cmodha
  • 105
  • 9
  • Does "F:/JNDI/" exist on the filesystem? – Totoro Jun 19 '14 at 22:49
  • Yes, the directory does exits on the file system. when I looked into the api for BasicDataSource it doesn't implement javax.naming.Referenceable interface. And that is the reason, I guess it's throwing this error. – cmodha Jun 24 '14 at 14:35

1 Answers1

0

After browsing through the web, I did find a way to bind BasicDataSource through files based context. I used the example provided by the below link

http://www.massapi.com/class/javax/naming/StringRefAddr.java.html

System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty(Context.PROVIDER_URL, "file:///F:/JNDI/");

    Reference ref = new Reference("javax.sql.DataSource","org.apache.commons.dbcp.BasicDataSourceFactory", null);
    ref.add(new StringRefAddr("driverClassName","com.ibm.db2.jcc.DB2Driver");
    ref.add(new StringRefAddr("url","jdbc:db2://myhost.example.com:port/dbname");
    ref.add(new StringRefAddr("password", "SomePassord");
    ref.add(new StringRefAddr("username", "myUser");

    ref.add(new StringRefAddr("maxActive","100"));
    ref.add(new StringRefAddr("maxWait", "10000"));
    ref.add(new StringRefAddr("maxIdle", "10"));
    ref.add(new StringRefAddr("minIdle", "5"));

    ref.add(new StringRefAddr("testOnBorrow", "true"));
    ref.add(new StringRefAddr("testOnReturn", "false"));
    ref.add(new StringRefAddr("testWhileIdle","true"));
    ref.add(new StringRefAddr("validationQuery","SELECT 1"));

    ref.add(new StringRefAddr("timeBetweenEvictionRunsMillis",Integer.toString(10*60*1000)));
    ref.add(new StringRefAddr("minEvictableIdleTimeMillis",Integer.toString(2*60*1000)));
    ref.add(new StringRefAddr("numTestsPerEvictionRun","10"));

    ref.add(new StringRefAddr("removeAbandoned", "true"));
    ref.add(new StringRefAddr("removeAbandonedTimeout", Integer.toString(30*60)));
    ref.add(new StringRefAddr("logAbandoned", "true"));

    Context ctx = new InitialContext();
    ctx.rebind("jdbc/MyDataSource",ref)
    ctx.close();
cmodha
  • 105
  • 9