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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀