get Connection in Session bean from Entity CMP Bean


EJB design: get Connection in Session bean from Entity CMP Bean

  1. I have an Entity CMP Bean and Session bean.
    (I use Borland App Server 4.5.1 and JBuilder 5.0)
    In Entity Bean in META-INF\ejb-inprise.xml
    there are connection parameters to database, such as:


    How to get this connection parameters from Session bean?
    Or how to get java.sql.Connection, obtained in CMP Bean, from Session bean?

    I'm sure, there are methods to get it using JNDI.

    THank you.
  2. Follow the following steps

    1) Get the intial context
       Context ctx = getInitialContext();
    2) Look up the datasource

       e.g DataSource ds = ctx.lookup("DataSource Name");
    3) Connection cn = ds.getConnection();

    U can use this connection now

    Note : U have to implement getInitialContext() method. The code should be ut application server specific
  3. Hey there.
    The steps in the message above are the standard way. I just wanted to note that getting an initial context is not app server specific. The portable way to do it is the obvious one:
    InitialContext ic = new InitialContext();

  4. Hi anton,
      use this code to get connection

    InitialContext initCtx = null;

     initCtx = new InitialContext();

     DataSource ds;

     ds = (javax.sql.DataSource)initCtx.lookup("DATASOURCE JNDI NAME");

     Connection con = ds.getConnection();
    catch(NamingException ne)



    Arun Sethia