View Javadoc

    1   /***
    2    * @(#)AbstractJdbcDAO.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.services.dao;
    26
    27  import java.sql.Connection;
    28  import java.sql.ResultSet;
    29  import java.sql.SQLException;
    30  import java.sql.Statement;
    31
    32  import org.apache.commons.dbutils.DbUtils;
    33  import org.huihoo.jfox.soaf.container.ServiceFactory;
    34  import org.huihoo.jfox.soaf.services.jdbc.DataSourceService;
    35
    36  /***
    37   * <p>
    38   * Abstract JDBC DAO Template.
    39   * </p>
    40   *
    41   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    42   * @version $Revision: 1.1 $ $Date: 2005/05/22 06:49:10 $
    43   * @version Revision: 1.0
    44   */
    45
    46  public abstract class AbstractJdbcDAO extends AbstractDAO {
    47
    48      private DataSourceService dsService;
    49
    50      /***
    51       * Default Constructor
    52       */
    53      public AbstractJdbcDAO() {
    54          dsService = (DataSourceService) ServiceFactory.getInstance()
    55                  .getService(DataSourceService.class);
    56      }
    57
    58      /***
    59       * Gets the JDBC Connection from given data source
    60       *
    61       * @param dsName datasource name
    62       * @return A JDBC Connection instance.
    63       */
    64      protected Connection getConnection(String dsName) throws SQLException {
    65          return dsService.getDataSource(dsName).getConnection();
    66      }
    67
    68      /***
    69       * Close a Connection.
    70       */
    71      protected void close(Connection conn) throws SQLException {
    72          DbUtils.close(conn);
    73      }
    74
    75      /***
    76       * Close a ResultSet.
    77       */
    78      protected void close(ResultSet rs) throws SQLException {
    79          DbUtils.close(rs);
    80      }
    81
    82      /***
    83       * Close a Statement.
    84       */
    85      protected void close(Statement stmt) throws SQLException {
    86          DbUtils.close(stmt);
    87      }
    88
    89      /***
    90       * Close a Connection, avoid closing if null and hide any SQLExceptions that
    91       * occur.
    92       */
    93      protected void closeQuietly(Connection conn) {
    94          DbUtils.closeQuietly(conn);
    95      }
    96
    97      /***
    98       * Close a Statement, avoid closing if null and hide any SQLExceptions that
    99       * occur.
    100      */
    101     protected void closeQuietly(Statement stmt) {
    102         DbUtils.closeQuietly(stmt);
    103     }
    104
    105     /***
    106      * Close a ResultSet, avoid closing if null and hide any SQLExceptions that
    107      * occur.
    108      */
    109     protected void closeQuietly(ResultSet rs) {
    110         DbUtils.closeQuietly(rs);
    111     }
    112
    113     /***
    114      * Close a Connection, Statement and ResultSet.
    115      */
    116     protected void close(Connection conn, Statement stmt, ResultSet rs)
    117             throws SQLException {
    118         DbUtils.close(conn);
    119         DbUtils.close(stmt);
    120         DbUtils.close(rs);
    121     }
    122
    123     /***
    124      * Close a Connection, Statement and ResultSet.
    125      */
    126     protected void closeQuietly(Connection conn, Statement stmt, ResultSet rs) {
    127         DbUtils.closeQuietly(conn, stmt, rs);
    128     }
    129
    130 }