Allow temporarily disable default transaction
This commit is contained in:
		
							parent
							
								
									e83e210971
								
							
						
					
					
						commit
						b8692c7671
					
				| @ -5,15 +5,18 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| func BeginTransaction(db *gorm.DB) { | ||||
| 	if !db.Config.SkipDefaultTransaction { | ||||
| 		if tx := db.Begin(); tx.Error == nil { | ||||
| 			db.Statement.ConnPool = tx.Statement.ConnPool | ||||
| 			db.InstanceSet("gorm:started_transaction", true) | ||||
| 		} else { | ||||
| 			tx.Error = nil | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func CommitOrRollbackTransaction(db *gorm.DB) { | ||||
| 	if !db.Config.SkipDefaultTransaction { | ||||
| 		if _, ok := db.InstanceGet("gorm:started_transaction"); ok { | ||||
| 			if db.Error == nil { | ||||
| 				db.Commit() | ||||
| @ -22,4 +25,5 @@ func CommitOrRollbackTransaction(db *gorm.DB) { | ||||
| 			} | ||||
| 			db.Statement.ConnPool = db.ConnPool | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										5
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								gorm.go
									
									
									
									
									
								
							| @ -60,6 +60,7 @@ type Session struct { | ||||
| 	DryRun                 bool | ||||
| 	PrepareStmt            bool | ||||
| 	WithConditions         bool | ||||
| 	SkipDefaultTransaction bool | ||||
| 	Context                context.Context | ||||
| 	Logger                 logger.Interface | ||||
| 	NowFunc                func() time.Time | ||||
| @ -145,6 +146,10 @@ func (db *DB) Session(config *Session) *DB { | ||||
| 		} | ||||
| 	) | ||||
| 
 | ||||
| 	if config.SkipDefaultTransaction { | ||||
| 		tx.Config.SkipDefaultTransaction = true | ||||
| 	} | ||||
| 
 | ||||
| 	if config.Context != nil { | ||||
| 		tx.Statement = tx.Statement.clone() | ||||
| 		tx.Statement.DB = tx | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu