Adding hibernate-memcached to your application
If you are using Maven2 for your application build you can follow the instructions on the MavenRepository page. For those that aren't using Maven2 you'll need to collect a few simple jars for yourself. I am going to assume you already have hibenrnate3 setup before you came here.
You'll need...
Configuring Hibernate
The first thing to do is to tell hibernate which cache provider you'd like to use, and to enable the second level cache.
| hibernate.cache.provider_class | com.googlecode.hibernate.memcached.MemcachedCacheProvider |
Using just the property above you get basic entity caching by default. The hibernate-memcached library supports query caching, but you have to enable query caching separately.
| hibernate.cache.use_query_cache | true |
!Now you can start setting up the hibernate-memcached specific properties. The hibernate-memcached properties are divided into two categories; cache-wide settings and cache-region settings.
Cache Wide Settings
The cache wide settings all have defaults can mostly be overwritten at a cache-region level.
| Property | Default | Description |
| hibernate.memcached.servers | localhost:11211 | Space delimited list of memcached instances in host:port format |
| hibernate.memcached.cacheTimeSeconds | 300 | The default number of seconds items should be cached. Can be overriden at the region level. |
| hibernate.memcached.keyStrategy | HashCodeKeyStrategy | Sets the strategy class to to use for generating cache keys. Must provide a class name that implements KeyStrategy |
| hibernate.memcached.readBufferSize | DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE | The read buffer size for each server connection from this factory |
| hibernate.memcached.operationQueueLength | DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN | Maximum length of the operation queue returned by this connection factory |
| hibernate.memcached.operationTimeout | DefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUT | Default operation timeout in milliseconds |
| hibernate.memcached.hashAlgorithm | HashAlgorithm.NATIVE_HASH | Which hash algorithm to use when adding items to the cache. Prior to hibernate-memcached 1.2 this defaulted to HashAlgorithm.KETAMA_HASH |
| hibernate.memcached.connectionFactory (since 1.2) | DefaultConnectionFactory | The "simple" name of the ConnectionFactory class to use from spymemcached. Must be one of DefaultConnectionFactory, KetamaConnectionFactory, or BinaryConnectionFactory |
| hibernate.memcached.clearSupported | false | Enables support for the MemcachedCache.clear() method for all cache regions. The way clear is implemented for memcached is expensive and adds overhead to all get/set operations. It is not recommended for production use. |
Cache Region Settings
Cache region properties are set by giving your cached data a "region name" in hibernate. You can tune the MemcachedCache instance for your region using the following properties. These properties essentially override the cache-wide properties above. Note: that the square brackets are there to denote the text you need to replace, they are not part of the property name.
| Property | Default | Description |
| hibernate.memcached.YOUR_REGION_NAME.cacheTimeSeconds | none, see hibernate.memcached.cacheTimeSeconds | Set the cache time for this cache region, overriding the cache-wide setting. |
| hibernate.memcached.YOUR_REGION_NAME.keyStrategy | none, see hibernate.memcached.keyStrategy | Overrides the strategy class to to use for generating cache keys in this cache region. Must provide a class name that implements KeyStrategy |
| hibernate.memcached.YOUR_REGION_NAME.clearSupported | none, see hibernate.memcached.clearSupported | Enables clear() operations for this cache region only. Again, the clear operation incurs cost on every get/set operation. |
Due to limitations in the JVM implementation of Enum.hashCode(), it is recommended that no keyed class rely on Enum.hashCode(). Instead, use Enum.name().hashCode instead, as in:
class MyClass { private String mVar1; private MyEnum mVar2; public int hashCode() { int result = mVar1.hashCode(); result = result + 31 * mVar2.name().hashCode(); return result; } }I also needed an slf4j-jdk14 jar to get the implementation to work (I used 1.5.0):
http://repo1.maven.org/maven2/org/slf4j/slf4j-jdk14/
i have the above setup, but i don't think it works. this is what i see, please let me know if i'm missing something.
in a controller i have this: System.out.println(getDao().getUser(5).getFirstName(); //5 is a random id the output is: David
now I manually log into the mysql database and manually update the row and change the firstname for id 5 from David to Chalres.
now i hit the controller and expect that It does not know anything about the change and still printout David but the output is Charles.
any explanation? Thanks
by the way, looking at my memcached log, it clearly accesses the memcached to get the user object: <23 get com.lushelephant.babelous.pojos.BabbleUser?:0:5 >23 sending key com.lushelephant.babelous.pojos.BabbleUser?:0:5 >23 END
does it mean that hibernate is aware of changes to the db table outside of the framework? cannot be right?
I sovled this problem by this configuration. <class name="eg.Cat" .... >
</class>Hi! I'm trying to use Memcached with Hibernate 3 on a project which uses ant. I have already tried every possible combination of versions for the slf4j-version?.jar and the slf4j implementation for my preferred logging framework (log4j) slf4j-log4j12-version?.jar.
I have tried also the slf4j-jdk14, both 1.5 and 1.6 versions, and without it. However I've not been able to get through the tomcat initialization without getting any exception. This is when deploying. My latest is a NoSuchMethod? when trying to create the sessionFactory bean.
java.lang.NoSuchMethodError?: org.hibernate.cache.CacheException?: method <init>(Ljava/lang/String;Ljava/lang/Throwable;)V not found
I've found the exact same situation on a forum, but using EhCache?, and it was a known bug. If this is a known issue, please, let me know, otherwise, if you come with any idea, I will be most grateful. Thanks
I'm sorry, I forget a detail I don't know whether it's important or not. I'm using java 5
Do you recommend setting hibernate.cache.use_minimal_puts=true for this cache since it goes over the network? It is recommended for clustered caches, this isnt quite a clustered cache, but close.
how can i use hibernate-memcached with ant.please give a sample example . i am fade up off trying this
How can I configure hibernate-memcached to handle any memcache server downs?
Lets say if memcache server is down for some reason I want app read data from DB directly.
Is it possible to setup?