HHH-4660 - Support Cache Retrieve Mode and Cache Store Mode Properties

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18603 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-01-22 01:55:20 +00:00
parent 7acb557f5b
commit 9bb0da4e69
8 changed files with 403 additions and 4 deletions

View File

@ -144,6 +144,10 @@ public abstract class AbstractQueryImpl implements Query {
return this;
}
public CacheMode getCacheMode() {
return cacheMode;
}
public Query setCacheable(boolean cacheable) {
this.cacheable = cacheable;
return this;

View File

@ -1,4 +1,4 @@
// $Id:$
// $Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
@ -33,6 +33,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
@ -66,6 +68,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.AssertionFailure;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
@ -87,6 +90,7 @@ import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.ValueHandlerFactory;
import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
import org.hibernate.ejb.transaction.JoinableCMTTransaction;
import org.hibernate.ejb.util.CacheModeHelper;
import org.hibernate.ejb.util.ConfigurationHelper;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
@ -110,6 +114,8 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
entityManagerSpecificProperties.add( AvailableSettings.LOCK_SCOPE );
entityManagerSpecificProperties.add( AvailableSettings.LOCK_TIMEOUT );
entityManagerSpecificProperties.add( AvailableSettings.FLUSH_MODE );
entityManagerSpecificProperties.add( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
entityManagerSpecificProperties.add( AvailableSettings.SHARED_CACHE_STORE_MODE );
}
private EntityManagerFactoryImpl entityManagerFactory;
@ -152,6 +158,28 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
private void applyProperties() {
getSession().setFlushMode( ConfigurationHelper.getFlushMode( properties.get( AvailableSettings.FLUSH_MODE ) ) );
setLockOptions(this.properties, this.lockOptions);
getSession().setCacheMode(
CacheModeHelper.interpretCacheMode(
currentCacheStoreMode(),
currentCacheRetrieveMode()
)
);
}
private CacheRetrieveMode currentCacheRetrieveMode() {
return determineCacheRetrieveMode( properties );
}
private CacheRetrieveMode determineCacheRetrieveMode(Map<String,Object> settings) {
return (CacheRetrieveMode) settings.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
}
private CacheStoreMode currentCacheStoreMode() {
return determineCacheStoreMode( properties );
}
private CacheStoreMode determineCacheStoreMode(Map<String,Object> settings) {
return (CacheStoreMode) properties.get( AvailableSettings.SHARED_CACHE_STORE_MODE );
}
private void setLockOptions(Map<String, Object> props, LockOptions options) {
@ -203,6 +231,12 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
if ( properties.get( AvailableSettings.LOCK_TIMEOUT ) == null) {
properties.put( AvailableSettings.LOCK_TIMEOUT, LockOptions.WAIT_FOREVER );
}
if ( properties.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) == null) {
properties.put( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheModeHelper.DEFAULT_RETRIEVE_MODE );
}
if ( properties.get( AvailableSettings.SHARED_CACHE_STORE_MODE ) == null) {
properties.put( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheModeHelper.DEFAULT_STORE_MODE );
}
}
public Query createQuery(String jpaqlString) {
@ -484,8 +518,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
CacheMode previousCacheMode = getSession().getCacheMode();
CacheMode cacheMode = determineAppropriateLocalCacheMode( properties );
LockOptions lockOptions = null;
try {
getSession().setCacheMode( cacheMode );
if ( lockModeType != null ) {
return ( A ) getSession().get(
entityClass, ( Serializable ) primaryKey,
@ -515,6 +552,27 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
catch ( HibernateException he ) {
throw convert( he , lockOptions );
}
finally {
getSession().setCacheMode( previousCacheMode );
}
}
public CacheMode determineAppropriateLocalCacheMode(Map<String, Object> localProperties) {
CacheRetrieveMode retrieveMode = null;
CacheStoreMode storeMode = null;
if ( localProperties != null ) {
retrieveMode = determineCacheRetrieveMode( localProperties );
storeMode = determineCacheStoreMode( localProperties );
}
if ( retrieveMode == null ) {
// use the EM setting
retrieveMode = determineCacheRetrieveMode( this.properties );
}
if ( storeMode == null ) {
// use the EM setting
storeMode = determineCacheStoreMode( this.properties );
}
return CacheModeHelper.interpretCacheMode( storeMode, retrieveMode );
}
private void checkTransactionNeeded() {
@ -583,8 +641,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
checkTransactionNeeded();
CacheMode previousCacheMode = getSession().getCacheMode();
CacheMode localCacheMode = determineAppropriateLocalCacheMode( properties );
LockOptions lockOptions = null;
try {
getSession().setCacheMode( localCacheMode );
if ( !getSession().contains( entity ) ) {
throw new IllegalArgumentException( "Entity not managed" );
}
@ -599,6 +660,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
catch ( HibernateException he ) {
throw convert( he, lockOptions);
}
finally {
getSession().setCacheMode( previousCacheMode );
}
}
public boolean contains(Object entity) {

View File

@ -27,6 +27,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import javax.persistence.FlushModeType;
import javax.persistence.Parameter;
import javax.persistence.TransactionRequiredException;
@ -47,6 +49,8 @@ import static org.hibernate.ejb.QueryHints.HINT_FETCH_SIZE;
import static org.hibernate.ejb.QueryHints.HINT_FLUSH_MODE;
import static org.hibernate.ejb.QueryHints.HINT_READONLY;
import static org.hibernate.ejb.QueryHints.HINT_TIMEOUT;
import org.hibernate.ejb.util.CacheModeHelper;
import org.hibernate.ejb.util.ConfigurationHelper;
import org.hibernate.hql.QueryExecutionRequestException;
@ -215,6 +219,34 @@ public abstract class AbstractQueryImpl<X> implements TypedQuery<X> {
else if ( HINT_FLUSH_MODE.equals( hintName ) ) {
applyFlushMode( ConfigurationHelper.getFlushMode( value ) );
}
else if ( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE.equals( hintName ) ) {
final CacheRetrieveMode retrieveMode = (CacheRetrieveMode) value;
CacheStoreMode storeMode = hints != null
? (CacheStoreMode) hints.get( AvailableSettings.SHARED_CACHE_STORE_MODE )
: null;
if ( storeMode == null ) {
storeMode = (CacheStoreMode) entityManager.getProperties()
.get( AvailableSettings.SHARED_CACHE_STORE_MODE );
}
applyCacheMode(
CacheModeHelper.interpretCacheMode( storeMode, retrieveMode )
);
}
else if ( AvailableSettings.SHARED_CACHE_STORE_MODE.equals( hintName ) ) {
final CacheStoreMode storeMode = (CacheStoreMode) value;
CacheRetrieveMode retrieveMode = hints != null
? (CacheRetrieveMode) hints.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE )
: null;
if ( retrieveMode == null ) {
retrieveMode = (CacheRetrieveMode) entityManager.getProperties()
.get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE );
}
applyCacheMode(
CacheModeHelper.interpretCacheMode( storeMode, retrieveMode )
);
}
/* TODO:
else if ( "org.hibernate.lockMode".equals( hintName ) ) {
query.setAliasLockMode( alias, lockMode );

View File

@ -113,14 +113,36 @@ public class AvailableSettings {
* enabled as per the rules defined in JPA 2 section 3.1.7.
* <p/>
* See JPA 2 sections 9.4.3 and 8.2.1.7
* @see javax.persistence.SharedCacheMode
*/
public static final String SHARED_CACHE_MODE = "javax.persistence.sharedCache.mode";
/**
* NOTE : Not a valid EMF property...
* <p/>
* Used to indicate if the provider should attempt to retrieve requested data
* in the shared cache.
*
* @see javax.persistence.CacheRetrieveMode
*/
public static final String SHARED_CACHE_RETRIEVE_MODE ="javax.persistence.cache.retrieveMode";
/**
* NOTE : Not a valid EMF property...
* <p/>
* Used to indicate if the provider should attempt to store data loaded from the database
* in the shared cache.
*
* @see javax.persistence.CacheStoreMode
*/
public static final String SHARED_CACHE_STORE_MODE ="javax.persistence.cache.storeMode";
/**
* Used to indicate what form of automatic validation is in effect as per rules defined
* in JPA 2 section 3.6.1.1
* <p/>
* See JPA 2 sections 9.4.3 and 8.2.1.8
* @see javax.persistence.ValidationMode
*/
public static final String VALIDATION_MODE = "javax.persistence.validation.mode";

View File

@ -28,6 +28,7 @@ import javax.persistence.LockModeType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Selection;
import org.hibernate.CacheMode;
import org.hibernate.HibernateException;
import org.hibernate.StaleStateException;
import org.hibernate.LockOptions;

View File

@ -0,0 +1,100 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.util;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import org.hibernate.CacheMode;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class CacheModeHelper {
public static final CacheMode DEFAULT_LEGACY_MODE = CacheMode.NORMAL;
public static final CacheStoreMode DEFAULT_STORE_MODE = CacheStoreMode.USE;
public static final CacheRetrieveMode DEFAULT_RETRIEVE_MODE = CacheRetrieveMode.USE;
/**
* Given a JPA {@link CacheStoreMode} and {@link CacheRetrieveMode}, determine the corresponding
* legacy Hibernate {@link CacheMode}.
*
* @param storeMode The JPA shared-cache store mode.
* @param retrieveMode The JPA shared-cache retrieve mode.
*
* @return Corresponding {@link CacheMode}.
*/
public static CacheMode interpretCacheMode(CacheStoreMode storeMode, CacheRetrieveMode retrieveMode) {
if ( storeMode == null ) {
storeMode = DEFAULT_STORE_MODE;
}
if ( retrieveMode == null ) {
retrieveMode = DEFAULT_RETRIEVE_MODE;
}
final boolean get = ( CacheRetrieveMode.USE == retrieveMode );
switch ( storeMode ) {
case USE: {
return get ? CacheMode.NORMAL : CacheMode.PUT;
}
case REFRESH: {
// really (get == true) here is a bit of an invalid combo...
return CacheMode.REFRESH;
}
case BYPASS: {
return get ? CacheMode.GET : CacheMode.IGNORE;
}
default: {
throw new IllegalStateException( "huh? :)" );
}
}
}
public static CacheStoreMode interpretCacheStoreMode(CacheMode cacheMode) {
if ( cacheMode == null ) {
cacheMode = DEFAULT_LEGACY_MODE;
}
if ( CacheMode.REFRESH == cacheMode ) {
return CacheStoreMode.REFRESH;
}
if ( CacheMode.NORMAL == cacheMode || CacheMode.PUT == cacheMode ) {
return CacheStoreMode.USE;
}
return CacheStoreMode.BYPASS;
}
public static CacheRetrieveMode interpretCacheRetrieveMode(CacheMode cacheMode) {
if ( cacheMode == null ) {
cacheMode = DEFAULT_LEGACY_MODE;
}
return ( CacheMode.NORMAL == cacheMode || CacheMode.GET == cacheMode )
? CacheRetrieveMode.USE
: CacheRetrieveMode.BYPASS;
}
}

View File

@ -0,0 +1,120 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.test.cacheable.cachemodes;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.HibernateEntityManager;
import org.hibernate.ejb.HibernateQuery;
import org.hibernate.ejb.test.TestCase;
import org.hibernate.impl.AbstractQueryImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class SharedCacheModesTest extends TestCase {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] { SimpleEntity.class };
}
// this is all i can do for now testing-wise since I did not implement @Cacheable first ;)
public void testEntityManagerCacheModes() {
EntityManager em;
Session session;
em = getOrCreateEntityManager();
session = ( (HibernateEntityManager) em ).getSession();
// defaults...
assertEquals( CacheStoreMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheRetrieveMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
assertEquals( CacheMode.NORMAL, session.getCacheMode() );
// overrides...
em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
assertEquals( CacheStoreMode.REFRESH, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.REFRESH, session.getCacheMode() );
em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
assertEquals( CacheStoreMode.BYPASS, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.GET, session.getCacheMode() );
em.setProperty( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS );
assertEquals( CacheRetrieveMode.BYPASS, em.getProperties().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
assertEquals( CacheMode.IGNORE, session.getCacheMode() );
em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
assertEquals( CacheStoreMode.USE, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.PUT, session.getCacheMode() );
em.setProperty( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
assertEquals( CacheStoreMode.REFRESH, em.getProperties().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.REFRESH, session.getCacheMode() );
}
public void testQueryCacheModes() {
EntityManager em = getOrCreateEntityManager();
Query jpaQuery = em.createQuery( "from SimpleEntity" );
AbstractQueryImpl hibQuery = (AbstractQueryImpl) ( (HibernateQuery) jpaQuery ).getHibernateQuery();
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
assertEquals( CacheStoreMode.USE, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.NORMAL, hibQuery.getCacheMode() );
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
assertEquals( CacheStoreMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.GET, hibQuery.getCacheMode() );
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH );
assertEquals( CacheStoreMode.REFRESH, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.REFRESH, hibQuery.getCacheMode() );
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS );
assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
assertEquals( CacheStoreMode.REFRESH, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.REFRESH, hibQuery.getCacheMode() );
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS );
assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
assertEquals( CacheStoreMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.IGNORE, hibQuery.getCacheMode() );
jpaQuery.setHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE );
assertEquals( CacheRetrieveMode.BYPASS, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE ) );
assertEquals( CacheStoreMode.USE, jpaQuery.getHints().get( AvailableSettings.SHARED_CACHE_STORE_MODE ) );
assertEquals( CacheMode.PUT, hibQuery.getCacheMode() );
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.test.cacheable.cachemodes;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
@Entity
public class SimpleEntity {
private Long id;
private String name;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}