Avoid ORM querys at Global Scope

Danny CrastoDanny Crasto
1 min read

Because …

(1) The query runs every time the application comes up

Elastic applications that scale with load, result in an unnecessary DB hits when new instances are added. The effect is multiplicative for each process of your application per pod or container.

(2) The connection associated with instance will be closed

More importantly, using this long lived, stale ORM object will result in an server has gone away error on usage. The default setting CONN_MAX_AGE is 0 where:

0 closes connection / request; None persistent connection; 0 < keep alive in seconds;

Move the query into the respective view or method. You could cache the object too.

References

0
Subscribe to my newsletter

Read articles from Danny Crasto directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Danny Crasto
Danny Crasto

I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae