SyntaxHighlighter

Saturday, September 27, 2008

Spring-Quartz Transactions

Its been after a long time I got chance to sit back in summarize....Past 3 months were hectic in terms of work n there was always a race on ,against time.
Recently I came across a excellent feature of Spring framework,Just wanted to share with you all.
You must be aware of scheduling framework Quartz.
Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application.
I was working on a spring application ,all my business services are spring beans ,my transaction manager is "org.springframework.jdbc.datasource.DataSourceTransactionManager" which is local transaction manager,as my application is not in demand of JTA or any distributed transaction.
Now comes a trick ,when I come across following scenario
Suppose I have a business service which does following
  1. Insert a record in DB
  2. Schedules a job in Quartz
  3. Updates some record in DB
Now what I required is
  • If update in step 3 fails ,I want to rollback steps 1,2
  • If Step 2 fails ,I want step 1 to rollback
As mine datasource is a Local transaction manager,It cant work across multiple resources and manage.That was enforcing me to use JTA or similar transaction manager ,which is actually overkill for my application.
As expcted ,Solution to this problem is provided by none other than spring,Thanks to Juergen Hoeller.
Spring comes with
org.springframework.scheduling.quartz.LocalDataSourceJobStore which is extension to Quartz JobStoreCMT
Now you just need to cnfigure your job store as below
org.quartz.jobStore.class =
org.springframework.scheduling.quartz.LocalDataSourceJobStore
Now ,Operations performed by this JobStore will properly participate in any kind of Spring-managed transaction, as it uses Spring's DataSourceUtils connection handling methods that are aware of a current transaction.
How easily spring solves this problem shows ,springs capability to be an enterprise application framework......