开发者

Why doesn't groovy withTransaction rollback on SqlException?

Using the following

import groovy.sql.Sql
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/eventDb", "user",
        "pwd", "com.mysql.jdbc.Driver")

def sqlInsert = "INSERT INTO GTEST (EVENTID,TSTAMP,USER_ID,USER_FIRST_NAME) VALUES (?,?,?,?)"
def sqlParams = ['EVENTID':0, 'TSTAMP':'','USER_ID':'janew','USER_FIRST_NAME':'janewithaverylongnamesothatitdoesntfitwell']
sqlParams['TSTAMP'] = new Date()

sql.withTransaction {stmt ->
    def eventId = sql.executeInsert("INSERT INTO EVENTS (LOGID,TSTAMP) VALUES (2,CURRENT_TIMESTAMP)")
    sqlParams['EVENTID'] = eventId [0][0]
    sql.executeInsert(sqlInsert, sqlParams.values().开发者_JAVA技巧toList())
}

The second execute will fail, however the first insert does not roll back. What am i doing wrong here?


You're not showing the definition of the tables; maybe those are MyISAM, and therefore do not support transactions?


Please see my answer to this question here:

https://stackoverflow.com/a/16863373/1216686

Not an exact match to your example but there may be something in there for you or someone else. I know this is an old post but it seems to be a common problem among Groovy Sql object users.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜