|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| HibernateServiceImpl.java | 37.5% | 45.5% | 75% | 47.4% |
|
||||||||||||||
| 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 | 3 |
public HibernateServiceImpl() throws HibernateException { |
| 62 | 3 |
config = new Configuration();
|
| 63 | 3 |
config.configure(getClass().getClassLoader().getResource( |
| 64 |
"hibernate.cfg.xml"));
|
|
| 65 |
} |
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#openSession()
|
|
| 69 |
*/
|
|
| 70 | 4 |
public Session openSession() throws HibernateException { |
| 71 | 4 |
if (session == null) { |
| 72 | 4 |
session = getSessionFactory().openSession(); |
| 73 | 4 |
transaction = session.beginTransaction(); |
| 74 |
} |
|
| 75 | 0 |
return session;
|
| 76 |
} |
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getConfiguration()
|
|
| 80 |
*/
|
|
| 81 | 1 |
public Configuration getConfiguration() throws HibernateException { |
| 82 | 1 |
return config;
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getSessionFactory()
|
|
| 87 |
*/
|
|
| 88 | 5 |
public synchronized SessionFactory getSessionFactory() |
| 89 |
throws HibernateException {
|
|
| 90 | 5 |
if (sessionFactory == null) { |
| 91 | 2 |
try {
|
| 92 | 2 |
sessionFactory = config.buildSessionFactory(); |
| 93 |
} catch (Throwable t) {
|
|
| 94 | 0 |
if (logger.isInfoEnabled()) {
|
| 95 | 0 |
logger.info("Could not configure hibernate ");
|
| 96 |
} |
|
| 97 | 0 |
t.printStackTrace(); |
| 98 |
} |
|
| 99 |
} |
|
| 100 | 5 |
return sessionFactory;
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#closeSession()
|
|
| 105 |
*/
|
|
| 106 | 4 |
public void closeSession() throws HibernateException { |
| 107 | 4 |
if (session != null) { |
| 108 | 4 |
if (transaction != null && !transaction.wasCommitted() |
| 109 |
&& !transaction.wasRolledBack()) {
|
|
| 110 | 0 |
transaction.commit(); |
| 111 | 0 |
transaction = null;
|
| 112 |
} |
|
| 113 | 4 |
session.close(); |
| 114 | 4 |
session = null;
|
| 115 |
} |
|
| 116 |
|
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
/**
|
|
| 120 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#commit()
|
|
| 121 |
*/
|
|
| 122 | 0 |
public void commit() throws HibernateException { |
| 123 | 0 |
if (session != null && transaction != null |
| 124 |
&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
|
|
| 125 | 0 |
transaction.commit(); |
| 126 | 0 |
transaction = session.beginTransaction(); |
| 127 |
} |
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
/**
|
|
| 131 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#reset()
|
|
| 132 |
*/
|
|
| 133 | 0 |
public void reset() { |
| 134 | 0 |
try {
|
| 135 | 0 |
rollback(); |
| 136 | 0 |
if (session != null) { |
| 137 | 0 |
session.clear(); |
| 138 | 0 |
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 | 0 |
session = null;
|
| 145 | 0 |
transaction = null;
|
| 146 |
} |
|
| 147 |
|
|
| 148 |
} |
|
| 149 |
|
|
| 150 |
/**
|
|
| 151 |
* @see org.huihoo.jfox.soaf.services.persistence.HibernateService#rollback()
|
|
| 152 |
*/
|
|
| 153 | 4 |
public void rollback() throws HibernateException { |
| 154 | 4 |
if (session != null && transaction != null |
| 155 |
&& !transaction.wasCommitted() && !transaction.wasRolledBack()) {
|
|
| 156 | 0 |
transaction.rollback(); |
| 157 | 0 |
transaction = session.beginTransaction(); |
| 158 |
} |
|
| 159 |
} |
|
| 160 |
} |
|
||||||||||