Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

2010/03/15

Improvement on Hibernate Query Cache

Cache improves performance by transparently storing data such that future requests for that data can be served faster. Hibernate offers a session scope transactional cache which spans the scope of the session. Hibernate also offers a configurable secondary level cache which spans across multiple sessions. Clustered/Distributed Caches can be configured so that it can be accessed across the entire application server cluster.

Hibernate secondary cache offers pretty good entity cache. Entity cache speeds up entity loading by its primary key. For instance:

select c from Cat c where c.id = :id

It supports query cache too, to enable query cache, you'll need to specify the hibernate property:

hibernate.cache.use_query_cache true

Query cache caches Query result sets, so the cache can provide you an answer for the queries like:

select p from Person p where p.sin = :sin

Let's say, the SIN of a person is never gonna change, so cache the above query will be useful.

Hibernate provides 'natural id' based on queries which can effectively solve the above problem. However, there are many other queries that hibernate cannot handle effectively. For instance, if we want to find all contact information (phone numbers only) for a person:

select pn from PhoneNumbers pn where pn.person.id = :pid 


The primary problem for Query caches, is that hibernate will completely invalidated by any modification to the underlying table, even on entities that's totally unrelated to the query. In the above example, any insertion/update/deletion to the telephone table, on any record, will completely wipe out the entire cache for the above query.

There needs to be a 'smart query cache' to solve this problem. Fortunately, this can be done by providing custom query cache, and we can then refreshes them by listening on hibernate 'entity modification' events. The listener will allow us to intercept any insertion/update/deletion to any telephone record, and then we can determine to only refresh the affected query cache.

2008/04/20

10 Ways to use Hibernate Effectively, by Brian Sam Bodden

Brian Sam Bodden on the Seattle NFJS Java Conference. 


- Inheritance, Table per Concrete Class, Table per Class Hierarchy, Table per Subclass
- Identify and use Components in the Mapping
- Dynamic Data Filtering can be enabled in either HBM file or use Annotation
- While generated SQL is good enough in most case, Hibernate does support Customer SQL as well as native SQL with direct JDBC connection. iBATIS alike but you only require to customize the SQL when needed.
- Bulk Operations - Enable JDBC Batching & Be mindful of Session and 2nd Level Cache
- Cache - Session Cache & 2nd Level Cache, Read-Only, Read/Write & Query Cache
- Terracotta - Claims of performance gains in the range of 2-4x when integrating with second level cache
- Hibernate Search - Free text search with Lucene, Annotation driven, luceneQuery = parser.parse("name:beckham or name:galaxy")
- Profiling/Debugging - beware of the Write behind fact of Hibernate. p6spy, Jahia's SQL Profiler, Hibernate Statistics
- Hibernate/Jboss Tools - http://www.hibernate.org/255.html
- Hibernate Tips & Tricks Page: http://www.hibernate.org/118.html

2007/10/12

Hibernate Mapping Cheat Sheet

Here's a nice hibernate mapping cheat sheet. Good reference when programming.

http://ndpsoftware.com/HibernateMappingCheatSheet.html

EHCache Singleton CacheManager

Here's the problem, when we enable the hibernate secondary cache using org.hibernate.cache.EHCacheProvider, and enable ACEGI user cache using

<property name="userCache">
<bean class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
<property name="cache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheName" value="AcegiUserCache">
</property>
</bean>
</property>
</bean>
</property>

They belongs to two different cache manager.

Instead of have to configure 2 different cache manager separately, have to have separate cache invalidating thread etc, we can fall back to the EHCache Singleton CacheManager.

hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider

Now ACEGI will use the same EHCache CacheManager. They can be configured in the same ehcache.xml file. Yup!

Well well... why another J2EE blog? I benefited from other people's technical blogs, and guess what, it's a good idea to contribute some of my works too. Hope it's helpful and useful, to all of your folks.