View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Helper class featuring methods for Hibernate Session handling.
66   * Also provides support for exception translation.
67   *
68   * <p>Used internally by {@link HibernateTransactionManager}.
69   * Can also be used directly in application code.
70   *
71   * @author Juergen Hoeller
72   * @since 3.1
73   * @see HibernateExceptionTranslator
74   * @see HibernateTransactionManager
75   */
76  public abstract class SessionFactoryUtils {
77  
78  	/**
79  	 * Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
80  	 * Returns {@code DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100}
81  	 * to execute Session cleanup before JDBC Connection cleanup, if any.
82  	 * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
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  	 * Bridging between the different ConnectionProvider package location in 4.0-4.2 vs 4.3.
91  	 */
92  	private static final Method getConnectionProviderMethod =
93  			ClassUtils.getMethodIfAvailable(SessionFactoryImplementor.class, "getConnectionProvider");
94  
95  
96  	/**
97  	 * Determine the DataSource of the given SessionFactory.
98  	 * @param sessionFactory the SessionFactory to check
99  	 * @return the DataSource, or {@code null} if none found
100 	 * @see org.hibernate.engine.spi.SessionFactoryImplementor#getConnectionProvider
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 	 * Perform actual closing of the Hibernate Session,
114 	 * catching and logging any cleanup exceptions thrown.
115 	 * @param session the Hibernate Session to close (may be {@code null})
116 	 * @see org.hibernate.Session#close()
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 	 * Convert the given HibernateException to an appropriate exception
134 	 * from the {@code org.springframework.dao} hierarchy.
135 	 * @param ex HibernateException that occurred
136 	 * @return the corresponding DataAccessException instance
137 	 * @see HibernateExceptionTranslator#convertHibernateAccessException
138 	 * @see HibernateTransactionManager#convertHibernateAccessException
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 		// end of JDBCException (subclass) handling
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 		// fallback
218 		return new HibernateSystemException(ex);
219 	}
220 
221 }