@@ -102,7 +102,7 @@ static int _close_pconn_with_res(zval *zv, void *p)
102102 }
103103
104104 odbc_connection * list_conn = ((odbc_connection * )(le -> ptr ));
105- odbc_connection * obj_conn = odbc_link_from_obj (( zend_object * ) p ) -> connection ;
105+ odbc_connection * obj_conn = ( odbc_connection * ) p ;
106106 if (list_conn == obj_conn ) {
107107 return ZEND_HASH_APPLY_REMOVE ;
108108 }
@@ -138,10 +138,10 @@ static void odbc_link_free(odbc_link *link)
138138 zend_hash_destroy (& link -> connection -> results );
139139 efree (link -> connection );
140140 ODBCG (num_links )-- ;
141+ }
141142
142- if (link -> hash ) {
143- zend_hash_del (& ODBCG (non_persistent_connections ), link -> hash );
144- }
143+ if (link -> hash ) {
144+ zend_hash_del (& ODBCG (connections ), link -> hash );
145145 }
146146
147147 link -> connection = NULL ;
@@ -317,6 +317,7 @@ static void _close_odbc_pconn(zend_resource *rsrc)
317317 SQLFreeConnect (conn -> hdbc );
318318 SQLFreeEnv (conn -> henv );
319319 }
320+ free (conn );
320321
321322 ODBCG (num_links )-- ;
322323 ODBCG (num_persistent )-- ;
@@ -501,12 +502,12 @@ static PHP_GINIT_FUNCTION(odbc)
501502 ZEND_TSRMLS_CACHE_UPDATE ();
502503#endif
503504 odbc_globals -> num_persistent = 0 ;
504- zend_hash_init (& odbc_globals -> non_persistent_connections , 0 , NULL , NULL , 1 );
505+ zend_hash_init (& odbc_globals -> connections , 0 , NULL , NULL , 1 );
505506}
506507
507508static PHP_GSHUTDOWN_FUNCTION (odbc )
508509{
509- zend_hash_destroy (& odbc_globals -> non_persistent_connections );
510+ zend_hash_destroy (& odbc_globals -> connections );
510511}
511512
512513/* {{{ PHP_MINIT_FUNCTION */
@@ -878,14 +879,14 @@ PHP_FUNCTION(odbc_close_all)
878879 }
879880
880881 /* Loop through the non-persistent connection list, now close all non-persistent connections and their results */
881- ZEND_HASH_FOREACH_VAL (& ODBCG (non_persistent_connections ), zv ) {
882+ ZEND_HASH_FOREACH_VAL (& ODBCG (connections ), zv ) {
882883 odbc_link * link = Z_ODBC_LINK_P (zv );
883884 if (link -> connection ) {
884885 odbc_link_free (link );
885886 }
886887 } ZEND_HASH_FOREACH_END ();
887888
888- zend_hash_clean (& ODBCG (non_persistent_connections ));
889+ zend_hash_clean (& ODBCG (connections ));
889890
890891 zend_hash_apply (& EG (persistent_list ), _close_pconn );
891892}
@@ -2299,7 +2300,7 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
22992300 }
23002301
23012302 char * hashed_details ;
2302- int hashed_details_len = spprintf (& hashed_details , 0 , "%s_%s_%s_%s_%d" , ODBC_TYPE , db , uid , pwd , cur_opt );
2303+ int hashed_details_len = spprintf (& hashed_details , 0 , "%d_% s_%s_%s_%s_%d" , persistent , ODBC_TYPE , db , uid , pwd , cur_opt );
23032304
23042305try_and_get_another_connection :
23052306
@@ -2333,6 +2334,8 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
23332334 RETURN_FALSE ;
23342335 }
23352336
2337+ zend_hash_str_add_new (& ODBCG (connections ), hashed_details , hashed_details_len , return_value );
2338+
23362339 ODBCG (num_persistent )++ ;
23372340 ODBCG (num_links )++ ;
23382341 } else { /* found connection */
@@ -2381,15 +2384,22 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
23812384 }
23822385 }
23832386
2384- object_init_ex (return_value , odbc_connection_ce );
2385- odbc_link * link = Z_ODBC_LINK_P (return_value );
2386- link -> connection = db_conn ;
2387- link -> hash = zend_string_init (hashed_details , hashed_details_len , persistent );
2388- link -> persistent = true;
2387+ zval * tmp ;
2388+ if ((tmp = zend_hash_str_find (& ODBCG (connections ), hashed_details , hashed_details_len )) == NULL ) {
2389+ object_init_ex (return_value , odbc_connection_ce );
2390+ odbc_link * link = Z_ODBC_LINK_P (return_value );
2391+ link -> connection = db_conn ;
2392+ link -> hash = zend_string_init (hashed_details , hashed_details_len , persistent );
2393+ link -> persistent = true;
2394+ } else {
2395+ ZVAL_COPY (return_value , tmp );
2396+
2397+ ZEND_ASSERT (Z_ODBC_CONNECTION_P (return_value ) == db_conn && "Persistent connection has changed" );
2398+ }
23892399 }
2390- } else {
2400+ } else { /* non-persistent */
23912401 zval * tmp ;
2392- if ((tmp = zend_hash_str_find (& ODBCG (non_persistent_connections ), hashed_details , hashed_details_len )) == NULL ) { /* non-persistent, new */
2402+ if ((tmp = zend_hash_str_find (& ODBCG (connections ), hashed_details , hashed_details_len )) == NULL ) { /* non-persistent, new */
23932403 if (ODBCG (max_links ) != -1 && ODBCG (num_links ) >= ODBCG (max_links )) {
23942404 php_error_docref (NULL , E_WARNING , "Too many open connections (" ZEND_LONG_FMT ")" , ODBCG (num_links ));
23952405 efree (hashed_details );
@@ -2403,7 +2413,7 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
24032413 }
24042414 ODBCG (num_links )++ ;
24052415
2406- zend_hash_str_add_new (& ODBCG (non_persistent_connections ), hashed_details , hashed_details_len , return_value );
2416+ zend_hash_str_add_new (& ODBCG (connections ), hashed_details , hashed_details_len , return_value );
24072417 } else { /* non-persistent, pre-existing */
24082418 ZVAL_COPY (return_value , tmp );
24092419 }
@@ -2417,19 +2427,21 @@ PHP_FUNCTION(odbc_close)
24172427{
24182428 zval * pv_conn ;
24192429 odbc_link * link ;
2430+ odbc_connection * connection ;
24202431
24212432 if (zend_parse_parameters (ZEND_NUM_ARGS (), "O" , & pv_conn , odbc_connection_ce ) == FAILURE ) {
24222433 RETURN_THROWS ();
24232434 }
24242435
24252436 link = Z_ODBC_LINK_P (pv_conn );
2426- CHECK_ODBC_CONNECTION (link -> connection );
2437+ connection = Z_ODBC_CONNECTION_P (pv_conn );
2438+ CHECK_ODBC_CONNECTION (connection );
2439+
2440+ odbc_link_free (link );
24272441
24282442 if (link -> persistent ) {
2429- zend_hash_apply_with_argument (& EG (persistent_list ), _close_pconn_with_res , (void * ) Z_OBJ_P ( pv_conn ) );
2443+ zend_hash_apply_with_argument (& EG (persistent_list ), _close_pconn_with_res , (void * ) connection );
24302444 }
2431-
2432- odbc_link_free (link );
24332445}
24342446/* }}} */
24352447
0 commit comments