Problem in locating jndi datasource in WebSphere Application Server6.1 - Websphere

This is a discussion on Problem in locating jndi datasource in WebSphere Application Server6.1 - Websphere ; Hi All, I am facing a problem in WebSphere Application Server when using jdbc datasource. I have created JDBC datasource named "jdbc/myDataSource". Code i use:- Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.Websphere.naming.WsnInitialContextFactory"); InitialContext ctx = new InitialContext(env); DataSource ds = ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Problem in locating jndi datasource in WebSphere Application Server6.1

  1. Problem in locating jndi datasource in WebSphere Application Server6.1

    Hi All,

    I am facing a problem in WebSphere Application Server when using jdbc datasource.
    I have created JDBC datasource named "jdbc/myDataSource".
    Code i use:-

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.ibm.Websphere.naming.WsnInitialContextFactory");
    InitialContext ctx = new InitialContext(env);
    DataSource ds = (DataSource) ctx.lookup("jdbc/myDataSource");

    This works fine but gives warning in the server logs as:-
    "I J2CA0122I: Resource reference jdbc/myDataSource could not be located, so default values of the following are used: [Resource-ref settings]

    res-auth: 1 (APPLICATION)
    res-isolation-level: 0 (TRANSACTION_NONE)
    res-sharing-scope: true (SHAREABLE)
    res-resolution-control: 999 (undefined)

    I J2CA0107I: Component-managed authentication alias not specified for connection factory or Data Source myDataSource."

    If i use
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");
    it gives error as:
    Name comp/env/jdbc not found in context "java:".

    I referred this link :- http://www.webspherepower.com/issues.../00001357.html

    It says that i should create alias and use it. But i dont have WebSphere Studio Application Developer. So how do i create one???

    I tried putting this in web.xml of my application

    jdbc/myDataSource
    javax.sql.DataSource
    Container
    Shareable


    and in ibm-web-bnd.xmi (binding)




    Still didnt solved my problem.Please help me..




  2. Re: Problem in locating jndi datasource in WebSphere ApplicationServer 6.1

    Firstly, you shouldn't need to use the properties object to create the InitialContext. The following should work just fine...
    InitialContext ic = new InitialContext();
    Websphere will default itself to using the correct initial context factory implementation.

    Secondly, you should be looking up the local reference using the following...
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");

    This will use the binding in ibm-web-bnd.xmi to map through to the global JNDI name.

    I would suggest the jndiname in the ibm-web-bnd.xmi, would be better as jdbc//MyDataSource
    to prevent confusion with the local reference.

    Hope this helps

    P.S
    If you want to run outside of websphere (e.g Java main or JUnit), I would recommend the MockEJB library. See attached for example code.





  3. Re: Problem in locating jndi datasource in WebSphere ApplicationServer 6.1

    Couldn't get the attachment facility to work, so here is the source...
    Code:
    public class DatasourceBindTest {
    
    private static String sql = "select COL1, COL2 from local.table1 order by 1,2";
    
    public DatasourceBindTest() {
    try {
    MockContextFactory.setAsInitial();
    Context ctx = new InitialContext();
    MockContextFactory.setDelegateContext(ctx);
    
    DB2DataSource myDS = new DB2DataSource();
    myDS.setDatabaseName("local");
    myDS.setUser("user");
    myDS.setPassword("pass");
    ctx.rebind("java:comp/env/jdbc/MyDataSource", myDS);
    } catch (NamingException e) {
    e.printStackTrace();
    }
    }
    
    public void testDatasource() {
    try {
    InitialContext ic = new InitialContext();
    DataSource myDS = (DataSource) ic.lookup("java:comp/env/jdbc/MyDataSource");
    Connection con = myDS.getConnection();
    ResultSet rs = con.prepareStatement(sql).executeQuery();
    while (rs.next()) {
    String col1 = rs.getString(1);
    String col2 = rs.getString(2);
    System.out.println(col1 + ": " + col2);
    }
    } catch (NamingException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    
    public static void main(String[] args) {
    DatasourceBindTest test = new DatasourceBindTest();
    test.testDatasource();
    }
    }

  4. Re: Problem in locating jndi datasource in WebSphere Application Server 6.1

    On Mar 26, 6:39 am, wrote:
    > Hi All,
    >
    > I am facing a problem in WebSphere Application Server when using jdbc datasource.
    > I have created JDBC datasource named "jdbc/myDataSource".
    > Code i use:-
    >
    > Hashtable env = new Hashtable();
    > env.put(Context.INITIAL_CONTEXT_FACTORY,
    > "com.ibm.Websphere.naming.WsnInitialContextFactory");
    > InitialContext ctx = new InitialContext(env);
    > DataSource ds = (DataSource) ctx.lookup("jdbc/myDataSource");
    >
    > This works fine but gives warning in the server logs as:-
    > "I J2CA0122I: Resource reference jdbc/myDataSource could not be located, so default values of the following are used: [Resource-ref settings]
    >
    > res-auth: 1 (APPLICATION)
    > res-isolation-level: 0 (TRANSACTION_NONE)
    > res-sharing-scope: true (SHAREABLE)
    > res-resolution-control: 999 (undefined)
    >
    > I J2CA0107I: Component-managed authentication alias not specified for connection factory or Data Source myDataSource."
    >
    > If i use
    > DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");
    > it gives error as:
    > Name comp/env/jdbc not found in context "java:".
    >
    > I referred this link :-http://www.webspherepower.com/issuesprint/issue200410/00001357.html
    >
    > It says that i should create alias and use it. But i dont have WebSphere Studio Application Developer. So how do i create one???
    >
    > I tried putting this in web.xml of my application
    >
    > jdbc/myDataSource
    > javax.sql.DataSource
    > Container
    > Shareable
    >

    >
    > and in ibm-web-bnd.xmi (binding)
    >
    >
    >

    >
    > Still didnt solved my problem.Please help me..


    In WebSphere 5.x the recommended configuration for a datasource was to
    make in the credentials container managed. In WAS 6 IBM recommends
    using component managed authentication aliases (and you will notice
    that in was 6 in the console container managed credentials are
    labelled as deprecated for was 6.

    I think you need to check how you configured and applied the
    authentication alias to your datasource. And I think you also need to
    confirm that the credentials and scope of credentials (component or
    container) you supply in the web.xml are consistent with what you have
    configured for the datasource.

    Also, just for the sake of sanity, check your datasource. Based on
    the error message it seems that you have been able to lookup the jndi
    name of the datasource, but without properly supplying the
    credentials, your application will not have any privileges granted,
    just the ability to create a reference to a connection object for the
    datasource.

    How is your datasource configured? To use component or container
    managed login? Also, when you test your datasource connection (by
    clicking the "test" button for the datasource connection in the
    adminconsole, is the test successful? If so this is a pretty good
    indication that you have some mismatch between the credentials you
    have configured for the datasource and the authentication alias you
    have specified in the web.xml. Hope this sparks some ideas. Please
    let me know if I can further assist with, or further confuse the
    issue.

    Best Regards,

    Brian

    Bronze Drum Consulting, Inc.

    http://www.bronzedrum.com


+ Reply to Thread