View Javadoc

    1   /***
    2    * @(#)HibernateServiceImpl.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.persistence;
    26
    27  import net.sf.hibernate.HibernateException;
    28  import net.sf.hibernate.Session;
    29  import net.sf.hibernate.SessionFactory;
    30  import net.sf.hibernate.Transaction;
    31  import net.sf.hibernate.cfg.Configuration;
    32
    33  import org.apache.commons.logging.Log;
    34  import org.apache.commons.logging.LogFactory;
    35
    36  /***
    37   * <p>
    38   * Hibernate persistence service implementation.
    39   * </p>
    40   *
    41   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    42   * @version $Revision: 1.10 $ $Date: 2005/05/22 06:50:29 $
    43   * @version Revision: 1.0
    44   */
    45
    46  public class HibernateServiceImpl implements HibernateService {
    47
    48      private final Log logger = LogFactory.getLog(getClass());
    49
    50      private Session session = null;
    51
    52      private Configuration config;
    53
    54      private Transaction transaction = null;
    55
    56      private SessionFactory sessionFactory = null;
    57
    58      /***
    59       * Default constructor.
    60       */
    61      public HibernateServiceImpl() throws HibernateException {
    62          config = new Configuration();
    63          config.configure(getClass().getClassLoader().getResource(
    64                  "hibernate.cfg.xml"));
    65      }
    66
    67      /***
    68       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#openSession()
    69       */
    70      public Session openSession() throws HibernateException {
    71          if (session == null) {
    72              session = getSessionFactory().openSession();
    73              transaction = session.beginTransaction();
    74          }
    75          return session;
    76      }
    77
    78      /***
    79       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getConfiguration()
    80       */
    81      public Configuration getConfiguration() throws HibernateException {
    82          return config;
    83      }
    84
    85      /***
    86       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getSessionFactory()
    87       */
    88      public synchronized SessionFactory getSessionFactory()
    89              throws HibernateException {
    90          if (sessionFactory == null) {
    91              try {
    92                  sessionFactory = config.buildSessionFactory();
    93              } catch (Throwable t) {
    94                  if (logger.isInfoEnabled()) {
    95                      logger.info("Could not configure hibernate ");
    96                  }
    97                  t.printStackTrace();
    98              }
    99          }
    100         return sessionFactory;
    101     }
    102
    103     /***
    104      * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#closeSession()
    105      */
    106     public void closeSession() throws HibernateException {
    107         if (session != null) {
    108             if (transaction != null && !transaction.wasCommitted()
    109                     && !transaction.wasRolledBack()) {
    110                 transaction.commit();
    111                 transaction = null;
    112             }
    113             session.close();
    114             session = null;
    115         }
    116
    117     }
    118
    119     /***
    120      * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#commit()
    121      */
    122     public void commit() throws HibernateException {
    123         if (session != null && transaction != null
    124                 && !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    125             transaction.commit();
    126             transaction = session.beginTransaction();
    127         }
    128     }
    129
    130     /***
    131      * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#reset()
    132      */
    133     public void reset() {
    134         try {
    135             rollback();
    136             if (session != null) {
    137                 session.clear();
    138                 session.close();
    139             }
    140         } catch (HibernateException ex) {
    141             // we do nothing here, because this shall be called if something
    142             // went wrong.
    143         } finally {
    144             session = null;
    145             transaction = null;
    146         }
    147
    148     }
    149
    150     /***
    151      * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#rollback()
    152      */
    153     public void rollback() throws HibernateException {
    154         if (session != null && transaction != null
    155                 && !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    156             transaction.rollback();
    157             transaction = session.beginTransaction();
    158         }
    159     }
    160 }