Wednesday, June 11. 2008Closeup on object encapsulation violation
Do you like Spring and Hibernate ? Personally, I do... but today I faced a strange issue... I tried to make the two incriminated guys auto generate a diff script for my DB schema...
To make it short, I wrote this kind of code :
And the associated conf/data-repository-conf.xml :
Little explanation here : * first it creates the AppContext * then it lookup for the LocalSessionFactoryBean (not the resulting SessionFactory, but its creator, the original Spring FactoryBean) : for that I use the special char "&" at the beginning of the bean's name * the LocalSessionFactoryBean is needed to retrieve the underlying Configuration for Hibernate, and it creates with it a SchemaUpdate object * finally, it runs the update stuff.. This is yet at a nice level of pornography, because of course an equivalent of such a code could be integrated in the bean declaration. But it doesn't deserve my purpose for some reason... whatever, let's go further ! Here is the result at runtime:
Hmmm... I googled this insane stack and found this is an old Spring issue : http://jira.springframework.org/browse/SPR-1547 I'm using Spring 2.5.1 so I shouldn't have the problem... ?? The fact is : I have the problem... After a small investigation, I discovered that the datasource is passed to the Update stuff via a strange ThreadLocal trick, but this trick failed here for some unobvious reasons... To waste no time, and as long as this small program is a one-shot test stuff, I decided to solve the problem with the ugly code following. First we have a look inside the problematic class code to see how to fix that :
And now the fixed code itself - enjoy it:
Huh, believe me or not, it simply works now Small explanation: * get the declared (static) field of the ThreadLocal * make it accessible * get its content (null arg for static access) * put the DataSource in it * here we go !!! Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchCategoriesSyndicate This BlogBlog Administration |