Skip to Content
Former Member
Jul 21, 2005 at 04:38 AM

Problems Accessing a DataSource from EJB


I'm having problems getting a DataSource to work from a Session Bean. Even after I followed the instructions at this link:

and setup a "Resource Reference" in ejb-jar.xml


where SAPJ2EDB is of course the 'default' DataSource shown by Visual Administrator.

Here's the code snippet

    Connection conn = null;

    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/SAPJ2EDB");
        conn = ds.getConnection();
        if (conn == null) {
            throw new SQLException("Connection Failed");
    } catch (SQLException sex) {
        return null;
    } catch (NamingException ex) {
        return null;

What happens is this: no Exception is thrown, but no data is returned by the Bean's business method. In other words, no data displays in my Web Dynpro View. The problem is not my EJB method or my WD View because

[1] When I return a dummy String w/o hitting the database everything displays correctly.

[2] I have pulled the code into a console app and replaced the DataSource with a std JDBC connection; this works as expected

I don't understand because the results are the same no matter what I put into the lookup statement:



If the lookup is failing, why don't I get an error??