fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements (#6220)
* test: add nested transaction and prepareStmt coexist test case note: please test in the MySQL environment Change-Id: I0db32adc5f74b0d443e98943d3b182236583b959 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements 1. SavetPoint SQL Statement not support in Prepared Statements e.g. see mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html Change-Id: I082012db9b140e8ec69764c633724665cc802692 Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> * revert(transaction_api): remove savepoint name pool,meaningless Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> --------- Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com> Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com>
This commit is contained in:
		
							parent
							
								
									8197c00def
								
							
						
					
					
						commit
						812bb20c34
					
				| @ -6,8 +6,6 @@ import ( | ||||
| 	"fmt" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
| 	"sync/atomic" | ||||
| 
 | ||||
| 	"gorm.io/gorm/clause" | ||||
| 	"gorm.io/gorm/logger" | ||||
| @ -612,15 +610,6 @@ func (db *DB) Connection(fc func(tx *DB) error) (err error) { | ||||
| 	return fc(tx) | ||||
| } | ||||
| 
 | ||||
| var ( | ||||
| 	savepointIdx      int64 | ||||
| 	savepointNamePool = &sync.Pool{ | ||||
| 		New: func() interface{} { | ||||
| 			return fmt.Sprintf("gorm_%d", atomic.AddInt64(&savepointIdx, 1)) | ||||
| 		}, | ||||
| 	} | ||||
| ) | ||||
| 
 | ||||
| // Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an
 | ||||
| // arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs
 | ||||
| // they are rolled back.
 | ||||
| @ -630,17 +619,14 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er | ||||
| 	if committer, ok := db.Statement.ConnPool.(TxCommitter); ok && committer != nil { | ||||
| 		// nested transaction
 | ||||
| 		if !db.DisableNestedTransaction { | ||||
| 			poolName := savepointNamePool.Get() | ||||
| 			defer savepointNamePool.Put(poolName) | ||||
| 			err = db.SavePoint(poolName.(string)).Error | ||||
| 			err = db.SavePoint(fmt.Sprintf("sp%p", fc)).Error | ||||
| 			if err != nil { | ||||
| 				return | ||||
| 			} | ||||
| 
 | ||||
| 			defer func() { | ||||
| 				// Make sure to rollback when panic, Block error or Commit error
 | ||||
| 				if panicked || err != nil { | ||||
| 					db.RollbackTo(poolName.(string)) | ||||
| 					db.RollbackTo(fmt.Sprintf("sp%p", fc)) | ||||
| 				} | ||||
| 			}() | ||||
| 		} | ||||
| @ -721,7 +707,21 @@ func (db *DB) Rollback() *DB { | ||||
| 
 | ||||
| func (db *DB) SavePoint(name string) *DB { | ||||
| 	if savePointer, ok := db.Dialector.(SavePointerDialectorInterface); ok { | ||||
| 		// close prepared statement, because SavePoint not support prepared statement.
 | ||||
| 		// e.g. mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html
 | ||||
| 		var ( | ||||
| 			preparedStmtTx   *PreparedStmtTX | ||||
| 			isPreparedStmtTx bool | ||||
| 		) | ||||
| 		// close prepared statement, because SavePoint not support prepared statement.
 | ||||
| 		if preparedStmtTx, isPreparedStmtTx = db.Statement.ConnPool.(*PreparedStmtTX); isPreparedStmtTx { | ||||
| 			db.Statement.ConnPool = preparedStmtTx.Tx | ||||
| 		} | ||||
| 		db.AddError(savePointer.SavePoint(db, name)) | ||||
| 		// restore prepared statement
 | ||||
| 		if isPreparedStmtTx { | ||||
| 			db.Statement.ConnPool = preparedStmtTx | ||||
| 		} | ||||
| 	} else { | ||||
| 		db.AddError(ErrUnsupportedDriver) | ||||
| 	} | ||||
| @ -730,7 +730,21 @@ func (db *DB) SavePoint(name string) *DB { | ||||
| 
 | ||||
| func (db *DB) RollbackTo(name string) *DB { | ||||
| 	if savePointer, ok := db.Dialector.(SavePointerDialectorInterface); ok { | ||||
| 		// close prepared statement, because RollbackTo not support prepared statement.
 | ||||
| 		// e.g. mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html
 | ||||
| 		var ( | ||||
| 			preparedStmtTx   *PreparedStmtTX | ||||
| 			isPreparedStmtTx bool | ||||
| 		) | ||||
| 		// close prepared statement, because SavePoint not support prepared statement.
 | ||||
| 		if preparedStmtTx, isPreparedStmtTx = db.Statement.ConnPool.(*PreparedStmtTX); isPreparedStmtTx { | ||||
| 			db.Statement.ConnPool = preparedStmtTx.Tx | ||||
| 		} | ||||
| 		db.AddError(savePointer.RollbackTo(db, name)) | ||||
| 		// restore prepared statement
 | ||||
| 		if isPreparedStmtTx { | ||||
| 			db.Statement.ConnPool = preparedStmtTx | ||||
| 		} | ||||
| 	} else { | ||||
| 		db.AddError(ErrUnsupportedDriver) | ||||
| 	} | ||||
|  | ||||
| @ -57,6 +57,19 @@ func TestTransaction(t *testing.T) { | ||||
| 	if err := DB.First(&User{}, "name = ?", "transaction-2").Error; err != nil { | ||||
| 		t.Fatalf("Should be able to find committed record, but got %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	t.Run("this is test nested transaction and prepareStmt coexist case", func(t *testing.T) { | ||||
| 		// enable prepare statement
 | ||||
| 		tx3 := DB.Session(&gorm.Session{PrepareStmt: true}) | ||||
| 		if err := tx3.Transaction(func(tx4 *gorm.DB) error { | ||||
| 			// nested transaction
 | ||||
| 			return tx4.Transaction(func(tx5 *gorm.DB) error { | ||||
| 				return tx5.First(&User{}, "name = ?", "transaction-2").Error | ||||
| 			}) | ||||
| 		}); err != nil { | ||||
| 			t.Fatalf("prepare statement and nested transcation coexist" + err.Error()) | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestCancelTransaction(t *testing.T) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 wangliuyang
						wangliuyang