Skip to main content

skip to main content

developerWorks  >  Java technology  >

Connection pools

Dive into connection pooling with J2EE

developerWorks

Return to article


An EJB application accesses a database resource by using pooled connection objects

import java.sql.*; 
  import javax.sql.*; 
  // import here vendor specific JDBC drivers
  public ProductPK ejbCreate() {
        try {
  // initialize JNDI lookup parameters
              Context ctx = new InitialContext(parms);
  ...
              ConnectionPoolDataSource cpds = (ConnectionPoolDataSource)ctx.lookup(cpsource); 
  ... 
  // Following parms could all come from a JNDI look-up 
              cpds.setDatabaseName("PTDB"); 
              cpds.setUserIF("XYZ"); 
  ...
              PooledConnection pc = cpds.getPooledConnection(); 
              Connection conn = pc.getConnection(); 
  ...
  // do business logic
              conn.close();
        }
  ...
  }  
  

Return to article