checkConnection.go 713 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. fDelay := func() {
  13. time.Sleep(time.Duration(30) * time.Second)
  14. }
  15. switch dbType {
  16. case 0: // oracle
  17. for {
  18. if err := GetEngine().Ping(); err != nil {
  19. InitDbEngine()
  20. }
  21. fDelay()
  22. }
  23. case 1: // mysql
  24. for {
  25. if err := GetMySQLEngine().Ping(); err != nil {
  26. InitMySQLDbEngine()
  27. }
  28. fDelay()
  29. }
  30. case 2: // mongodb
  31. for {
  32. if err := GetMongoDB().Session.Ping(); err != nil {
  33. InitMongoDB()
  34. }
  35. fDelay()
  36. }
  37. }
  38. }