Problem using 'DataSource' in Websphere

Discussions

EJB programming & troubleshooting: Problem using 'DataSource' in Websphere

  1. Problem using 'DataSource' in Websphere (5 messages)

    I have been trying to use DataSource in Websphere App Server 3.5..

    I have coded as below..

    InitialContext ctx = null;
    javax.sql.DataSource ds = null;
    try {

    java.util.Hashtable env= new java.util.Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
    env.put("javax.naming.Context.PROVIDER_URL","IIOP://localhost:900/");
            // Create the Initial Naming Context.
    ctx = new InitialContext(env);
    ds = (javax.sql.DataSource) ctx.lookup ("TestDataSource");

    ds.getConnection(); }
    catch (Throwable theException) {
    // Handle Exception
    theException.printStackTrace();
    }



    The problem is it is giving a compilation error saying that 'ds'(DataSource) doesnot contain the method getConnection()..I am not able to access any methods in the DataSource..Pls respond...

    Thanks in Advance
    --Raj
  2. I have never seen this problem.

    Perhaps there is a a problem with the cast. When you are using IIOP you usually should not blindly cast the result of a lookup. Instead cast the result of javax.rmi.PortableRemoteObject.narrow(ctx.lookup("name"), Datasource.class) to DataSource.

    Evan.
  3. Hi

     I tried that way...but still the problem persists
  4. Try calling ds.getConnection() with two parameters... user name and password...

    ex.
    Connection conn = ds.getConnection(user, password);
  5. The problem is the DataSource object 'ds' is not showing up any methods..There seems to be some casting problem with this DataSource object(It is an interface..)..I am using Visual Age for Java as my development tool..

    Regards - Raj
  6. Try this code it works for me... also make sure you import javax.sql.*

    try{
            Hashtable parms = new Hashtable();
    parms.put(
    Context.INITIAL_CONTEXT_FACTORY,
    "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
    parms.put(
                    Context.PROVIDER_URL,
                        "iiop://sun522concept-dev/");
    Context ctx = new InitialContext(parms);
    DataSource ds = (DataSource) ctx.lookup
                    ("jdbc/WecsDB");
    Connection conn =
                    ds.getConnection"tptux", "tptux1");
    conn.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }