Spring: SessionFactory Prototype

May 18, 2006

In the spirit of Rod Johnson's naming conventions, I was not ashamed when I decided to name this class SessionFactoryPrototypeFactoryBean. Before I get into details, let me present the problem. Spring provides a factory bean out of the box to establish Hibernate SessionFactory instances, or, should I say instance. In fact, this class isn't much of a factory at all. At its very best, it can create and handoff exactly one instance of a Hibernate SessionFactory. It acts as a transparent proxy to the SessionFactory, creating a new instance in the afterPropertiesSet method and passing all subsequent method calls directly to the underlying SessionFactory. I offer a solution that will allow prototyping of the SessionFactory that this bean manages (saving you all some grief along the way).

So why the pain? Well, a business requirement arose in which we needed to dynamically switch between different instances of a SessionFactory backing the same database schema. There are two prime use cases calling for this functionality. In one case, it might be necessary to connect to a different regional data source depending on credentials or regionality of the user, or perhaps the same user needs to connect to databases of different regions in order to manage or report on them. The second case would be when a single application needs to access different environments in order to migrate data between them.

Let's look at what is wrong with LocalSessionFactoryBean. First off, it hardcodes the return for the isSingleton method to true. This makes configuration as a prototype impossible. Naturally, a factory is a singleton, but in the case of a spring factory bean, the flag indicates how the underlying bean should be managed. So, no matter how the factory is configured, it will always return the same SessionFactory. The second problem is that the SessionFactory is created in the afterPropertiesSet method. As everyone knows, SessionFactory instances take a long time to create and if the connection to the DataSource is slow, it makes matters worse (resulting in an unnecessary slow startup). Finally, because a Hibernate SessionFactory maps 1:1 with a DataSource, it is necessary to be able to notify the factory that the DataSource has changed. You might think that this is straightforward since there is a setDataSource on the LocalSessionFactoryBean. Not so. Since the LocalSessionFactoryBean passes all calls directly to SessionFactory, it is not possible to reconfigure it after initial load.

The solution comes in three parts. First, the return for the isSingleton method is going to be reversed to true (I suppose it could be made configurable, but I will leave that open for discussion and refactoring). The second change is to add a preinitialize flag, which will indicate whether the SessionFactory should be created eagerly, or whether it should be deferred until first use. Finally, the most significant change is to allow the DataSource to be looked up dynamically by providing a spring bean name which might have a factory-method or is otherwise made mutable (within a synchronized block of course).

The code and furthur explanation can be found on my wiki.

There is also a thread on the Spring forums where I announced this solution.

Posted at 01:24 PM in Java | Permalink Icon Permalink | Comment Icon What, no comments?

Life Without BaseCamp

May 07, 2006

...would be pure chaos. In fact, I can't even remember what life was like without it, or how I even managed to stay afloat. BaseCamp, the prototypical Ruby on Rails application, is an extremely agile project-management tool, with features including a message log, todo lists, and a milestone scheduler. About a year ago, I proposed to my wife that we try out BaseCamp for our personal projects and daily tasks. There were a number of initiatives that we wanted to tackle, but we had no good way of planning them, other than scribbling all over a bunch of scrap paper and notebook stationary (which was eventually misplaced). In short, our previous system was haphazard at best. Reflective of the way in which we viewed our situation, we named our free BaseCamp account "two lost souls" and the sole project "...in search of a direction". We were hopeful that we would find one.


Posted at 10:36 PM in Technology | Permalink Icon Permalink | Comment Icon Comments (7)