英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论