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:

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)

   <bean id="sessionFactory" class="..." autowire="autodetect">
            <!-- Tell hibernate to use our given datasource -->
                <property name="dataSource" ref="dataSource" /> 
                <property name="hibernateProperties">
                        <prop key="hibernate.dialect">...</prop>
            <!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
            <bean id="dataSource"
                <property name="targetDataSource">
                    <ref local="mainDataSource" />
            <!-- 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://" />
                <property name="username" value="root"/>
                <property name="password" value="abcdefgh"/>

Tags: , , , ,

812 Responses to “HOWTO: Spring LazyConnectionDataSourceProxy + JDBC/Hibernate + cache”

  1. cheap Dior granville replica handbags…

    Excellent, thank you!…

  2. jordan 6 white carmine…

    1FoampositesFkJordan 6 White CarmineEjJordan 11FlLebron 11 For SaleCjCheap Lebron 11BjLebron 11 ShoesAlLebron 11 For SaleAlConcord Low 11sDhConcord 11sAnJordan 6 CarmineAnCarmine 6sBmJordan 6BnJordan Retro 6AkWhite Carmine 6sDmCarmine 6sChJordan 6 Carm…

  3. hermes lindy bags…

    The film is the first of its kind to examine the phenomenon that, over the 50 year period of its development, the songs of the Broadway musical were created almost exclusively by Jewish Americans. These are the popular songs that our nation took to war…

  4. borse prada e prezzi…

    Si vous aimez la mode et une copie des chaussures chrétiennes de Louboutins comme moi, veuillez alors se font une faveur, pour copier et les sacs à main chrétiens de Louboutin de copies. Vous?Une cérémonie à l’accent belge…

  5. home says:


    the Timberland shoes or boots clearance by using smallest amount, brand new arrivals designs, quickly shipping and delivery low-cost timberlands on the market, timberland shoes HOWTO: Spring LazyConnectionDataSourceProxy JDBC/Hibernate cache « jolbox.c…

  6. louis vuitton bags…

    4Jordan 13 infrared 23DqVjordan 13 3mAqU3m 13sDoZjordan 11 legend blueBoWLegend blue 11sCqYLegend blue 11sBpYjordan 11 legend blueGoWjordan 11 legend blueGoXLegend blue 11sDsVLegend blue 11sFrYjordan 11 legend blueDoYLegend blue 11sFrYLegend blue 11BsY…

  7. abercrombie et fitch paris…

    En plus de voir et d’apprendre des m?decins de la NFL, Ellis est impatient de voir les six joueurs de Baylor passent par des exercices ? la moissonneuse-batteuse. Six joueurs de Baylor comprennent joueur de ligne offensive Cyril Richardson, running ba…

  8. timberland women boots black…

    The paper that you use as your toddler’s canvas is also important to consider, not just for artistic purposes, but for strength. Toddlers are heavy handed with glue until they learn more control, so you’ll want to choose a paper that can handle that …

  9. gucci shops in hong kong…

    A significant work containing much information on foreign and international animal welfare laws, as well as US provisions. Includes useful information on the evolution of anti cruelty laws and the role of animal protection organisations and law enforce…

  10. mulberry bags…

    True Berries: These conform with the botanical definition of berries. Hence, true berries….

  11. 3m reflective 13s…

    2ugg saleBpVugg black friday saleDtWugg bootsGoVuggs on sale black fridayCqXuggs on sale for black fridayDqYugg saleDoYcyber monday ugg bootsErZhttp://lananeumeyer.comGrYhttp://lakdawalagroup.comDtXcyber monday deals on ugg bootsFrWLegend blue 11sBqYht…

Leave a Reply