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: , , , ,

16 Responses 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!

  2. Ahaa, its fastidious dialogue concerning this post at this place at this
    weblog, I have read all that, so now me also commenting at this place.

  3. casino spill says:

    Wow that was odd. I just wrote an incredibly long comment
    but after I clicked submit my comment didn’t show up. Grrrr…
    well I’m not writing all that over again. Anyway,
    just wanted to say superb blog!

    my blog post … casino spill

  4. When you are done, send the duly accomplished form and wait for the result overnight.

    At that scenario you are usually launched with cheap payday loan products.
    One payday mortgage can start a period that will keep you in a pin that continues obtaining deeper
    and deeper every time you try to drill down yourself out of
    it.

    Feel free to visit my homepage … payday loans no credit checks

  5. WOW just what I was looking for. Came here by searching for cheap instagram
    likes uk

    My blog post get 500 instagram likes

  6. Hi! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us useful information to work on. You have done a marvellous job!

    Feel free to visit my site: 5000 photo Likes

  7. toronto long term disability lawyer…

    HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache « jolbox.com…

  8. jordan retro 5…

    jordan spizike…

  9. Jordan Scarpe Online…

    UGG Discount…

  10. ugg boots sale…

    UGGs Outlet…

  11. Maybell says:

    Tremendous things here. I am very satisfied to peer your article.
    Thank you so much and I’m looking ahead to contact
    you. Will you please drop me a mail?

    Feel free to surf to my web-site: spilleautomater gratis (Maybell)

  12. Hi, Neat post. There is a problem along with your website in web explorer, might check this?
    IE nonetheless is the market leader and a good portion of other people will miss your great writing
    due to this problem.

  13. y3.me.uk says:

    y3.me.uk…

    HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache « jolbox.com…

  14. nike clearance…

    This really is furthermore that means it is so well liked. The person desires to be around individuals, will be educated, discovers quickly, and wishes to please. Ad pork tenderizers have distinctive lacunas. All the experienced dealer will definitely …

  15. I’m not sure where you are getting your information, but great topic.
    I needs to spend some time learning more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

    Feel free to surf to my blog spilleautomater

  16. china says:

    china…

    I’ve all these china found in red, and I Enjoy Them all! they really are for that reason good, and I any longer ! provide these people with footwear. I bring these products regularly I want it a whole lot!…

Leave a Reply