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.
精彩评论