EJB and getConnection

Discussions

EJB programming & troubleshooting: EJB and getConnection

  1. EJB and getConnection (2 messages)

    How do you ppl get your connections in an EJB?
    Best practice? Best performance?

    -----------------
    I saw this at SUN (http://java.sun.com/developer/onlineTraining/Programming/JDCBook/bmp3.html)

    //Create static instance of database driver
    static {
      new weblogic.jdbc.jts.Driver();
    }

    //Get registered driver from static instance
    public Connection getConnection() throws SQLException{
      return DriverManager.getConnection(
                            "jdbc:weblogic:jts:ejbPool");
    }

    -----------------

    It looks good and gaining good performance. But I dont like it, while it is WAS specific.

    I m using the JBoss 4.0.

    Threaded Messages (2)

  2. EJB and getConnection[ Go to top ]

    How do you ppl get your connections in an EJB?Best practice? Best performance?-----------------I saw this at SUN (http://java.sun.com/developer/onlineTraining/Programming/JDCBook/bmp3.html)//Create static instance of database driverstatic {  new weblogic.jdbc.jts.Driver();}//Get registered driver from static instancepublic Connection getConnection() throws SQLException{  return DriverManager.getConnection(                        "jdbc:weblogic:jts:ejbPool");}-----------------It looks good and gaining good performance. But I dont like it, while it is WAS specific.I m using the JBoss 4.0.

    Obtain it from a datasource. Look up the datasource from the jndi tree. e,g -

    InitialContext ctx = new InitialContext();
    javax.sql.DataSource dataSrc = (javax.sql.DataSource) ctx.lookup ("jdbc/MyDataSource");
    Connection con = dataSrc.getConnection();

    You will of course have to configure the datasource using your appserver configuration tools.

    You get the following advantages -

    1. It Perform well - since appservers usually (the ones I have seen) have a db connection pool behind the datasource

    2. Code will be free from any vendor specific details - all vendor specific stuff is in the configuration step.

    3. You can configure the datasource appropriate to your need s - XA connections, row pre-fetch, connection leak profiling, pool shrinking/expansion etc etc.

    4. Most importantly - the db operations you do within an EJB by obtaining connections this away would be part of the ejb transaction (the container will know about it). If you obtain a connection by loading your own driver the container wouldn't "know" about it and the operations you do using that connection can not be part of a container transaction.

    Reagrds
    Kingshuk
  3. EJB and getConnection[ Go to top ]

    Thanks alot Kingshuk!