dgraham     2003/07/28 21:54:58

  Modified:    mapper/src/share/org/apache/commons/mapper/jdbc
                        JdbcHelper.java
  Log:
  Added default constructor, get/setDataSource(), and createMapperException().
  The createMapperException() method can be used to generate more
  specific exceptions based on the SQLState and ErrorCode of an
  SQLException.
  
  Revision  Changes    Path
  1.8       +50 -7     
jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcHelper.java
  
  Index: JdbcHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcHelper.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JdbcHelper.java   23 Jul 2003 00:12:28 -0000      1.7
  +++ JdbcHelper.java   29 Jul 2003 04:54:58 -0000      1.8
  @@ -74,7 +74,7 @@
   
   /**
    * JdbcHelper factors out mundane and error prone JDBC coding into easy to use 
  - * helper methods.
  + * helper methods.  This class is thread safe.
    * 
    * @author David Graham
    */
  @@ -113,6 +113,16 @@
       protected DataSource ds = null;
   
       /**
  +     * Default constructor for JdbcHelper.  This is provided mainly to
  +     * allow JavaBean type creation from a factory.  Clients must call
  +     * <code>setDataSource()</code> before using an instance of this class
  +     * created with this constructor.
  +     */
  +    public JdbcHelper() {
  +        super();
  +    }
  +
  +    /**
        * Constructor for JdbcHelper.
        * @param ds The DataSource to get Connections from.
        */
  @@ -157,7 +167,7 @@
   
           } catch (SQLException e) {
               this.rollback(conn);
  -            throw new MapperException(e);
  +            throw this.createMapperException(e);
           } finally {
               this.setAutoCommit(autoCommit, conn);
               this.closeStatement(stmt);
  @@ -194,7 +204,7 @@
       public int executeUpdate(String sql, Object param) throws MapperException {
           return this.executeUpdate(sql, ARRAY_PREPARER, new Object[] { param });
       }
  -    
  +
       /**
        * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
        * statement is executed in it's own transaction that will be committed or 
  @@ -242,7 +252,7 @@
               rows = processor.process(rs);
   
           } catch (SQLException e) {
  -            throw new MapperException(e);
  +            throw this.createMapperException(e);
           } finally {
               this.closeResultSet(rs);
               this.closeStatement(stmt);
  @@ -253,6 +263,22 @@
       }
   
       /**
  +     * When executeQuery() and executeUpdate() catch an SQLException they call
  +     * this factory method to generate a MapperException to throw to the 
  +     * caller.  This can be useful for generating a specific MapperException
  +     * subclass that is more descriptive of the error.  For example, this method
  +     * could examine the SQLState and ErrorCode of the SQLException to determine
  +     * that a unique constraint was violated and throw a 
  +     * UniqueFieldAlreadyExistsException. This implementation always returns 
  +     * a MapperException wrapping the given SQLException.
  +     * @param e The SQLException that caused the query to fail.
  +     * @return A MapperException for the given SQLException.  
  +     */
  +    protected MapperException createMapperException(SQLException e) {
  +        return new MapperException(e);
  +    }
  +
  +    /**
        * Executes the given SELECT SQL query and returns a List of results.
        * @param sql The SQL statement to execute.
        * @param params An array of values to fill the sql '?' markers with.
  @@ -493,6 +519,23 @@
        */
       protected Connection getConnection() throws SQLException {
           return this.ds.getConnection();
  +    }
  +
  +    /**
  +     * Returns the <code>DataSource</code> this helper is using.
  +     */
  +    public DataSource getDataSource() {
  +        return this.ds;
  +    }
  +
  +    /**
  +     * Sets the <code>DataSource</code> this helper will use to get
  +     * database connections from.  This should be called after creating a
  +     * helper with the default constructor.
  +     * @param dataSource The DataSource to use.
  +     */
  +    public void setDataSource(DataSource dataSource) {
  +        this.ds = dataSource;
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to