HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache

You might not have encountered it yet, but the excellent Spring framework has a pretty useful class, the so-called LazyConnectionDataSourceProxy. Where would you use it?

Let’s say you have a servlet or process that will eventually talk to a database. Typically you’d have something like:

  • Open a database connection
  • Perform a SELECT query
  • Return the result and close the connection

This works fine for small loads but eventually you’ll want to hit a cache to avoid a database hit if necessary.

Under spring/hibernate control this is typically coded as:

1
2
3
4
@Transactional
public void doSomething(){
   dataLayer.createQuery("SELECT ....");
}

Now hibernate does support caching to avoid the database hit, but by the time you’re at the dataLayer call you’ve already gone past the @Transaction. This means that before you even hit the cache, you’ve already talked to the database! What’s there to talk about? Not much — but at the very least somewhere you’d be sending “BEGIN TRANSACTION”. This is database chatter that you can do without.

Enter LazyDataSourceProxy which, as the name implies, makes the datasource behave “lazily”. This is actually a pretty simple bean — instead of talking to the database as soon as Spring sees @Transactional, the bean will add some more logic to your connection handles. In essence, the bean will simply delay the BEGIN TRANSACTION request (or whatever) until you do something useful with the connection — in this case, the SQL query. Therefore for the code snippet pasted above this would result in:

Line 1-2: Do nothing
Line 3: If Hibernate doesn’t hit the cache and somewhere tries to use the connection to actually send the query: Do the @Transactional bits to open a connection etc and proceed as normal; otherwise do nothing.

The best thing about the bean is that it is completely non-invasive – you just bounce off your datasource off the bean and that’s it.

Enough talk. Here’s an example on how to configure it using BoneCP as a connection pool (the same technique applies for any connection pool or datasource provider)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   <bean id="sessionFactory" class="..." autowire="autodetect">
            <!-- Tell hibernate to use our given datasource -->
                <property name="dataSource" ref="dataSource" /> 
 
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">...</prop>
                        ...
                    </props>
                </property>
            </bean>
 
            <!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
            <bean id="dataSource"
                class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
                <property name="targetDataSource">
                    <ref local="mainDataSource" />
                </property>
            </bean>
 
            <!-- BoneCP configuration -->
            <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
                <property name="driverClass" value="com.mysql.jdbc.Driver" />
                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/yourdb" />
                <property name="username" value="root"/>
                <property name="password" value="abcdefgh"/>
                ...
            </bean>

Tags: , , , ,

One Response to “HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache”

  1. Hi there would you mind stating which blog platform you’re working with?
    I’m looking to start my own blog soon but I’m having a difficult time
    deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m
    looking for something unique. P.S Sorry for getting off-topic but
    I had to ask!

Leave a Reply