1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.hibernate.nonstop;
18
19 import net.sf.ehcache.constructs.nonstop.NonStopCacheException;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /***
25 * Class that takes care of {@link NonStopCacheException} that happens in hibernate module
26 *
27 * @author Abhishek Sanoujam
28 *
29 */
30 public final class HibernateNonstopCacheExceptionHandler {
31 /***
32 * Property name which set as "true" will throw exceptions on timeout with hibernate
33 */
34 public static final String HIBERNATE_THROW_EXCEPTION_ON_TIMEOUT_PROPERTY = "ehcache.hibernate.propagateNonStopCacheException";
35
36 /***
37 * Property name for logging the stack trace of the nonstop cache exception too. False by default
38 */
39 public static final String HIBERNATE_LOG_EXCEPTION_STACK_TRACE_PROPERTY = "ehcache.hibernate.logNonStopCacheExceptionStackTrace";
40
41 private static final Logger LOGGER = LoggerFactory.getLogger(HibernateNonstopCacheExceptionHandler.class);
42
43 private static final HibernateNonstopCacheExceptionHandler INSTANCE = new HibernateNonstopCacheExceptionHandler();
44
45 /***
46 * private constructor
47 */
48 private HibernateNonstopCacheExceptionHandler() {
49
50 }
51
52 /***
53 * Returns the singleton instance
54 *
55 * @return the singleton instance
56 */
57 public static HibernateNonstopCacheExceptionHandler getInstance() {
58 return INSTANCE;
59 }
60
61 /***
62 * Handle {@link NonStopCacheException}.
63 * If {@link HibernateNonstopCacheExceptionHandler#HIBERNATE_THROW_EXCEPTION_ON_TIMEOUT_PROPERTY} system property is set to true,
64 * rethrows the {@link NonStopCacheException}, otherwise logs the exception. While logging, if
65 * {@link HibernateNonstopCacheExceptionHandler#HIBERNATE_LOG_EXCEPTION_STACK_TRACE_PROPERTY} is set to true, logs the exception stack
66 * trace too, otherwise logs the exception message only
67 *
68 * @param nonStopCacheException
69 */
70 public void handleNonstopCacheException(NonStopCacheException nonStopCacheException) {
71 if (Boolean.getBoolean(HIBERNATE_THROW_EXCEPTION_ON_TIMEOUT_PROPERTY)) {
72 throw nonStopCacheException;
73 } else {
74 if (Boolean.getBoolean(HIBERNATE_LOG_EXCEPTION_STACK_TRACE_PROPERTY)) {
75 LOGGER.debug("Ignoring NonstopCacheException - " + nonStopCacheException.getMessage(), nonStopCacheException);
76 } else {
77 LOGGER.debug("Ignoring NonstopCacheException - " + nonStopCacheException.getMessage());
78 }
79 }
80 }
81
82 }