impig33
V2EX  ›  PHP

php 操作数据库的连接与释放怎样做比较好?

  •  
  •   impig33 · Jul 25, 2016 · 2286 views
    This topic created in 3669 days ago, the information mentioned may be changed or developed.
    在手册中,每个查询完成后,都会关闭连接。那么当有多个数据库操作的时候这样就导致不停的生成与释放连接,是不是不合理?

    比如,有一个 a.php ,里面有 20 个函数,分别要实例化 20 个对象,每个对象中都有数据库操作。

    那么,有三种方法:
    1.在 20 个对象的每个类中分别获得一个数据库连接,操作完成并关闭。
    2.在 a.php 中获得数据库连接,在每个函数中,把连接传参进去,最终在 a.php 释放。
    3.生成一个静态的数据库连接对象,每个类中调用 ConnectionClass::db

    怎么样比较好,请大家讨论讨论。

    写在这了,有空请水: https://segmentfault.com/q/1010000006059949

    .
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1148 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:06 · PVG 02:06 · LAX 11:06 · JFK 14:06
    ♥ Do have faith in what you're doing.