天天看點

groovy datasource sql 在grails應用中如何處理事務

特殊的需求,在GRAILS調用GROOVY SQL直接做資料更新、插入和删除,如何控制原子事務?

比較特殊的處理方案(如果有更好的方案,還請賜教)。

場景:SQL是用DATASOURCE建立的。

描述:因為設定是否自動送出是由CONNECTION處理的,但是直接用dataSource來建立,是沒法得到connection的引用,是以必須用如下方式:

        def dbTran  = new groovy.sql.Sql(dataSource)

        def con = dbTran.createConnection()

        def db =  new groovy.sql.Sql(con)

        try{

            con.autoCommit = false

            db.execute("delete from rcbinfo")

            db.insert.........

            con.commit()

        }catch(Exception e){

            con.rollback()

        }finally{

            con.autoCommit = true

        }