peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

This MySqlConnection is already in use #1069

Closed BKSoftCraft closed 1 year ago

BKSoftCraft commented 1 year ago

//--------------------------------------------------------index.php-------------------------------------------------------------
<?php
require "dbconn.php";
$flag = 1;
$conn->beginTransaction();
$sdate = "2022-09-15";
$stdate = "2022-09-21";
$total_amt = 100;
// Insert
$i_sql = "INSERT INTO 00test (date,value) VALUES (:date,:value)";
$i_stmt = $conn->prepare($i_sql);
// Update
$u_sql = "UPDATE 00test SET value = value + :amt WHERE date >= :date";
$u_stmt = $conn->prepare($u_sql);

while ($sdate < $stdate) {
    $i_rs = $i_stmt->execute([':date' => $sdate, ':value' => $total_amt]);
    if (!$i_rs) {
        $showError = "Unable to Insert data";
        $flag = 0;
    }
    $sdate = date('Y-m-d', strtotime($sdate . ' +1 day'));
}
$u_rs = $u_stmt->execute([':amt' => $total_amt, ':date' => $stdate]);      // Peachpie.Library.PDO.PDOException: 'This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse'
if (!$u_rs) {
    $showError = "Unable to Update data";
    $flag = 0;
}
if ($flag == 0) {
    $conn->rollBack();
} else {
    if (!$conn->commit()) {
        echo "Commit Fail";
    } else {
        echo "Success";
    }
}
//---------------------------------------------------------------dbconn.php-------------------------------------------------------------
<?php
try {
    $server = "localhost";
    $username = "root";
    $password = "";

    $conn = new PDO("mysql:host=$server;dbname=test1", $username, $password);
    $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (Exception $e) {
    echo "Failed: " . $e->getMessage();
}
BKSoftCraft commented 1 year ago

Peachpie.Library.PDO.PDOException
  HResult=0x80131500
  Message=This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse
  Source=Peachpie.Library.PDO
  StackTrace:
   at Peachpie.Library.PDO.PDO.HandleError(ErrorInfo error)
   at Peachpie.Library.PDO.PDOStatement.HandleError(ErrorInfo error)
   at Peachpie.Library.PDO.PDOStatement.HandleError(Exception exception)
   at Peachpie.Library.PDO.PDOStatement.execute(PhpArray input_parameters)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at <Root>.index_php.<Main>(Context <ctx>, PhpArray <locals>, Object this, RuntimeTypeHandle <self>) in D:\vs\TestWeb\TestWeb\TestWeb\TestWeb\index.php:line 23
   at <Root>.index_php.<Main>`0(Context <ctx>, PhpArray <locals>, Object this, RuntimeTypeHandle <self>)
   at Pchp.Core.Context.ScriptInfo.Evaluate(Context ctx, PhpArray locals, Object this, RuntimeTypeHandle self)
   at Peachpie.AspNetCore.Web.RequestContextCore.ProcessScript(ScriptInfo script, String path_info)
BKSoftCraft commented 1 year ago

this work well in core php but in peachpie it throw error. Error : Peachpie.Library.PDO.PDOException: 'This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse Is any solution for this ?

jakubmisek commented 1 year ago

this work well in core php but in peachpie it throw error. Error : Peachpie.Library.PDO.PDOException: 'This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse Is any solution for this ?

please note, PeachPie is not PHP. It is a new implementation, it does not use anything from "Core" PHP

jakubmisek commented 1 year ago
Peachpie.Library.PDO.PDOException
  HResult=0x80131500
  Message=This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse
  Source=Peachpie.Library.PDO
  StackTrace:
   at Peachpie.Library.PDO.PDO.HandleError(ErrorInfo error)
   at Peachpie.Library.PDO.PDOStatement.HandleError(ErrorInfo error)
   at Peachpie.Library.PDO.PDOStatement.HandleError(Exception exception)
   at Peachpie.Library.PDO.PDOStatement.execute(PhpArray input_parameters)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at <Root>.index_php.<Main>(Context <ctx>, PhpArray <locals>, Object this, RuntimeTypeHandle <self>) in D:\vs\TestWeb\TestWeb\TestWeb\TestWeb\index.php:line 23
   at <Root>.index_php.<Main>`0(Context <ctx>, PhpArray <locals>, Object this, RuntimeTypeHandle <self>)
   at Pchp.Core.Context.ScriptInfo.Evaluate(Context ctx, PhpArray locals, Object this, RuntimeTypeHandle self)
   at Peachpie.AspNetCore.Web.RequestContextCore.ProcessScript(ScriptInfo script, String path_info)

thank you for the stack trace; seems like a bug. To test it, we would need the test database as well.

BKSoftCraft commented 1 year ago

//This is database data which i used for testing
CREATE TABLE `00test` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `value` decimal(20,3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4

// I have same issue before with id #990 but that test work fine.
BKSoftCraft commented 1 year ago

This database file test1.zip

jakubmisek commented 1 year ago

awesome, thanks!

jakubmisek commented 1 year ago

Thanks for the detailed test case. Using two or more pdo statements has been fixed.

Preparing release soon.