checkConnection.go 769 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package db
  2. import (
  3. "runtime"
  4. "time"
  5. )
  6. // StartCheckConnection 自动重连数据库(5S), dbType = 0 - oracle; 1 - mysql; 2 - mongodb
  7. func StartCheckConnection(dbType int) {
  8. // windows 方便调试不做ping检测
  9. if runtime.GOOS == "windows" {
  10. return
  11. }
  12. switch dbType {
  13. case 0: // oracle
  14. for {
  15. if err := GetEngine().Ping(); err != nil {
  16. InitDbEngine()
  17. }
  18. time.Sleep(time.Duration(5000) * time.Millisecond)
  19. }
  20. case 1: // mysql
  21. for {
  22. if err := GetMySQLEngine().Ping(); err != nil {
  23. InitMySQLDbEngine()
  24. }
  25. time.Sleep(time.Duration(5000) * time.Millisecond)
  26. }
  27. case 2: // mongodb
  28. for {
  29. if err := GetMongoDB().Session.Ping(); err != nil {
  30. InitMongoDB()
  31. }
  32. time.Sleep(time.Duration(5000) * time.Millisecond)
  33. }
  34. }
  35. }