Discussions

EJB programming & troubleshooting: jdbc Connection Pool from java class

  1. jdbc Connection Pool from java class (8 messages)


    I would like to get the jdbc connection pool from my connection pool created weblogic container from my java programs note session or entity beans.Iam weblogic context not the default context of bean(session or entity)
    Iam skeptical about the jdbc connection returning back to connection pool as container will take care of returning
    back to the connection pool.

    Please through your ideas onthat


  2. Hi Bob,

    As far I understood you want to get connection from Weblogic connection pool in your java or jsp.

    before go for this import necessary pacakages.

    InitialContext initCtx = null;

    try
    {
     initCtx = new InitialContext();

     DataSource ds;

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

     Connection con = ds.getConnection();


    }
    catch(NamingException ne)
    {

    }

     once u get connection do your DB activity using Statement or PrepareStatement Object. To return this connection back into pool assign Statement or PrepareStatement=null. Weblogic will take care to return this back to pool.

    Arun Sethia
    sethia_jain@yahoo.com
  3. Actually common practice is close the connection after you use it in a finally block. Waiting for the garbage collector to run is bad practice.

    Connection con = null;
    try
    {
      initCtx = new InitialContext();
      DataSource ds = (javax.sql.DataSource)initCtx.lookup("DATASOURCE JNDI NAME");
      con = ds.getConnection();
      .......
    }
    catch(Exception ne)
    {

    } finally
    {
      try { con.close(); } catch (Exception ignore) {}
    }

    Close() will return the connection to the pool. If you need the connection again in the same transaction you can access the datasource again and you will get the same physical connection back.
  4. If you call the weblogic or any Appserver, you have to use
    http or t3 protocol to get connected to app server(Like Test clients).I believe it will take more resources as its opening a separate connection to access the resources of app server.
    My question is accessing thro java class or writing a session bean...Which is a better way to access the resouces
    of App server like Jdbc connnection pools..

    Please thro you ideas on this..
    Bob.
  5. Hi Bob,

      If u are using Transactions with Database connection pool
      than it is better to you go for Session bean or u can
      choose DAO Patteren where session bean will call a DAO
      (Java Class) but either or OR case session bean should be
      under Transaction required so DAO will also run under
      that Transaction.

      The advantage u will get in DAO is you can change your
      Database SQL without affecting Session Bean.

    sethia_jain@yahoo.com
  6. Arun,
         Thannks for your reply.My question is that what are the potential dis-advantages of having accessing the connection pool outside (fron java class file) rather than accessing from EJB's....

    This is my actual Question....

    Throw your thoughts

    Bob
  7. Hi Bob,

       If u use connection within EJB u can manage Transaction very easly (Flat and Nested Transaction). When u are using with Session bean u can use Bean managed Transaction. Where u are utilizing Middelware services effactively.

      Ya we use it from Java class not with EJB we have to manage Transaction fwith in java code.


    Regards
    Arun

      
  8. If I want the sessionbean and DAO under the same transaction, should I set the datasource in jta mode(support two-phrase commit);
    But when I want to get connection there will throw
    exception
  9. jdbc Connection Pool from java class[ Go to top ]

    When we are accessing DAO from Session bean we need to define Tranaction required with session bean.

    This will help u lot. Right now j2EE spec. says nobody is supporting 2- phase Commit.