javasqldesign-patternsdata-structuresmiddle-tier

Data structure/Java Technique for managing a list of sequential commands


I'm not sure if something special exists for this use case - but it felt like a case where someone was likely to have made some sort of useful structure/technique/design-pattern.

My Situation

My Question

So, I need to build a chain of commands that get executed sequentially, and I need to trigger a rollback if any of them fail. I'd like to do this in the most clear, documented way possible.

Does anyone know a standard way of coding this? I'm sure anyone migrating from stored procedure code to middle tier code has done this before and I don't want to reinvent the wheel if there are good options out there.


Additional Information

One of my main concerns is making everything clear. To elaborate, I'll have a set of queries specifically designed to:

The same logic will apply to tables B, C, D, etc. Unfortunately, it can be the case where just A and C need an extra step, like syncing deletes to a certain derived table, to be done after the deletions but before the upserts.

I'd obviously like to group all the logic for updating a table, and I'd like to group all the logic for updating a derived table as well, but at execution time they have to be intelligently interleaved and this sounds messy to me.


Solution

  • Don't write such a thing yourself. This is what JTA was born for.

    You can use either JPA or Spring to do it.

    Annotate the unit of work as transactional and let the database and JDBC handle it.

    If you must do it yourself, follow the aspect-oriented approach and make it a decorative "before & after" implementation.