Saturday, September 15, 2012

Post#10 Txn Management and Error Handling

This was a tricky one. There are several ways of handling Transactions. I chose to use Annotation. It simplifies the coding a lot. With Spring Txn management is more about configuration. You could use a template and manage manually or you could manage through Txn: The challenge was largely how to setup transaction manager.
That led to revisiting the past and doing some cleanup. I decided on using Spring connection manager rather than jndi lookup. There was lot of duplication. I removed some of it as I cleaned the XML config files.

Then it was onto my favourite crib for about a decade. How to manage exceptions.
I had always wanted to manage the exceptions in a centralised fashion. Spring enabled me to do that beautifully. I wrote a Spring interceptor. It helps with instrumentation (basic) and also checks for error flag. If there is an error it would log useful info such as request params. The method arguments etc. all in one place. That way you do not have to write error handling code everywhere.
To centralise error handling I used AspectJ for Spring. Wrote an AOP @Throwafter annotation. It took a lot of time configuring it. But it was worth it.

I could suggest this to other program's too. I think it helps with debugging. I have wrapped up the initial implementation. It will of course evolve as we go along. To make it all work together I also leveraged ThreadLocal to store information.

No comments: