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