Mapping the 2nd-level-cache to multiple datasources

278 views
Skip to first unread message

udoline

unread,
Oct 4, 2011, 2:46:00 PM10/4/11
to Ehcache Spring Annotations, udo.k...@inter-forum.de
Hallo guys,

How is behavior of mapping the 2nd-level-cache to multiple
datasources.
Note, in the real world it doesn't have a global UUID key. I know,
that's the hell.

Is it possible within this project?

Is there any kind to map (@OneToOne) the cache-name to datasource-name
by a given prefix?


[Design: Section Caching]
http://www.solutionhacker.com/architecture/

[Spring: solution dynamic datasource]
http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/

[hibernate shards: subproject multiple datasource within hibernate]
http://www.hibernate.org/subprojects/shards.html

--
So long,
Udo Kühne

Eric Dalquist

unread,
Oct 4, 2011, 2:46:43 PM10/4/11
to ehcache-sprin...@googlegroups.com
This project is in no way related to caching in Hibernate.

udoline

unread,
Oct 4, 2011, 3:50:15 PM10/4/11
to Ehcache Spring Annotations
Hallo Eric,

actually I developed a simple hibernate sample wihin "ehcache-spring-
example-1" an it works very well.

I switched of the 2nd-level cache handling and deligates to the
"ehcache-spring-annotations", see below

<pre>
...
<!-- + Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</
prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.bytecode.provider">javassist</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
<property name="packagesToScan"
value="com.googlecode.ehcache.annotations.examples" />
</bean>

<!-- + HibernateTransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- + annotation HibernateTransactionManager -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-
target-class="false" />
...
</pre>

Eric Dalquist

unread,
Oct 4, 2011, 4:02:11 PM10/4/11
to ehcache-sprin...@googlegroups.com
This is a VERY bad idea. Ehcache-spring-annotations is meant to provide an easy way to cache the results of method invocations. The Hibernate cache SPI is fairly complex and has a lot of extra handling around cache transactionality. If you want to use Ehcache with Hibernate go read: http://www.ehcache.org/documentation/user-guide/hibernate

Using ehcache-spring-annoations as a proxy between hibernate and ehcache just adds overhead, cripples cache isolation behavior and is generally a very very bad idea.

-Eric

udoline

unread,
Oct 4, 2011, 4:34:11 PM10/4/11
to Ehcache Spring Annotations
Hallo Eric,

thx for Your answer about cache annontation solution within hibernate.

--
So long,
Udo Kühne


udoline

unread,
Oct 4, 2011, 5:20:33 PM10/4/11
to Ehcache Spring Annotations
Hallo Eric,

here is the (very very bad idea) solution within hibernate ... ;-)
http://code.google.com/p/ehcache-spring-annotations/issues/detail?id=85

--
So long,
Udo Kühne

David Karlsen

unread,
Oct 4, 2011, 5:30:51 PM10/4/11
to ehcache-sprin...@googlegroups.com
Why not use the native JPA/Hibernate caching (which also has ehcache support)?

2011/10/4 udoline <udo.k...@googlemail.com>



--
--
David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen

udoline

unread,
Oct 4, 2011, 5:53:41 PM10/4/11
to Ehcache Spring Annotations
Hallo David,

sure, it's more transparent for me or developer what or when the cache
is or will be doing.

My main interests are, howto add the rigght hibernate 2nd-level-cache-
instance to the one of the multiple datasources (@OneToOne).

Have you got any solutions?

--
So long,
Udo Kühne

On 4 Okt., 23:30, David Karlsen <davidkarl...@gmail.com> wrote:
> Why not use the native JPA/Hibernate caching (which also has ehcache
> support)?
>
> 2011/10/4 udoline <udo.kue...@googlemail.com>

Eric Dalquist

unread,
Oct 4, 2011, 8:53:44 PM10/4/11
to ehcache-sprin...@googlegroups.com
I think you need to become more familiar with hibernate and the problems around using detached entities. The patch you've provided is a terrible approach to caching hibernate entities. The hibernate 2nd level cache has very specialized handling of what is actually stored in it that is based on very specific information that hibernate knows internally about each entity. Caching hibernate managed entities outside of the hibernate session or 2nd level cache is WILL lead to hard/impossible to fix application bugs. EVERY time you want to use a hibernate managed entity you should get it from the current hibernate session. I'm the primary developer on an OSS enterprise web portal that has deployments with millions of users. We use hibernate extensively and every time any hibernate managed entity is needed a request to the current hibernate session is made. We've never seen any performance issues with this approach. We have had problems when someone decides to cache an entity in the user's session or elsewhere, then at some later point when that entity is presented back to hibernate it results in a very hard to track down error.

If there is specific or special behavior you'd like to get out of hibernate's 2nd level caching please go check with the hibernate forums. Like I said before this project isn't related to hibernate in any way and using ehcache-spring-annoations or any external cache to store hibernate managed objects is a recipe for disaster.

-Eric
Reply all
Reply to author
Forward
0 new messages