// AutoTransaction starts transaction for the session and store it
// if not in the AutoTransaction mode, nothing happens
// if old transaction exists, return it
func (xse *XormSessionManager) AutoTransaction(id Identifier, obj interface{}, s Session) error {
	sl := xse.getOrCreateSessionList(id)
	if !sl.IsAutoTransaction() {
		return nil
	}
	db := xse.orm.Master(obj)
	oldTx := sl.getTransaction(db)
	switch {
	case oldTx == s:
		return nil
	case oldTx != nil:
		return errors.NewErrAnotherTx(NormalizeValue(obj))
	}

	err := s.Begin()
	if err != nil {
		return err
	}

	sl.addTransaction(db, s)
	return nil
}
Example #2
0
// AutoTransaction starts transaction for the session and store it
// if not in the AutoTransaction mode, nothing happens
// if old transaction exists, return it
func (xtx *XormTransaction) AutoTransaction(obj interface{}, s Session) error {
	if !xtx.orm.IsAutoTransaction() {
		return nil
	}

	db := xtx.orm.Master(obj)
	oldTx := xtx.getTransaction(db)
	switch {
	case oldTx == s:
		return nil
	case oldTx != nil:
		return errors.NewErrAnotherTx(NormalizeValue(obj))
	}

	err := s.Begin()
	if err != nil {
		return err
	}

	xtx.addTransaction(db, s)
	return nil
}