Beego:在每个请求之前创建一个新的ORM吗?

huangapple go评论78阅读模式
英文:

Beego: Creating a new orm before every request?

问题

目前,我在每个访问数据库的函数的开头使用以下命令:

o := orm.NewOrm()
o.Using("default") // 使用默认数据库,你也可以使用其他数据库

我感觉我应该只在路由初始化时执行一次。这样做会有安全问题吗?

英文:

Currently I'm using following commands at the beginning of each function that accesses the database.

o := orm.NewOrm()
o.Using("default") // Using default, you can use other database

It feels like I should do that only once at router initialization. Can that be a security issue?

答案1

得分: 4

你已经做得很正确了。如果你正在使用默认命名的数据库,甚至可以省略第二个语句。orm.NewOrm建立了一个新的关系,不一定是一个新的数据库连接。这个关系在控制器中是必要的,这样当发出另一个命令时,你的数据最终可以映射到正确的数据库调用上。据我所见,这并不会带来任何安全问题。

英文:

You are doing it correctly already. If you are using the default named database you can even omit the second statement. orm.NewOrm sets up a new relationship, not necessarily a new database connection. This relationship is necessary in the controller so that your data can be mapped to the correct database calls eventually when another command is issued. As far as I have seen this does not pose any security issues.

huangapple
  • 本文由 发表于 2015年9月29日 19:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/32843160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定