• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1455
  • Last Modified:

Java: JDBC & JNDI: Retrieving a Context

Hi,

Am trying to retrieve a DataSource object (once it has been bound).

I believe I manage to bind the object successfully - a .bindings file is created in the relevant directory.
However, when I try to retrieve the object (doing a lookup), nothing is returned.

Maybe I'm just oversimplifying things...the MyDataSource class just extends DataSource and doesn't do much else.

Relevant code below:

      MyDataSource ds = new MyDataSource()
      ds.setDescription("Data Source");
      ds.setDataSourceName("jdbc/Oracle");
     
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
      env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
     
      Context ctx = new InitialContext(env);

      // Register the data source to JNDI naming service
      // for application to use
      ctx.ind("jdbc/Oracle", ds);

    // The following call will return null
     DataSource  new_ds = (DataSource) ctx.lookup(jdbc/Oracle);



Thanks,


Andy
0
context
Asked:
context
1 Solution
 
patbinCommented:
try this solution

Context ic = new InitialContext();
       java.lang.Object objref =
          ic.lookup("java:comp/env/jdbc/Oracle");

OR

private String dbName = "java:comp/env/jdbc/Oracle";

InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);

Connection con =  ds.getConnection();

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now