Skip to main content

Avoid ORM querys at Global Scope

Published 1 min readMarkdown
Avoid ORM querys at Global Scope

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

About the author

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

No comments yet. Be the first to comment.