php阻止每个请求中的数据库连接

时间:2015-10-03 14:39:24

标签: php

我在连接类中有一个方法,它返回一个dbconnection实例,所以我可以用它进行查询。

    $dbObject = Connection::getConnection();
    $request = $dbObject->dbh->prepare($sql);
    $request->bindParam(':username', $username, PDO::PARAM_STR);

首次请求时,需要连接,所以我进入这个连接功能就可以了。但为什么我需要再次连接,即使有这么明显我将使用相同的连接..它似乎导致性能问题..

这是我的功能,它使真正的工作。我使用dbObject在db上进行查询,但是每次我需要连接实例(getconnection)来执行它。

DocumentStore store = new DocumentStore("Server=127.0.0.1;Port=5432;User Id=store_user;password=my super secret password;database=store;");

//create the object
var myAudi = new Car {
    Id = Guid.NewGuid(),
    Make = "Audi",
    Model = "A8",
    ImageUrl = "http://some_image_url",
    NumberPlate = "ABC029"
};

//save the object to the document store
using (var session = store.OpenSession())
{
    session.Store<Car>(myAudi);
    session.SaveChanges();
}

是否可以为同一用户的下一个请求保存现有连接或以其他方式传递连接操作

1 个答案:

答案 0 :(得分:0)

您已经使用了单例模式。这仅适用于一个请求中的多个连接。对于每个请求,此类都会创建连接类的新对象。 PDO实例无法序列化或反序列化或保存在会话中。我认为现在没有办法做到这一点。