1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.orm.hibernate4;
18
19 import java.lang.reflect.Method;
20 import javax.sql.DataSource;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.hibernate.HibernateException;
25 import org.hibernate.JDBCException;
26 import org.hibernate.NonUniqueObjectException;
27 import org.hibernate.NonUniqueResultException;
28 import org.hibernate.ObjectDeletedException;
29 import org.hibernate.PersistentObjectException;
30 import org.hibernate.PessimisticLockException;
31 import org.hibernate.PropertyValueException;
32 import org.hibernate.QueryException;
33 import org.hibernate.QueryTimeoutException;
34 import org.hibernate.Session;
35 import org.hibernate.SessionFactory;
36 import org.hibernate.StaleObjectStateException;
37 import org.hibernate.StaleStateException;
38 import org.hibernate.TransientObjectException;
39 import org.hibernate.UnresolvableObjectException;
40 import org.hibernate.WrongClassException;
41 import org.hibernate.dialect.lock.OptimisticEntityLockException;
42 import org.hibernate.dialect.lock.PessimisticEntityLockException;
43 import org.hibernate.engine.spi.SessionFactoryImplementor;
44 import org.hibernate.exception.ConstraintViolationException;
45 import org.hibernate.exception.DataException;
46 import org.hibernate.exception.JDBCConnectionException;
47 import org.hibernate.exception.LockAcquisitionException;
48 import org.hibernate.exception.SQLGrammarException;
49 import org.hibernate.service.spi.Wrapped;
50
51 import org.springframework.dao.CannotAcquireLockException;
52 import org.springframework.dao.DataAccessException;
53 import org.springframework.dao.DataAccessResourceFailureException;
54 import org.springframework.dao.DataIntegrityViolationException;
55 import org.springframework.dao.DuplicateKeyException;
56 import org.springframework.dao.IncorrectResultSizeDataAccessException;
57 import org.springframework.dao.InvalidDataAccessApiUsageException;
58 import org.springframework.dao.InvalidDataAccessResourceUsageException;
59 import org.springframework.dao.PessimisticLockingFailureException;
60 import org.springframework.jdbc.datasource.DataSourceUtils;
61 import org.springframework.util.ClassUtils;
62 import org.springframework.util.ReflectionUtils;
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public abstract class SessionFactoryUtils {
77
78
79
80
81
82
83
84 public static final int SESSION_SYNCHRONIZATION_ORDER =
85 DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100;
86
87 static final Log logger = LogFactory.getLog(SessionFactoryUtils.class);
88
89
90
91
92 private static final Method getConnectionProviderMethod =
93 ClassUtils.getMethodIfAvailable(SessionFactoryImplementor.class, "getConnectionProvider");
94
95
96
97
98
99
100
101
102 public static DataSource getDataSource(SessionFactory sessionFactory) {
103 if (getConnectionProviderMethod != null && sessionFactory instanceof SessionFactoryImplementor) {
104 Wrapped cp = (Wrapped) ReflectionUtils.invokeMethod(getConnectionProviderMethod, sessionFactory);
105 if (cp != null) {
106 return cp.unwrap(DataSource.class);
107 }
108 }
109 return null;
110 }
111
112
113
114
115
116
117
118 public static void closeSession(Session session) {
119 if (session != null) {
120 try {
121 session.close();
122 }
123 catch (HibernateException ex) {
124 logger.debug("Could not close Hibernate Session", ex);
125 }
126 catch (Throwable ex) {
127 logger.debug("Unexpected exception on closing Hibernate Session", ex);
128 }
129 }
130 }
131
132
133
134
135
136
137
138
139
140 public static DataAccessException convertHibernateAccessException(HibernateException ex) {
141 if (ex instanceof JDBCConnectionException) {
142 return new DataAccessResourceFailureException(ex.getMessage(), ex);
143 }
144 if (ex instanceof SQLGrammarException) {
145 SQLGrammarException jdbcEx = (SQLGrammarException) ex;
146 return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
147 }
148 if (ex instanceof QueryTimeoutException) {
149 QueryTimeoutException jdbcEx = (QueryTimeoutException) ex;
150 return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
151 }
152 if (ex instanceof LockAcquisitionException) {
153 LockAcquisitionException jdbcEx = (LockAcquisitionException) ex;
154 return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
155 }
156 if (ex instanceof PessimisticLockException) {
157 PessimisticLockException jdbcEx = (PessimisticLockException) ex;
158 return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
159 }
160 if (ex instanceof ConstraintViolationException) {
161 ConstraintViolationException jdbcEx = (ConstraintViolationException) ex;
162 return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() +
163 "]; constraint [" + jdbcEx.getConstraintName() + "]", ex);
164 }
165 if (ex instanceof DataException) {
166 DataException jdbcEx = (DataException) ex;
167 return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
168 }
169 if (ex instanceof JDBCException) {
170 return new HibernateJdbcException((JDBCException) ex);
171 }
172
173
174 if (ex instanceof QueryException) {
175 return new HibernateQueryException((QueryException) ex);
176 }
177 if (ex instanceof NonUniqueResultException) {
178 return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
179 }
180 if (ex instanceof NonUniqueObjectException) {
181 return new DuplicateKeyException(ex.getMessage(), ex);
182 }
183 if (ex instanceof PropertyValueException) {
184 return new DataIntegrityViolationException(ex.getMessage(), ex);
185 }
186 if (ex instanceof PersistentObjectException) {
187 return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
188 }
189 if (ex instanceof TransientObjectException) {
190 return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
191 }
192 if (ex instanceof ObjectDeletedException) {
193 return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
194 }
195 if (ex instanceof UnresolvableObjectException) {
196 return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex);
197 }
198 if (ex instanceof WrongClassException) {
199 return new HibernateObjectRetrievalFailureException((WrongClassException) ex);
200 }
201 if (ex instanceof StaleObjectStateException) {
202 return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex);
203 }
204 if (ex instanceof StaleStateException) {
205 return new HibernateOptimisticLockingFailureException((StaleStateException) ex);
206 }
207 if (ex instanceof OptimisticEntityLockException) {
208 return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex);
209 }
210 if (ex instanceof PessimisticEntityLockException) {
211 if (ex.getCause() instanceof LockAcquisitionException) {
212 return new CannotAcquireLockException(ex.getMessage(), ex.getCause());
213 }
214 return new PessimisticLockingFailureException(ex.getMessage(), ex);
215 }
216
217
218 return new HibernateSystemException(ex);
219 }
220
221 }