dongyaobo9081 2016-05-24 04:37
浏览 258
已采纳

如何在Golang中切换数据库连接并将其共享给模型? (狂欢+武装)

I want to setup Database sharding like https://github.com/thiagopradi/octopus in Revel + Gorm(or any other ORM in Golang).

I will get db connection string in security filter (which was a custom filter, added after revel's param filter). Now I need to switch db connection in next custom filter called db filter

And also need to implement db connection pool in revel application start up.

  • 写回答

1条回答 默认 最新

  • dswg47377 2016-05-24 08:47
    关注

    First is prepare you DB connection on the init (only call once) :

    db1, err := gorm.Open(dbdriver, dboption) //your main server
    db2, err := gorm.Open(dbdriver, dboption) //your sharding 1
    db3, err := gorm.Open(dbdriver, dboption) //your sharding 2
    

    And on your db filter just choose the between db1, db2, or db3 you want to be used by your model. Thats all.

    Gorm already make the pool for you, so you don't need to take care.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作