Your Question

Do we need call tx.Rollback() after tx.Commit() got error? According to examples in https://gorm.io/docs/transactions.html, it seems like we don't need call tx.Rollback() after tx.Commit() got error?

The document you expected this should be explained

https://gorm.io/docs/transactions.html

Expected answer

I want to know if we need call tx.Rollback() after tx.Commit() got error?

Comment From: codeboytj

Gorm Do we need call tx.Rollback() after tx.Commit() got error?

Just return when error occur according to the example

Comment From: a631807682

~~This should be required~~ Sorry, seems not required, the driver will close the conn. I think we can just use defer tx.Rollback() nomatter commited or error or painc . Because if tx is done, rollback will do nothing. https://pkg.go.dev/database/sql#example-Tx.Prepare https://github.com/golang/go/blob/master/src/database/sql/sql.go#L2305 https://github.com/go-sql-driver/mysql/blob/master/transaction.go#L20

Comment From: jinzhu

Suggest to just use the Transaction method