A standard way to get a connection from a connection pool

Discussions

General J2EE: A standard way to get a connection from a connection pool

  1. Hi everybody,

    I am developping a Web application which must run with Tomcat 4.1.24 and with Websphere 4.0.
    As the code is portable, I don't meet any problem except this one:

    I can't find a unique way to get a JDBC connection from a connection pool, a way which would run on both environments.
     
    The name of the JNDI resource is "jdbc/digit".

    On Tomcat, the code looks like this:
    ...
    Context ctx = new InitialContext();
    ctx = (Context) ctx.lookup( "java:comp/env");
    DataSource ds = (DataSource)ctx.lookup( "jdbc/digit");
    Connection cnx = ds.getConnection();
    ...

    whereas on Whebsphere it looks like this:
    ...
    Context ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup( "jdbc/digit");
    Connection cnx = ds.getConnection();
    ...

    Of course, the "Tomcat way" doesn't work with Websphere, and the "Websphere way" doesn't work with Tomcat.

    I wonder if there is a standard way to get a connection which would work with any container, a standard way which would be defined by the J2EE interface ?

    Thank you for any answers or any clues ...
  2. As far as I know, you should externalize the string identifying your CP and have it modified in your installation procedure (which can be as simple as a text substitution in a properties file by ant).

    hth,
    sv
  3. Add Resource Reference.[ Go to top ]

    Hi Jean,

    Maybe you have not added resource reference in your deployment descriptor file. If you add the resource reference in your web.xml, you should be able to lookup jdbc/digit in the java:comp/env namespace.

    Regards,
    Senthil
  4. Add Resource Reference.[ Go to top ]

    Yes, you are right.
    Indeed, the jdbc ressource was not declared in the web.xml file. Thank you for the hint.