I'm developing a project and made a class in the Singleton
pattern to connect to the database:
<?
class Conexao extends PDO {
private static $instancia;
public function __construct($dsn, $username = "", $password = ""){
parent::__construct($dsn, $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
}
public function prepare($statement, $options = NULL){
if (!isset($options))
$options = array();
$options += array(
PDO::ATTR_STATEMENT_CLASS => array('Query')
);
return parent::prepare($statement, $options);
}
public static function getInstance(){
if (!isset(self::$instancia)){
try{
self::$instancia = new Conexao("mysql:host=localhost; dbname=meu_banco",
"meu_usuario", "minha_senha");
} catch (Exception $e) {
throw new Exception("Ocorreu um erro ao conectar!<br />".$e->getMessage());
}
}
return self::$instancia;
}
}
class Query extends PDOStatement{
function execute($bound_input_params = NULL): bool{
try{
return parent::execute($bound_input_params);
} catch (PDOException $e){
throw new Exception("
Query: ".$this->queryString."<br />
(".$e->getCode().") ".$e->getMessage()."<br />
");
}
}
}
?>
At some point I had to make a transaction to guarantee the execution of multiple scripts
, so I did the following:
<?
function salvar(){
$this->validar();
$conn = null;
try{
$conn = Conexao::getInstance();
$conn->beginTransaction();
$sql = "";
if ($this->id == 0){
$sql = "
INSERT INTO 'PedidoCompra' ('idFormaPagamento', 'idFornecedor', 'dataPedido')
VALUES (:idFormaPagamento, :idFornecedor, NOW())
";
} else{
$sql = "
UPDATE 'PedidoCompra' SET
'idFormaPagamento' = :idFormaPagamento, 'idFornecedor' = :idFornecedor
WHERE 'idPedidoCompra' = :id
";
}
$query = $conn->prepare($sql);
if ($this->id != 0)
$query->bindValue(":id", $this->id, PDO::PARAM_INT);
$query->bindValue(":idFormaPagamento", $this->formaPagamento->getId(), PDO::PARAM_INT);
$query->bindValue(":idFornecedor", $this->fornecedor->getId(), PDO::PARAM_INT);
$query->execute();
if ($this->id == 0)
$this->id = $conn->lastInsertId();
foreach($this->transacoes as $transacao){
$transacao->setIdPedidoCompra($this->id);
$transacao->salvar();
}
foreach($this->itens as $item){
$item->setIdPedido($this->id);
$item->salvar();
}
$conn->commit();
} catch (Exception $e){
if ($conn != null)
$conn->rollBack();
throw $e;
}
}
?>
The Singleton
pattern prevents me from creating multiple instances of an object, but if two people are connected simultaneously, will the same instance be used for the two connections?
My concern is that I give
rollBack
toscripts
dousuario1
and it also affectsscripts
ofusuario2
.
The Doubt