Getting DataSource using JNDI in weblogic5.1

Discussions

EJB programming & troubleshooting: Getting DataSource using JNDI in weblogic5.1

  1. Hi ,

    in weblogic.properties i have defined connection pool and JNDI name for that connection like ...

    weblogic.jdbc.connectionPool.oraclePool=\ url=jdbc:weblogic:oracle,\ driver=weblogic.jdbc.oci.Driver,\ loginDelaySecs=1,\ initialCapacity=4,\ maxCapacity=10,\ capacityIncrement=2,\ allowShrinking=true,\ shrinkPeriodMins=15,\ refreshMinutes=10,\ testTable=dual,\ props=user=ermnetdev;password=ermnetdev;server=ermg

    weblogic.jdbc.TXDataSource.oraclePool=oraclePool

    one way to get DataSource thru JNDI name oraclePool is
    defining resource ref during deployment like

    ejb-jar.xml:
    <entity>
      <ejb-name>Customer</ejb-name>
      ......
      <resource-ref>
        <res-ref-name>jdbc/myDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </entity>

    weblogic-ejb.jar.xml:
    <weblogic-enterprise-bean>
      <ejb-name>Customer</ejb-name>
      <reference-descriptor>
        <resource-description>
          <res-ref-name>jdbc/myDB</res-ref-name>
          <jndi-name>oraclePool</jndi-name>
        </resource-description>
      </reference-descriptor>
    </weblogic-enterprise-bean>

    DataSource ds=(DataSource)context.lookup("java:comp/env/jdbc/myDB");


    But i am following a direct route like,

    DataSource ds=(DataSource)context.lookup("oraclePool");

    it is also working fine(so far i have tested).

    My question is

    is my way is correct ? how does it differ from the first one ? is there any chance that my way will create any problem in future ?

    Thanks in advance .

    Regards ,


    Ashim Chakraborty





  2. Hi,

    your first way is more portable than the second and conforms to J2EE standards.

    If you are using the second one and in future want to change the connection to a different pool or change the environment( porting into a new machine) then you are forced to create the pool with the same name and cannot use the existing pool. If you want to use the existing pool you have to change the code.

    If you are using the first just changing the weblogic-ejb-jar.xml will do.

    Srinivas.J
  3. To add more, these XMLs are like JCLs in mainframes which makes the program portable and can change the file etc., outside the code.

    If you dont know about JCL just ignore.

    Srinivas.J
  4. Hi thanks for your reply .
    Rather than hard coding in the code what i have done is ,

    maintaining one cutomdefined class like

    public class Utils
    {
       public static DBJNDINAME = "oraclePool";
    }

    in my bean class i will be using like

    ds = (javax.sql.DataSource)initCtx.lookup(Utils.DBJNDINAME);

    My idea is if any changes is required ( due to change of application server settings ) then i will only change the Utils class .
    I don't have bother about Bean or XML .

    My this idea is working fine in WebLogic 5.1 .

    If any body have some better idea , or find any drawback in my idea please mention it .

    Thanks in advance

    waiting for your ideas and comments.

    Ashim Chakraborty
  5. How do you register your Utils class with JNDI?