xugu_tranx.go 694 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package xugu
  2. import "errors"
  3. type xuguTx struct {
  4. tconn *xuguConn
  5. }
  6. func (tx *xuguTx) Commit() error {
  7. tx.tconn.mu.Lock()
  8. defer tx.tconn.mu.Unlock()
  9. if tx.tconn == nil {
  10. return errors.New("Invalid connection")
  11. }
  12. _, err := tx.tconn.exec("commit;", nil)
  13. _, err = tx.tconn.exec("set auto_commit on;", nil)
  14. if err != nil {
  15. return err
  16. }
  17. return nil
  18. }
  19. func (tx *xuguTx) Rollback() error {
  20. tx.tconn.mu.Lock()
  21. defer tx.tconn.mu.Unlock()
  22. if tx.tconn == nil {
  23. return errors.New("Invalid connection")
  24. }
  25. _, err := tx.tconn.exec("rollback;", nil)
  26. if err != nil {
  27. return err
  28. }
  29. _, err = tx.tconn.exec("set auto_commit on;", nil)
  30. if err != nil {
  31. return err
  32. }
  33. return err
  34. }