1
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
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
73 }
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
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 }