public inbox for [email protected]  
help / color / mirror / Atom feed
during call SQLFreeHandle, the connection is broken. client can't be notified.
2+ messages / 2 participants
[nested] [flat]

* during call SQLFreeHandle, the connection is broken. client can't be notified.
@ 2023-12-05 12:37  =?gb18030?B?TGlDaG9uZw==?= <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: =?gb18030?B?TGlDaG9uZw==?= @ 2023-12-05 12:37 UTC (permalink / raw)
  To: pgsql-odbc

你好


事务场景下,SQLFreeHandle调用进行中,连接断开,但是SQLFreeHandle返回SQL_SUCCESS。由于SQLFreeHandle调用连接断开,触发cc_on_abort,将事务取消,在客户端执行commit时,没有与服务端交互并且返回成功,导致客户端以为数据已提交,导致丢数据。


In a transaction scenario, the connection is disconnected while the SQLFreeHandle call is in progress, but SQLFreeHandle returns SQL_SUCCESS. Because the connection is disconnected by the SQLFreeHandle call, the cc_on_abort is triggered, the transaction is canceled, and the client performs a commit without interacting with the server and returning success, causing the client to think that the data has been submitted, resulting in data loss.



call step:
SQLAllocHandle1
sql1
SQLFreeHandle1
SQLAllocHandle2
sql2
SQLFreeHandle2
commit


during&nbsp;call SQLFreeHandle2, the connection&nbsp;is broken. client can't&nbsp;be&nbsp;notified.
we modify the code, in statement.c&nbsp;SC_Destructor and PGAPI_FreeStmt,&nbsp;&nbsp;Is there any other better solution to this problem? Please share your thoughts and suggestions, thanks.




below is psqlodbc log, debug=2
--------------
[7f5843a3a700]&nbsp; &nbsp; bind.c[PDATA_free_params]689: leaving
[7f5843a3a700] results.c[PGAPI_MoreResults]2035: leaving 100
[7f5843a3a700]odbcapi30.[SQLFreeHandle]250: Entering
[7f5843a3a700]statement.[PGAPI_FreeStmt]248: entering...hstmt=0x7f583c044600, fOption=1
[7f5843a3a700] qresult.c[QR_Destructor]344: entering
[7f5843a3a700]statement.[SC_init_Result]536: leaving(0x7f583c044600)
[7f5843a3a700]statement.[SC_Destructor]483: entering self=0x7f583c044600, self-&gt;result=(nil), self-&gt;hdbc=0xe27cc0
[7f5843a3a700]connection[CC_send_query_append]1809: conn=0xe27cc0, query='DEALLOCATE "_PLAN0x7f583c044600"'
[7f5843a3a700]connection[CC_send_query_append]1891: query_len=32
[7f5843a3a700]connection[CC_send_query_append]1935: [QLOG]PQsendQuery: 0xdf4990 'SAVEPOINT _per_query_svp_;DEALLOCATE "_PLAN0x7f583c044600";RELEASE _per_query_svp_'
[7f5843a3a700] qresult.c[QR_Constructor]173: entering
[7f5843a3a700] qresult.c[QR_Constructor]242: leaving 0x7f583c030380
[7f5843a3a700]connection[handle_pgres_error]864: PG_DIAG_SEVERITY_NONLOCALIZED=(null)
[7f5843a3a700]connection[handle_pgres_error]883: [QLOG]	((null)) (null) '(null)'
[7f5843a3a700]connection[handle_pgres_error]950: error message=server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
(137)
[7f5843a3a700]connection[CC_on_abort]1582: entering opt=2
[7f5843a3a700]connection[CC_on_abort]1604: [QLOG]PQfinish: 0xdf4990
[7f5843a3a700] qresult.c[QR_Destructor]344: entering
[7f5843a3a700] qresult.c[QR_close_result]255: entering
[7f5843a3a700] qresult.c[QR_free_memory]479: entering fcount=0
[7f5843a3a700] qresult.c[QR_free_memory]577: leaving
[7f5843a3a700] qresult.c[QR_close_result]319: leaving
[7f5843a3a700] qresult.c[QR_Destructor]348: leaving
[7f5843a3a700]&nbsp; &nbsp; bind.c[APD_free_params]643: entering self=0x7f583c0447e0
[7f5843a3a700]&nbsp; &nbsp; bind.c[APD_free_params]655: leaving
[7f5843a3a700]&nbsp; &nbsp; bind.c[IPD_free_params]698: entering self=0x7f583c044850
[7f5843a3a700]&nbsp; &nbsp; bind.c[IPD_free_params]709: leaving
[7f5843a3a700]&nbsp; &nbsp; bind.c[PDATA_free_params]663: entering self=0x7f583c044948
[7f5843a3a700]&nbsp; &nbsp; bind.c[PDATA_free_params]689: leaving
[7f5843a3a700]statement.[SC_Destructor]526: leaving
[7f5843a3a700]odbcapi30.[SQLEndTran]171: Entering
[7f5843a3a700] execute.c[PGAPI_Transact]1208: entering hdbc=0xe27cc0, henv=(nil)
[7f5843a3a700]odbcapi30.[SQLAllocHandle]40: Entering
[7f5843a3a700]statement.[PGAPI_AllocStmt]189: entering...



Best Wishes


LiChong
[email protected]



&nbsp;

Attachments:

  [application/octet-stream] statement.c (85.0K, 3-statement.c)
  download

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: during call SQLFreeHandle, the connection is broken. client can't be notified.
@ 2023-12-05 21:17  Dave Cramer <[email protected]>
  parent: =?gb18030?B?TGlDaG9uZw==?= <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Dave Cramer @ 2023-12-05 21:17 UTC (permalink / raw)
  To: LiChong <[email protected]>; +Cc: pgsql-odbc

Hi LiChong,

Attached is a patch which is the diff between your file and the current
master

There are some differences at the bottom of the file that seem to indicate
that your version of the source is older than the current master. Can you
confirm?

Also can you send back your changes in a patch file.

I used git diff to produce the attached patch
Dave Cramer
www.postgres.rocks


On Tue, 5 Dec 2023 at 10:05, LiChong <[email protected]> wrote:

> 你好
>
>
> 事务场景下,SQLFreeHandle调用进行中,连接断开,但是SQLFreeHandle返回SQL_SUCCESS。由于SQLFreeHandle调用连接断开,触发cc_on_abort,将事务取消,在客户端执行commit时,没有与服务端交互并且返回成功,导致客户端以为数据已提交,导致丢数据。
>
> In a transaction scenario, the connection is disconnected while the
> SQLFreeHandle call is in progress, but SQLFreeHandle returns SQL_SUCCESS.
> Because the connection is disconnected by the SQLFreeHandle call, the
> cc_on_abort is triggered, the transaction is canceled, and the client
> performs a commit without interacting with the server and returning
> success, causing the client to think that the data has been submitted,
> resulting in data loss.
>
> call step:
> SQLAllocHandle1
> sql1
> SQLFreeHandle1
> SQLAllocHandle2
> sql2
> SQLFreeHandle2
> commit
>
> during call SQLFreeHandle2, the connection is broken. client can't be
> notified.
> we modify the code, in statement.c SC_Destructor and PGAPI_FreeStmt,  Is
> there any other better solution to this problem? Please share your thoughts
> and suggestions, thanks.
>
>
> below is psqlodbc log, debug=2
> --------------
> [7f5843a3a700]    bind.c[PDATA_free_params]689: leaving
> [7f5843a3a700] results.c[PGAPI_MoreResults]2035: leaving 100
> [7f5843a3a700]odbcapi30.[SQLFreeHandle]250: Entering
> [7f5843a3a700]statement.[PGAPI_FreeStmt]248:
> entering...hstmt=0x7f583c044600, fOption=1
> [7f5843a3a700] qresult.c[QR_Destructor]344: entering
> [7f5843a3a700]statement.[SC_init_Result]536: leaving(0x7f583c044600)
> [7f5843a3a700]statement.[SC_Destructor]483: entering self=0x7f583c044600,
> self->result=(nil), self->hdbc=0xe27cc0
> [7f5843a3a700]connection[CC_send_query_append]1809: conn=0xe27cc0,
> query='DEALLOCATE "_PLAN0x7f583c044600"'
> [7f5843a3a700]connection[CC_send_query_append]1891: query_len=32
> [7f5843a3a700]connection[CC_send_query_append]1935: [QLOG]PQsendQuery:
> 0xdf4990 'SAVEPOINT _per_query_svp_;DEALLOCATE
> "_PLAN0x7f583c044600";RELEASE _per_query_svp_'
> [7f5843a3a700] qresult.c[QR_Constructor]173: entering
> [7f5843a3a700] qresult.c[QR_Constructor]242: leaving 0x7f583c030380
> [7f5843a3a700]connection[handle_pgres_error]864:
> PG_DIAG_SEVERITY_NONLOCALIZED=(null)
> [7f5843a3a700]connection[handle_pgres_error]883: [QLOG] ((null)) (null)
> '(null)'
> [7f5843a3a700]connection[handle_pgres_error]950: error message=server
> closed the connection unexpectedly
> This probably means the server terminated abnormally
> before or while processing the request.
> (137)
> [7f5843a3a700]connection[CC_on_abort]1582: entering opt=2
> [7f5843a3a700]connection[CC_on_abort]1604: [QLOG]PQfinish: 0xdf4990
> [7f5843a3a700] qresult.c[QR_Destructor]344: entering
> [7f5843a3a700] qresult.c[QR_close_result]255: entering
> [7f5843a3a700] qresult.c[QR_free_memory]479: entering fcount=0
> [7f5843a3a700] qresult.c[QR_free_memory]577: leaving
> [7f5843a3a700] qresult.c[QR_close_result]319: leaving
> [7f5843a3a700] qresult.c[QR_Destructor]348: leaving
> [7f5843a3a700]    bind.c[APD_free_params]643: entering self=0x7f583c0447e0
> [7f5843a3a700]    bind.c[APD_free_params]655: leaving
> [7f5843a3a700]    bind.c[IPD_free_params]698: entering self=0x7f583c044850
> [7f5843a3a700]    bind.c[IPD_free_params]709: leaving
> [7f5843a3a700]    bind.c[PDATA_free_params]663: entering
> self=0x7f583c044948
> [7f5843a3a700]    bind.c[PDATA_free_params]689: leaving
> [7f5843a3a700]statement.[SC_Destructor]526: leaving
> [7f5843a3a700]odbcapi30.[SQLEndTran]171: Entering
> [7f5843a3a700] execute.c[PGAPI_Transact]1208: entering hdbc=0xe27cc0,
> henv=(nil)
> [7f5843a3a700]odbcapi30.[SQLAllocHandle]40: Entering
> [7f5843a3a700]statement.[PGAPI_AllocStmt]189: entering...
>
> Best Wishes
> ------------------------------
> LiChong
> [email protected]
>
> <https://wx.mail.qq.com/home/index?t=readmail_businesscard_midpage&nocheck=true&name=LiChong&...;
>
>


Attachments:

  [application/octet-stream] statement.c.patch (2.5K, 3-statement.c.patch)
  download | inline diff:
diff --git a/statement.c b/statement.c
index f45cd79..0df55d4 100644
--- a/statement.c
+++ b/statement.c
@@ -296,7 +296,10 @@ PGAPI_FreeStmt(HSTMT hstmt,
 		if (stmt->execute_parent)
 			stmt->execute_parent->execute_delegate = NULL;
 		/* Destroy the statement and free any results, cursors, etc. */
-		SC_Destructor(stmt);
+		//SC_Destructor(stmt);
+		/*if the connection was give up,return SQL_ERROR.*/
+		if(SC_Destructor(stmt) == FALSE)
+			return SQL_ERROR;
 	}
 	else if (fOption == SQL_UNBIND)
 		SC_unbind_cols(stmt);
@@ -477,6 +480,7 @@ SC_Constructor(ConnectionClass *conn)
 char
 SC_Destructor(StatementClass *self)
 {
+	char cRet = TRUE;
 	CSTR func	= "SC_Destructor";
 	QResultClass	*res = SC_get_Result(self);
 
@@ -498,6 +502,12 @@ SC_Destructor(StatementClass *self)
 
 	SC_initialize_stmts(self, TRUE);
 
+	if(self->hdbc && !self->hdbc->pqconn)
+	{
+		SC_set_error(self, STMT_COMMUNICATION_ERROR, "connection error.", func);
+		cRet = FALSE;
+	}
+
         /* Free the parsed table information */
 	SC_initialize_cols_info(self, FALSE, TRUE);
 
@@ -525,7 +535,7 @@ SC_Destructor(StatementClass *self)
 
 	MYLOG(0, "leaving\n");
 
-	return TRUE;
+	return cRet;
 }
 
 void
@@ -2304,7 +2314,6 @@ MYLOG(DETAIL_LOG_LEVEL, "!!SC_fetch return =%d\n", ret);
 		{
 			char			fetch[128];
 			QResultClass 	*last = NULL, *res;
-			BOOL			refcursor_found = FALSE;
 
 			/* Iterate the columns in the result to look for refcursors */
 			numcols = QR_NumResultCols(rhold.first);
@@ -2319,12 +2328,7 @@ MYLOG(DETAIL_LOG_LEVEL, "!!SC_fetch return =%d\n", ret);
 						break;
 					}
 
-					refcursor_found = TRUE;
 					STR_TO_NAME(self->cursor_name, QR_get_value_backend_text(rhold.first, 0, i));
-					/* Skip NULL refcursors (allows procedure to return a variable number of results) */
-					if (!SC_cursor_is_valid(self))
-						continue;
-
 					SC_set_fetchcursor(self);
 					qi.result_in = NULL;
 					qi.cursor = SC_cursor_name(self);
@@ -2345,23 +2349,19 @@ MYLOG(DETAIL_LOG_LEVEL, "!!SC_fetch return =%d\n", ret);
 							QR_concat(last, res);
 							self->multi_statement = TRUE;
 						}
-						last = res;
 						if (!QR_command_maybe_successful(res))
 						{
 							SC_set_errorinfo(self, res, 0);
+							QR_Destructor(rhold.first);
 							break;
 						}
+
+						last = res;
 					}
 				}
 			}
-			if (refcursor_found)
-			{
-				/* Discard original result */
-				if (NULL == last)
-					SC_set_Result(self, QR_Constructor());	/* return empty result */
-				else
-					QR_Destructor(rhold.first);
-			}
+			if (last)
+				QR_Destructor(rhold.first);
 		}
 	}
 cleanup:


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2023-12-05 21:17 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05 12:37 during call SQLFreeHandle, the connection is broken. client can't be notified. =?gb18030?B?TGlDaG9uZw==?= <[email protected]>
2023-12-05 21:17 ` Dave Cramer <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox