隨筆-112  評(píng)論-73  文章-0  trackbacks-0
          groovy sql 事務(wù)
          def init = { servletContext -> Sql.metaClass.withTransaction = { Closure stmt -> cacheConnection(){ connection.autoCommit = false try { stmt().each { execute(it.query, it.params) } connection.commit() } catch ( e) { connection.rollback() throw e } finally { connection.autoCommit = true } } } } def destroy = { } } Then, you may invoke it in a service: def sql = new Sql(dataSource) Connection connection = sql.createConnection(); try{ def sql2 = new Sql(connection); sql2.withTransaction { [ [query:"insert into table values (?,?,?)", params:[p1, p2, p3]], [query:"insert into table2 values (?,?,?)", params:[p4, p5, p6]] ] } }finally{ if(sql.dataSource){ connection.close(); } } There is already a JIRA requesting this to be added to Groovy: http://jira.codehaus.org/browse/GROOVY-3158. Vote it if you find this useful.


          public void ttt(){
          Sql.metaClass.useTransaction = { Closure closure ->
          java.sql.Connection conn = null
          try { conn = createConnection();
          conn.autoCommit = false;
          Sql sql2 = new Sql( conn );
          closure(sql2);
          conn.commit();
          } catch (e) {
          if(conn != null)
          conn.rollback();
          e.printStackTrace();
          throw e
          } finally {
          if ( conn != null ) {
          conn.autoCommit = true
          if ( dataSource ) { conn.close()
          }
          }
          }
          }
          }




          posted on 2009-12-18 19:53 Libo 閱讀(981) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沂水县| 综艺| 江山市| 揭东县| 绥宁县| 大宁县| 浏阳市| 五常市| 皮山县| 黄梅县| 景东| 北辰区| 长治市| 新昌县| 奉贤区| 濮阳县| 海丰县| 吴旗县| 九龙坡区| 双牌县| 济南市| 涞源县| 区。| 尼木县| 商河县| 吉木萨尔县| 昌宁县| 上虞市| 南投县| 安溪县| 霍城县| 靖安县| 马龙县| 淮南市| 普兰县| 彰化市| 法库县| 抚顺县| 新建县| 嵊州市| 漳州市|