org.hibernate.cache.ehcache.internal.StorageAccessImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-ehcache Show documentation
Show all versions of hibernate-ehcache Show documentation
Integration for Ehcache into Hibernate as a second-level caching service
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.cache.ehcache.internal;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.constructs.nonstop.NonStopCacheException;
import net.sf.ehcache.hibernate.nonstop.HibernateNonstopCacheExceptionHandler;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.support.DomainDataStorageAccess;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.jboss.logging.Logger;
/**
* Implementation of StorageAccess for "talking to" Ehcache
*
* @author Steve Ebersole
*/
public class StorageAccessImpl implements DomainDataStorageAccess {
private static final Logger LOG = Logger.getLogger( StorageAccessImpl.class );
private final Cache cache;
@SuppressWarnings("WeakerAccess")
public StorageAccessImpl(Cache cache) {
this.cache = cache;
}
public Cache getCache() {
return cache;
}
@Override
public boolean contains(Object key) {
return getCache().isKeyInCache( key );
}
@Override
public Object getFromCache(Object key, SharedSessionContractImplementor session) {
try {
final Element element = getCache().get( key );
if ( element == null ) {
return null;
}
else {
return element.getObjectValue();
}
}
catch (net.sf.ehcache.CacheException e) {
if ( e instanceof NonStopCacheException ) {
HibernateNonstopCacheExceptionHandler.getInstance()
.handleNonstopCacheException( (NonStopCacheException) e );
return null;
}
else {
throw new CacheException( e );
}
}
}
@Override
public void putIntoCache(Object key, Object value, SharedSessionContractImplementor session) {
try {
final Element element = new Element( key, value );
getCache().put( element );
}
catch (IllegalArgumentException | IllegalStateException e) {
throw new CacheException( e );
}
catch (net.sf.ehcache.CacheException e) {
if ( e instanceof NonStopCacheException ) {
HibernateNonstopCacheExceptionHandler.getInstance()
.handleNonstopCacheException( (NonStopCacheException) e );
}
else {
throw new CacheException( e );
}
}
}
@Override
public void evictData(Object key) {
try {
getCache().remove( key );
}
catch (ClassCastException | IllegalStateException e) {
throw new CacheException( e );
}
catch (net.sf.ehcache.CacheException e) {
if ( e instanceof NonStopCacheException ) {
HibernateNonstopCacheExceptionHandler.getInstance()
.handleNonstopCacheException( (NonStopCacheException) e );
}
else {
throw new CacheException( e );
}
}
}
@Override
public void evictData() {
try {
getCache().removeAll();
}
catch (IllegalStateException e) {
throw new CacheException( e );
}
catch (net.sf.ehcache.CacheException e) {
if ( e instanceof NonStopCacheException ) {
HibernateNonstopCacheExceptionHandler.getInstance()
.handleNonstopCacheException( (NonStopCacheException) e );
}
else {
throw new CacheException( e );
}
}
}
@Override
public void release() {
try {
getCache().getCacheManager().removeCache( getCache().getName() );
}
catch (IllegalStateException e) {
//When Spring and Hibernate are both involved this will happen in normal shutdown operation.
//Do not throw an exception, simply log this one.
LOG.debug( "This can happen if multiple frameworks both try to shutdown ehcache", e );
}
catch (net.sf.ehcache.CacheException e) {
if ( e instanceof NonStopCacheException ) {
HibernateNonstopCacheExceptionHandler.getInstance()
.handleNonstopCacheException( (NonStopCacheException) e );
}
else {
throw new CacheException( e );
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy