Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ext/mysqli/mysqli_mysqlnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@
#define mysqli_stmt_close(c, implicit) mysqlnd_stmt_close((c), (implicit))
#define mysqli_free_result(r, implicit) mysqlnd_free_result((r), (implicit))
#define mysqli_async_query(c, q, l) mysqlnd_async_query((c), (q), (l))
#define mysqli_change_user_silent(c, u, p, d, p_len) mysqlnd_change_user_ex((c), (u), (p), (d), true, (size_t)(p_len))

#endif
8 changes: 1 addition & 7 deletions ext/mysqli/mysqli_nonapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,8 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, bool is_real_connect, b
mysql->mysql = zend_ptr_stack_pop(&plist->free_links);

MyG(num_inactive_persistent)--;
/* reset variables */

#ifndef MYSQLI_NO_CHANGE_USER_ON_PCONNECT
if (!mysqli_change_user_silent(mysql->mysql, username, passwd, dbname, passwd_len)) {
#else
if (!mysql_ping(mysql->mysql)) {
#endif
mysqlnd_restart_psession(mysql->mysql);
if (!mysqlnd_restart_psession(mysql->mysql)) {
MyG(num_active_persistent)++;

/* clear error */
Expand Down
61 changes: 61 additions & 0 deletions ext/mysqli/tests/mysqli_pconnect_reset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
mysqli_pconnect() - COM_RESET_CONNECTION clears session state
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
require_once 'connect.inc';

$host = 'p:' . $host;

$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
if (!$link) {
printf("[001] Cannot connect\n");
}

mysqli_query($link, "SET @test_var = 42");
mysqli_query($link, "CREATE TEMPORARY TABLE test_reset_tmp (id INT)");
mysqli_query($link, "INSERT INTO test_reset_tmp VALUES (1)");

$res = mysqli_query($link, "SELECT @test_var AS v");
$row = mysqli_fetch_assoc($res);
if ($row['v'] !== '42') {
printf("[002] Expected 42, got %s\n", $row['v']);
}
mysqli_free_result($res);

$thread_id = mysqli_thread_id($link);
mysqli_close($link);

$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
if (!$link) {
printf("[003] Cannot reconnect\n");
}

if (mysqli_thread_id($link) !== $thread_id) {
printf("[004] Got a different connection, persistent reuse did not happen\n");
}

$res = mysqli_query($link, "SELECT @test_var AS v");
$row = mysqli_fetch_assoc($res);
if ($row['v'] !== null) {
printf("[005] Expected NULL after reset, got %s\n", $row['v']);
}
mysqli_free_result($res);

$res = mysqli_query($link, "SHOW TABLES LIKE 'test_reset_tmp'");
if (mysqli_num_rows($res) !== 0) {
printf("[006] Temporary table should not exist after reset\n");
}
mysqli_free_result($res);

mysqli_close($link);

echo "done!";
?>
--EXPECT--
done!
28 changes: 28 additions & 0 deletions ext/mysqlnd/mysqlnd_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ MYSQLND_METHOD(mysqlnd_command, ping)(MYSQLND_CONN_DATA * const conn)
/* }}} */


/* {{{ mysqlnd_command::reset_connection */
static enum_func_status
MYSQLND_METHOD(mysqlnd_command, reset_connection)(MYSQLND_CONN_DATA * const conn)
{
const func_mysqlnd_protocol_payload_decoder_factory__send_command send_command = conn->payload_decoder_factory->m.send_command;
const func_mysqlnd_protocol_payload_decoder_factory__send_command_handle_response send_command_handle_response = conn->payload_decoder_factory->m.send_command_handle_response;
enum_func_status ret = FAIL;

DBG_ENTER("mysqlnd_command::reset_connection");

ret = send_command(conn->payload_decoder_factory, COM_RESET_CONNECTION, NULL, 0, FALSE,
&conn->state,
conn->error_info,
conn->upsert_status,
conn->stats,
conn->m->send_close,
conn);
if (PASS == ret) {
ret = send_command_handle_response(conn->payload_decoder_factory, PROT_OK_PACKET, FALSE, COM_RESET_CONNECTION, FALSE,
conn->error_info, conn->upsert_status, &conn->last_message);
}

DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_command::statistics */
static enum_func_status
MYSQLND_METHOD(mysqlnd_command, statistics)(MYSQLND_CONN_DATA * const conn, zend_string ** message)
Expand Down Expand Up @@ -659,4 +686,5 @@ MYSQLND_CLASS_METHODS_START(mysqlnd_command)
MYSQLND_METHOD(mysqlnd_command, stmt_close),
MYSQLND_METHOD(mysqlnd_command, enable_ssl),
MYSQLND_METHOD(mysqlnd_command, handshake),
MYSQLND_METHOD(mysqlnd_command, reset_connection),
MYSQLND_CLASS_METHODS_END;
15 changes: 10 additions & 5 deletions ext/mysqlnd/mysqlnd_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,19 @@ MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const c


/* {{{ mysqlnd_conn_data::restart_psession */
static void
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::restart_psession");
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
conn->current_result = NULL;
conn->last_message.s = NULL;
DBG_VOID_RETURN;

enum_func_status ret = conn->command->reset_connection(conn);
if (ret == PASS) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
conn->current_result = NULL;
conn->last_message.s = NULL;
}

DBG_RETURN(ret);
}
/* }}} */

Expand Down
4 changes: 3 additions & 1 deletion ext/mysqlnd/mysqlnd_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ typedef enum_func_status (*func_mysqlnd_execute_com_set_option)(MYSQLND_CONN_DAT
typedef enum_func_status (*func_mysqlnd_execute_com_debug)(MYSQLND_CONN_DATA * const conn);
typedef enum_func_status (*func_mysqlnd_execute_com_init_db)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING db);
typedef enum_func_status (*func_mysqlnd_execute_com_ping)(MYSQLND_CONN_DATA * const conn);
typedef enum_func_status (*func_mysqlnd_execute_com_reset_connection)(MYSQLND_CONN_DATA * const conn);
typedef enum_func_status (*func_mysqlnd_execute_com_statistics)(MYSQLND_CONN_DATA * const conn, zend_string ** message);
typedef enum_func_status (*func_mysqlnd_execute_com_process_kill)(MYSQLND_CONN_DATA * const conn, const unsigned int process_id, const bool read_response);
typedef enum_func_status (*func_mysqlnd_execute_com_refresh)(MYSQLND_CONN_DATA * const conn, const uint8_t options);
Expand Down Expand Up @@ -344,6 +345,7 @@ MYSQLND_CLASS_METHODS_TYPE(mysqlnd_command)
func_mysqlnd_execute_com_stmt_close stmt_close;
func_mysqlnd_execute_com_enable_ssl enable_ssl;
func_mysqlnd_execute_com_handshake handshake;
func_mysqlnd_execute_com_reset_connection reset_connection;
};


Expand Down Expand Up @@ -482,7 +484,7 @@ typedef enum_func_status (*func_mysqlnd_conn_data__free_reference)(MYSQLND_CONN_
typedef enum_func_status (*func_mysqlnd_conn_data__send_command_do_request)(MYSQLND_CONN_DATA * const conn, const enum php_mysqlnd_server_command command, const zend_uchar * const arg, const size_t arg_len, const bool silent, const bool ignore_upsert_status);
typedef enum_func_status (*func_mysqlnd_conn_data__send_command_handle_response)(MYSQLND_CONN_DATA * const conn, const enum mysqlnd_packet_type ok_packet, const bool silent, const enum php_mysqlnd_server_command command, const bool ignore_upsert_status);

typedef void (*func_mysqlnd_conn_data__restart_psession)(MYSQLND_CONN_DATA * conn);
typedef enum_func_status (*func_mysqlnd_conn_data__restart_psession)(MYSQLND_CONN_DATA * conn);
typedef void (*func_mysqlnd_conn_data__end_psession)(MYSQLND_CONN_DATA * conn);
typedef enum_func_status (*func_mysqlnd_conn_data__send_close)(MYSQLND_CONN_DATA * conn);

Expand Down
11 changes: 6 additions & 5 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,15 @@ static zend_result pdo_mysql_check_liveness(pdo_dbh_t *dbh)
PDO_DBG_ENTER("pdo_mysql_check_liveness");
PDO_DBG_INF_FMT("dbh=%p", dbh);

#ifdef PDO_USE_MYSQLND
if (mysqlnd_restart_psession(H->server)) {
PDO_DBG_RETURN(FAILURE);
}
#else
if (mysql_ping(H->server)) {
PDO_DBG_RETURN(FAILURE);
}
#endif
PDO_DBG_RETURN(SUCCESS);
}
/* }}} */
Expand Down Expand Up @@ -724,11 +730,6 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
pdo_mysql_error(dbh);
goto cleanup;
}
#ifdef PDO_USE_MYSQLND
if (dbh->is_persistent) {
mysqlnd_restart_psession(H->server);
}
#endif

dbh->driver_data = H;

Expand Down
Loading