Example #1
0
// Begin begins a transaction and calls f to complete it .
// If f returns an error and IsNetErr(error) == true it reconnects and calls
// f up to MaxRetries times. If error is of type *mysql.Error it tries rollback
// the transaction.
func (c *Conn) Begin(f func(mysql.Transaction, ...interface{}) error, args ...interface{}) error {
	err := c.connectIfNotConnected()
	if err != nil {
		return err
	}
	nn := 0
	for {
		var tr mysql.Transaction
		if tr, err = c.Raw.Begin(); err == nil {
			if err = f(tr, args...); err == nil {
				return nil
			}
		}
		if c.reconnectIfNetErr(&nn, &err); err != nil {
			if _, ok := err.(*mysql.Error); ok && tr.IsValid() {
				tr.Rollback()
			}
			return err
		}
	}
	panic(nil)
}