Q:

Writing groovy closure or some pattern to take care of transactions

I want to write some type of closure or method template pattern in groovy that takes care of DB transactions in one place
What i want is some thing like this...
Pseudo code
def txMethod(codeOrSetStatementsToExecute){
    //begin transaction
    execute given - codeOrSetStatementsToExecute
    //end transaction by commit or rollback
}

def createSomething(args){
    txMethod({
        -create1statement(args)
        -create2statement
    });
}

def deleteSomething(){
    txMethod({
        -delete1statement()
        -doSomethingElse
   });
}

A:

I've written something akin to that using JPA, sometime ago. IIRC, it turned into something like this:
class DB<T> {
    void save(T t) {
        transactional {
            em.persist(t)
            em.flush()
        }
    }

    void delete(T t) {
        transactional {
            em.remove(t)
        }
    }

    void update(T t) {
        transactional {
            em.merge(t)
            em.flush()
        }
    }

    protected UserTransaction getTransaction() {
        // get transaction from JPA/Context/Younameit
    }

    protected void transactional(Closure<?> closure) {
        def utx = getTransaction();

        try {
            utx.begin();
            closure.call();
            em.flush();
        } catch (Throwable t) {
            utx.setRollbackOnly()
            throw t;
        } finally {
            utx.commit();
        }
    }
}

