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 java.io.Serializable;
    28  import java.util.Iterator;
    29  import java.util.List;
    30
    31  import net.sf.hibernate.HibernateException;
    32  import net.sf.hibernate.LockMode;
    33  import net.sf.hibernate.Session;
    34  import net.sf.hibernate.SessionFactory;
    35  import net.sf.hibernate.Transaction;
    36  import net.sf.hibernate.cfg.Configuration;
    37  import net.sf.hibernate.type.Type;
    38
    39  import org.apache.commons.logging.Log;
    40  import org.apache.commons.logging.LogFactory;
    41  import org.huihoo.jfox.soaf.exception.DAOException;
    42  import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
    43  import org.picocontainer.Startable;
    44
    45  /***
    46   * <p>
    47   * Hibernate persistence service implementation.
    48   * </p>
    49   *
    50   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    51   * @version $Revision: 1.10 $ $Date: 2005/05/22 06:50:29 $
    52   * @version Revision: 1.0
    53   */
    54
    55  public class HibernateServiceImpl implements HibernateService, Startable {
    56
    57  	private static String HIBERNATE_CONFIG = "hibernate.cfg.xml";
    58
    59  	private final Log logger = LogFactory.getLog(getClass());
    60
    61  	private Session session = null;
    62
    63  	private Configuration config;
    64
    65  	private Transaction transaction = null;
    66
    67  	private SessionFactory sessionFactory = null;
    68
    69  	/***
    70  	 * Default constructor.
    71  	 */
    72  	public HibernateServiceImpl() throws HibernateException {
    73  	}
    74
    75  	/***
    76  	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#openSession()
    77  	 */
    78  	public Session openSession() throws HibernateException {
    79  		if (session == null) {
    80  			session = getSessionFactory().openSession();
    81  			transaction = session.beginTransaction();
    82  		}
    83  		return session;
    84  	}
    85
    86  	/***
    87  	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getConfiguration()
    88  	 */
    89  	public Configuration getConfiguration() throws HibernateException {
    90  		return config;
    91  	}
    92
    93  	/***
    94  	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getSessionFactory()
    95  	 */
    96  	public synchronized SessionFactory getSessionFactory()
    97  			throws HibernateException {
    98  		if (sessionFactory == null) {
    99  			try {
    100 				sessionFactory = config.buildSessionFactory();
    101 			} catch (Throwable t) {
    102 				if (logger.isInfoEnabled()) {
    103 					logger.info("Could not configure hibernate ");
    104 				}
    105 				t.printStackTrace();
    106 			}
    107 		}
    108 		return sessionFactory;
    109 	}
    110
    111 	/***
    112 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#closeSession()
    113 	 */
    114 	public void closeSession() throws HibernateException {
    115 		if (session != null) {
    116 			if (transaction != null && !transaction.wasCommitted()
    117 					&& !transaction.wasRolledBack()) {
    118 				transaction.commit();
    119 				transaction = null;
    120 			}
    121 			session.close();
    122 			session = null;
    123 		}
    124 	}
    125
    126 	/***
    127 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#commit()
    128 	 */
    129 	public void commit() throws HibernateException {
    130 		if (session != null && transaction != null
    131 				&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    132 			transaction.commit();
    133 			transaction = session.beginTransaction();
    134 		}
    135 	}
    136
    137 	/***
    138 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#reset()
    139 	 */
    140 	public void reset() {
    141 		try {
    142 			rollback();
    143 			if (session != null) {
    144 				session.clear();
    145 				session.close();
    146 			}
    147 		} catch (HibernateException ex) {
    148 			// we do nothing here, because this shall be called if something
    149 			// went wrong.
    150 		} finally {
    151 			session = null;
    152 			transaction = null;
    153 		}
    154
    155 	}
    156
    157 	/***
    158 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#rollback()
    159 	 */
    160 	public void rollback() throws HibernateException {
    161 		if (session != null && transaction != null
    162 				&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
    163 			transaction.rollback();
    164 			transaction = session.beginTransaction();
    165 		}
    166 	}
    167
    168 	/***
    169 	 * @see org.picocontainer.Startable#start()
    170 	 */
    171 	public void start() {
    172 		config = new Configuration();
    173 		try {
    174 			config.configure(ResourceHelper.getResourceURL(HIBERNATE_CONFIG));
    175 		} catch (Exception e) {
    176 			logger.error("Failed to start hibernate service " + e.getMessage());
    177 		}
    178 		logger.info("Start hibernate service successful.");
    179 	}
    180
    181 	/***
    182 	 * @see org.picocontainer.Startable#stop()
    183 	 */
    184 	public void stop() {
    185 	}
    186
    187 	/***
    188 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#contains(Object)
    189 	 */
    190 	public boolean contains(Object object) throws HibernateException,
    191 			DAOException {
    192 		boolean isContains = false;
    193 		Session session;
    194 		try {
    195 			session = openSession();
    196 			session.contains(object);
    197 			commit();
    198 		} catch (Exception e) {
    199 			rollback();
    200 			throw new DAOException(e);
    201 		} finally {
    202 			session = null;
    203 			closeSession();
    204 		}
    205 		return isContains;
    206 	}
    207
    208 	/***
    209 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(Object)
    210 	 */
    211 	public void delete(Object object) throws HibernateException, DAOException {
    212 		Session session;
    213 		try {
    214 			session = openSession();
    215 			session.delete(object);
    216 			commit();
    217 		} catch (Exception e) {
    218 			rollback();
    219 			throw new DAOException(e);
    220 		} finally {
    221 			session = null;
    222 			closeSession();
    223 		}
    224 	}
    225
    226 	/***
    227 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(Object, LockMode)
    228 	 */
    229 	public void delete(Object object, LockMode lockMode)
    230 			throws HibernateException, DAOException {
    231 		Session session;
    232 		try {
    233 			session = openSession();
    234 			session.lock(object, lockMode);
    235 			session.delete(object);
    236 			commit();
    237 		} catch (Exception e) {
    238 			rollback();
    239 			throw new HibernateException(e);
    240 		} finally {
    241 			session = null;
    242 			closeSession();
    243 		}
    244 	}
    245
    246 	/***
    247      * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String)
    248 	 */
    249 	public int delete(String query) throws HibernateException, DAOException {
    250 		int instancesCount = 0;
    251 		Session session;
    252 		try {
    253 			session = openSession();
    254 			instancesCount = session.delete(query);
    255 			commit();
    256 		} catch (Exception e) {
    257 			rollback();
    258 			throw new DAOException(e);
    259 		} finally {
    260 			session = null;
    261 			closeSession();
    262 		}
    263 		return instancesCount;
    264 	}
    265
    266 	/***
    267 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String, Object, Type)
    268 	 */
    269 	public int delete(String query, Object value, Type type)
    270 			throws HibernateException, DAOException {
    271 		int instancesCount = 0;
    272 		Session session;
    273 		try {
    274 			session = openSession();
    275 			instancesCount = session.delete(query, value, type);
    276 			commit();
    277 		} catch (Exception e) {
    278 			rollback();
    279 			throw new DAOException(e);
    280 		} finally {
    281 			session = null;
    282 			closeSession();
    283 		}
    284 		return instancesCount;
    285 	}
    286
    287 	/***
    288 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#delete(String, Object[], Type[])
    289 	 */
    290 	public int delete(String query, Object[] values, Type[] types)
    291 			throws HibernateException, DAOException {
    292 		int instancesCount = 0;
    293 		Session session;
    294 		try {
    295 			session = openSession();
    296 			instancesCount = session.delete(query, values, types);
    297 			commit();
    298 		} catch (Exception e) {
    299 			rollback();
    300 			throw new DAOException(e);
    301 		} finally {
    302 			session = null;
    303 			closeSession();
    304 		}
    305 		return instancesCount;
    306 	}
    307
    308 	/***
    309 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#evict(Object)
    310 	 */
    311 	public void evict(Object object) throws HibernateException, DAOException {
    312 		Session session;
    313 		try {
    314 			session = openSession();
    315 			session.evict(object);
    316 			commit();
    317 		} catch (Exception e) {
    318 			rollback();
    319 			throw new DAOException(e);
    320 		} finally {
    321 			session = null;
    322 			closeSession();
    323 		}
    324 	}
    325
    326 	/***
    327 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String)
    328 	 */
    329 	public List find(String query) throws HibernateException, DAOException {
    330 		List list;
    331 		Session session;
    332 		try {
    333 			session = openSession();
    334 			list = session.find(query);
    335 			commit();
    336 		} catch (Exception e) {
    337 			rollback();
    338 			throw new DAOException(e);
    339 		} finally {
    340 			session = null;
    341 			closeSession();
    342 		}
    343 		return list;
    344 	}
    345
    346 	/***
    347 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String, Object, Type)
    348 	 */
    349 	public List find(String query, Object value, Type type)
    350 			throws HibernateException, DAOException {
    351 		List list;
    352 		Session session;
    353 		try {
    354 			session = openSession();
    355 			list = session.find(query, value, type);
    356 			commit();
    357 		} catch (Exception e) {
    358 			rollback();
    359 			throw new DAOException(e);
    360 		} finally {
    361 			session = null;
    362 			closeSession();
    363 		}
    364 		return list;
    365 	}
    366
    367 	/***
    368 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#find(String, Object[], Type[])
    369 	 */
    370 	public List find(String query, Object[] values, Type[] types)
    371 			throws HibernateException, DAOException {
    372 		List list;
    373 		Session session;
    374 		try {
    375 			session = openSession();
    376 			list = session.find(query, values, types);
    377 			commit();
    378 		} catch (Exception e) {
    379 			rollback();
    380 			throw new DAOException(e);
    381 		} finally {
    382 			session = null;
    383 			closeSession();
    384 		}
    385 		return list;
    386 	}
    387
    388 	/***
    389 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#get(Class, Serializable)
    390 	 */
    391 	public Object get(Class clazz, final Serializable id) throws HibernateException,
    392 			DAOException {
    393 		Object object;
    394 		Session session;
    395 		try {
    396 			session = openSession();
    397 			object = session.get(clazz, id);
    398 			commit();
    399 		} catch (Exception e) {
    400 			rollback();
    401 			throw new DAOException(e);
    402 		} finally {
    403 			session = null;
    404 			closeSession();
    405 		}
    406 		return object;
    407 	}
    408
    409 	/***
    410 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#get(Class, Serializable, LockMode)
    411 	 */
    412 	public Object get(Class clazz, final Serializable id, LockMode lockMode)
    413 			throws HibernateException, DAOException {
    414 		Object object;
    415 		Session session;
    416 		try {
    417 			session = openSession();
    418 			object = session.get(clazz, id, lockMode);
    419 			commit();
    420 		} catch (Exception e) {
    421 			rollback();
    422 			throw new DAOException(e);
    423 		} finally {
    424 			session = null;
    425 			closeSession();
    426 		}
    427 		return object;
    428 	}
    429
    430 	/***
    431 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String)
    432 	 */
    433 	public Iterator iterate(String query) throws HibernateException,
    434 			DAOException {
    435 		Iterator iter;
    436 		Session session;
    437 		try {
    438 			session = openSession();
    439 			iter = session.iterate(query);
    440 			commit();
    441 		} catch (Exception e) {
    442 			rollback();
    443 			throw new DAOException(e);
    444 		} finally {
    445 			session = null;
    446 			closeSession();
    447 		}
    448 		return iter;
    449 	}
    450
    451 	/***
    452 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String, Object, Type)
    453 	 */
    454 	public Iterator iterate(String query, Object value, Type type)
    455 			throws HibernateException, DAOException {
    456 		Iterator iter;
    457 		Session session;
    458 		try {
    459 			session = openSession();
    460 			iter = session.iterate(query, value, type);
    461 			commit();
    462 		} catch (Exception e) {
    463 			rollback();
    464 			throw new DAOException(e);
    465 		} finally {
    466 			session = null;
    467 			closeSession();
    468 		}
    469 		return iter;
    470 	}
    471
    472 	/***
    473 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#iterate(String, Object[], Type[])
    474 	 */
    475 	public Iterator iterate(String query, Object[] values, Type[] types)
    476 			throws HibernateException, DAOException {
    477 		Iterator iter;
    478 		Session session;
    479 		try {
    480 			session = openSession();
    481 			iter = session.iterate(query, values, types);
    482 			commit();
    483 		} catch (Exception e) {
    484 			rollback();
    485 			throw new DAOException(e);
    486 		} finally {
    487 			session = null;
    488 			closeSession();
    489 		}
    490 		return iter;
    491 	}
    492
    493 	/***
    494 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class, Serializable)
    495 	 */
    496 	public Object load(Class clazz, final Serializable id) throws HibernateException,
    497 			DAOException {
    498 		Object object;
    499 		Session session;
    500 		try {
    501 			session = openSession();
    502 			object = session.load(clazz, id);
    503 			commit();
    504 		} catch (Exception e) {
    505 			rollback();
    506 			throw new DAOException(e);
    507 		} finally {
    508 			session = null;
    509 			closeSession();
    510 		}
    511 		return object;
    512 	}
    513
    514 	/***
    515 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class, Serializable, LockMode)
    516 	 */
    517 	public Object load(Class clazz, final Serializable id, LockMode lockMode)
    518 			throws HibernateException, DAOException {
    519 		Object object;
    520 		Session session;
    521 		try {
    522 			session = openSession();
    523 			object = session.load(clazz, id, lockMode);
    524 			commit();
    525 		} catch (Exception e) {
    526 			rollback();
    527 			throw new DAOException(e);
    528 		} finally {
    529 			session = null;
    530 			closeSession();
    531 		}
    532 		return object;
    533 	}
    534
    535 	/***
    536 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Object, Serializable)
    537 	 */
    538 	public void load(Object object, final Serializable id) throws HibernateException,
    539 			DAOException {
    540 		Session session;
    541 		try {
    542 			session = openSession();
    543 			session.load(object, id);
    544 			commit();
    545 		} catch (Exception e) {
    546 			rollback();
    547 			throw new DAOException(e);
    548 		} finally {
    549 			session = null;
    550 			closeSession();
    551 		}
    552 	}
    553
    554 	/***
    555 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#load(Class, Serializable, LockMode)
    556 	 */
    557 	public void lock(Object object, LockMode lockMode)
    558 			throws HibernateException, DAOException {
    559 		Session session;
    560 		try {
    561 			session = openSession();
    562 			session.lock(object, lockMode);
    563 			commit();
    564 		} catch (Exception e) {
    565 			rollback();
    566 			throw new DAOException(e);
    567 		} finally {
    568 			session = null;
    569 			closeSession();
    570 		}
    571 	}
    572
    573 	/***
    574 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#update(Object)
    575 	 */
    576 	public void update(Object object) throws HibernateException, DAOException {
    577 		Session session;
    578 		try {
    579 			session = openSession();
    580 			session.update(object);
    581 			commit();
    582 		} catch (Exception e) {
    583 			rollback();
    584 			throw new DAOException(e);
    585 		} finally {
    586 			session = null;
    587 			closeSession();
    588 		}
    589 	}
    590
    591 	/***
    592 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#update(Object, Serializable)
    593 	 */
    594 	public void update(Object object, final Serializable id)
    595 			throws HibernateException, DAOException {
    596 		Session session;
    597 		try {
    598 			session = openSession();
    599 			session.update(object, id);
    600 			commit();
    601 		} catch (Exception e) {
    602 			rollback();
    603 			throw new DAOException(e);
    604 		} finally {
    605 			session = null;
    606 			closeSession();
    607 		}
    608 	}
    609
    610 	/***
    611 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdate(Object)
    612 	 */
    613 	public Object saveOrUpdateCopy(Object object) throws HibernateException,
    614 			DAOException {
    615 		Session session;
    616 		Object obj;
    617 		try {
    618 			session = openSession();
    619 			obj = session.saveOrUpdateCopy(object);
    620 			commit();
    621 		} catch (Exception e) {
    622 			rollback();
    623 			throw new DAOException(e);
    624 		} finally {
    625 			session = null;
    626 			closeSession();
    627 		}
    628 		return obj;
    629 	}
    630
    631 	/***
    632 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdateCopy(Object, Serializable)
    633 	 */
    634 	public Object saveOrUpdateCopy(Object object, final Serializable id)
    635 			throws HibernateException, DAOException {
    636 		Session session;
    637 		Object obj;
    638 		try {
    639 			session = openSession();
    640 			obj = session.saveOrUpdateCopy(object, id);
    641 			commit();
    642 		} catch (Exception e) {
    643 			rollback();
    644 			throw new DAOException(e);
    645 		} finally {
    646 			session = null;
    647 			closeSession();
    648 		}
    649 		return obj;
    650 	}
    651
    652 	/***
    653 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#refresh(Object)
    654 	 */
    655 	public void refresh(Object object) throws HibernateException, DAOException {
    656 		Session session;
    657 		try {
    658 			session = openSession();
    659 			session.refresh(object);
    660 			commit();
    661 		} catch (Exception e) {
    662 			rollback();
    663 			throw new DAOException(e);
    664 		} finally {
    665 			session = null;
    666 			closeSession();
    667 		}
    668 	}
    669
    670 	/***
    671 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#refresh(Object, LockMode)
    672 	 */
    673 	public void refresh(Object object, LockMode lockMode)
    674 			throws HibernateException, DAOException {
    675 		Session session;
    676 		try {
    677 			session = openSession();
    678 			session.refresh(object, lockMode);
    679 			commit();
    680 		} catch (Exception e) {
    681 			rollback();
    682 			throw new DAOException(e);
    683 		} finally {
    684 			session = null;
    685 			closeSession();
    686 		}
    687 	}
    688
    689 	/***
    690 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#save(Object)
    691 	 */
    692 	public Serializable save(Object object) throws HibernateException,
    693 			DAOException {
    694 		Serializable seriaObj;
    695 		Session session;
    696 		try {
    697 			session = openSession();
    698 			seriaObj = session.save(object);
    699 			commit();
    700 		} catch (Exception e) {
    701 			rollback();
    702 			throw new DAOException(e);
    703 		} finally {
    704 			session = null;
    705 			closeSession();
    706 		}
    707 		return seriaObj;
    708 	}
    709
    710 	/***
    711 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#save(Object, Serializable)
    712 	 */
    713 	public void save(Object object, final Serializable id) throws HibernateException,
    714 			DAOException {
    715 		Session session;
    716 		try {
    717 			session = openSession();
    718 			session.save(object, id);
    719 			commit();
    720 		} catch (Exception e) {
    721 			rollback();
    722 			throw new DAOException(e);
    723 		} finally {
    724 			System.out.println("save close session");
    725 			session = null;
    726 			closeSession();
    727 		}
    728 	}
    729
    730 	/***
    731 	 * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#saveOrUpdate(Object)
    732 	 */
    733 	public void saveOrUpdate(Object object) throws HibernateException,
    734 			DAOException {
    735 		Session session;
    736 		try {
    737 			session = openSession();
    738 			session.saveOrUpdate(object);
    739 			commit();
    740 		} catch (Exception e) {
    741 			rollback();
    742 			throw new DAOException(e);
    743 		} finally {
    744 			session = null;
    745 			closeSession();
    746 		}
    747 	}
    748
    749 }