Spring jdbc namespace

Posted in java, spring by pedro | Thursday, July 1st, 2010 at 2:51 pm

One of the new feature of Spring Framework 3.0 is more namespaces. Today I want to write about jdbc namespace, which I’m using in last project.

The namespace is really simple, it has two main elements

  • initialize-database – which allow us to initialize database with scripts.
  • embedded-database – which allow us to start embedded database.

initialize-database

So we can use that xml tag to initialize our datasource with SQL scripts. There are some properties we should set:

  • data-source – which is javax.sql.DataSource bean reference. It will be used to connect and execute all the scripts.
  • enabled – if we should do anything so if we are enabled or not. (remember it can be property ${} or even spring EL), by default it is enabled.
  • ignore-failures – if we should ignore when statements end with errors.
    • NONE – do not ignore default value.
    • DROPS – we are ignoring failed DROP statements.
    • ALL – we ignore all failed statements.

And what is more important this tag has zero or more scripts tags. All this tags has location property which behave exactly the same as spring resources. In my application I create schema for production (I don’t use any ORM) and for tests I have the same schema with one more script (test data/fixtures – no more DbUnit problems :) ).

It is soooo simple.

<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:/schema.sql">
</jdbc:script></jdbc:initialize-database>

embedded-database

The second tag, is really helpful when you want to use embedded database. Embedded database is helpful for things like storing configuration or other persistent application stuff.

It allow to simple start embedded database and make them visible as javax.sql.DataSource, ready to be injected to our JDBCTemplete objects. It has just two attributes:

  • id – which value becomes the bean name.
  • type – embedded database type

Actually those types are supported:


        
        
    

Under The Hood

All things starts at JdbcNamespaceHandler class, which register two parsers for elements. There is EmbeddedDatabaseBeanDefinitionParser for embedded-database element and InitializeDatabaseBeanDefinitionParser for initialize-database.

When everything is ok this elements build and configure two beans.

And that’s all.


Leave a Reply

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. During my studies I started work for merlin.pl. The primary language I use is Java but I have also programmed in Python, Ruby and Scala. I worked as a technical solution architect at merlin.pl. infrastructure when we were moving from PL/SQL to J2EE. I engineering a great performance optimized solution that made the application 10 times faster than requirements and 85 times faster as original solution.

Currently, I am working as a Senior Expert at F.Hoffmann-La Roche to help define future roadmap in design and development of Enterprise software at Roche and Genentech and build adoption for new technologies. I'm continuously mentoring new developers, helping them understand how important test driven development is and empowering them to get better at their daily job. I'm involved in many activities which brings new technologies for better and faster development. You can find more details on my LinkedIn profile.

But don’t get me wrong, I am not your typical nerd. I'm a pleasant guy that you can drink a glass of wine with me and talk about a range of topics with. My leisure activities include playing basketball, soccer and listening to music. I try to be pragmatic while staying focused on application performance and tuning with success in my daily work.

My favorite quote from Yoda's and my life’s motto is: Do, or do not. There is no try.