Problem using 'DataSource' in Websphere


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, "");
            // Create the Initial Naming Context.
    ctx = new InitialContext(env);
    ds = (javax.sql.DataSource) ctx.lookup ("TestDataSource");

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

    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
  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.

  3. Hi

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

    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.*

            Hashtable parms = new Hashtable();
    Context ctx = new InitialContext(parms);
    DataSource ds = (DataSource) ctx.lookup
    Connection conn =
                    ds.getConnection"tptux", "tptux1");
    catch(Exception e){