1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.hibernate;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.BeanLocatorUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.PropsUtil;
29  import com.liferay.util.CollectionFactory;
30  import com.liferay.util.dao.hibernate.LiferayClassicSession;
31  
32  import java.sql.Connection;
33  import java.sql.SQLException;
34  
35  import java.util.Map;
36  
37  import javax.sql.DataSource;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.hibernate.HibernateException;
43  import org.hibernate.Session;
44  import org.hibernate.SessionFactory;
45  import org.hibernate.dialect.Dialect;
46  import org.hibernate.engine.SessionFactoryImplementor;
47  import org.hibernate.impl.SessionImpl;
48  
49  import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
50  
51  /**
52   * <a href="HibernateUtil.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class HibernateUtil {
58  
59      public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
60  
61      public static final String SPRING_HIBERNATE_DATA_SOURCE =
62          PropsUtil.get(PropsUtil.SPRING_HIBERNATE_DATA_SOURCE);
63  
64      public static final String SPRING_HIBERNATE_SESSION_FACTORY =
65          PropsUtil.get(PropsUtil.SPRING_HIBERNATE_SESSION_FACTORY);
66  
67      public static void closeSession(Session session) {
68          try {
69              if (session != null) {
70  
71                  // Let Spring manage sessions
72  
73                  //session.close();
74              }
75          }
76          catch (HibernateException he) {
77              _log.error(he.getMessage());
78          }
79      }
80  
81      public static Connection getConnection() throws SQLException {
82          return getDataSource().getConnection();
83      }
84  
85      public static String getCountColumnName() {
86          return COUNT_COLUMN_NAME;
87      }
88  
89      public static DataSource getDataSource() {
90          if (_dataSource == null) {
91              _dataSource = (DataSource)BeanLocatorUtil.locate(
92                  SPRING_HIBERNATE_DATA_SOURCE);
93          }
94  
95          return _dataSource;
96      }
97  
98      public static Dialect getDialect() {
99          return getDialect(SPRING_HIBERNATE_SESSION_FACTORY);
100     }
101 
102     public static Dialect getDialect(String sessionFactoryName) {
103         return getSessionFactory(sessionFactoryName).getDialect();
104     }
105 
106     public static SessionFactoryImplementor getSessionFactory() {
107         return getSessionFactory(SPRING_HIBERNATE_SESSION_FACTORY);
108     }
109 
110     public static SessionFactoryImplementor getSessionFactory(
111         String sessionFactoryName) {
112 
113         if (sessionFactoryName.equals(SPRING_HIBERNATE_SESSION_FACTORY)) {
114             if (_sessionFactory == null) {
115                 LocalSessionFactoryBean lsfb =
116                     (LocalSessionFactoryBean)BeanLocatorUtil.locate(
117                         sessionFactoryName);
118 
119                 _sessionFactory = (SessionFactoryImplementor)lsfb.getObject();
120             }
121 
122             return _sessionFactory;
123         }
124         else {
125             SessionFactoryImplementor sessionFactory =
126                 (SessionFactoryImplementor)_sessionFactories.get(
127                     sessionFactoryName);
128 
129             if (sessionFactory == null) {
130                 LocalSessionFactoryBean lsfb =
131                     (LocalSessionFactoryBean)BeanLocatorUtil.locate(
132                         sessionFactoryName);
133 
134                 sessionFactory = (SessionFactoryImplementor)lsfb.getObject();
135 
136                 _sessionFactories.put(sessionFactoryName, sessionFactory);
137             }
138 
139             return sessionFactory;
140         }
141     }
142 
143     public static Dialect getWrappedDialect() {
144         return getWrappedDialect(SPRING_HIBERNATE_SESSION_FACTORY);
145     }
146 
147     public static Dialect getWrappedDialect(String sessionFactoryName) {
148         Dialect dialect = getSessionFactory(sessionFactoryName).getDialect();
149 
150         if (dialect instanceof DynamicDialect) {
151             DynamicDialect dynamicDialect = (DynamicDialect)dialect;
152 
153             return dynamicDialect.getWrappedDialect();
154         }
155         else {
156             return dialect;
157         }
158     }
159 
160     public static Session openSession() throws HibernateException {
161         return openSession(SPRING_HIBERNATE_SESSION_FACTORY);
162     }
163 
164     public static Session openSession(String sessionFactoryName)
165         throws HibernateException {
166 
167         if (Validator.isNull(sessionFactoryName)) {
168             return openSession();
169         }
170 
171         SessionFactoryImplementor sessionFactory =
172             getSessionFactory(sessionFactoryName);
173 
174         return openSession(sessionFactory);
175     }
176 
177     public static Session openSession(SessionFactory sessionFactory)
178         throws HibernateException {
179 
180         if (sessionFactory == null) {
181             return openSession();
182         }
183 
184         // Let Spring manage sessions
185 
186         Session session = sessionFactory.getCurrentSession();
187 
188         if (_log.isDebugEnabled()) {
189             LiferayClassicSession classicSession =
190                 (LiferayClassicSession)session;
191 
192             SessionImpl sessionImpl =
193                 (SessionImpl)classicSession.getHibernateClassicSession();
194 
195             _log.debug(
196                 "Session is using connection release mode " +
197                     sessionImpl.getConnectionReleaseMode());
198         }
199 
200         return session;
201     }
202 
203     public static SystemException processException(Exception e) {
204         if (e instanceof HibernateException) {
205             _log.error("Caught HibernateException");
206         }
207         else {
208             _log.error("Caught unexpected exception " + e.getClass().getName());
209         }
210 
211         _log.error(e, e);
212 
213         return new SystemException(e);
214     }
215 
216     private static Log _log = LogFactory.getLog(HibernateUtil.class);
217 
218     private static DataSource _dataSource;
219     private static SessionFactoryImplementor _sessionFactory;
220     private static Map _sessionFactories = CollectionFactory.getHashMap();
221 
222 }