20.3. The ConnectionProvider class

A specialized connectionProvider class is a common class that extends the ConnectionProvider abstract class and provides the implementation of the following abstract methods:

A connectionProvider can implement the code to interact with a connection pool system.

20.3.1. The init method

The init method should be used to initialize the chosen data source object or the connection pool component.

Examples:

  • loading a specified DriverManager class

  • initialize a java DataSource object

  • initialize a connection pool object

Here's what the JakartaConnectionProvider class does:


  /**
   *  Initialize the Jakarta Commons connection pool.
   *
   * @throws Exception if any error occurs
   */
  protected void init() throws Exception 
  {  
    dataSource = new BasicDataSource();    
    dataSource.setDriverClassName(prefs.getJdbcDriver());
    dataSource.setUrl(prefs.getJdbcURL());
    dataSource.setUsername(prefs.getUser());
    dataSource.setPassword(prefs.getPassword());
    dataSource.setValidationQuery(null);
    dataSource.setMaxActive(20);
    dataSource.setMaxIdle(5);
    dataSource.setMaxWait(-1);
  }
      

20.3.2. The getConnection method

Simply, it must return an initialized Connection object.

Example of the JakartaConnectionProvider class:

  /**
   *  Get a JDBC Connection
   *
   * @return  a JDBC Connection
   *
   * @exception  SQLException Description of the Exception
   */ 
  protected Connection getConnection() throws SQLException 
  {
    return dataSource.getConnection();        
  }