xref: /PHP-8.3/ext/oci8/tests/bug43497.phpt (revision a53e5617)
1--TEST--
2Bug #43497 (OCI8 XML/getClobVal aka temporary LOBs leak UGA memory)
3--EXTENSIONS--
4oci8
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8$target_dbs = array('oracledb' => true, 'timesten' => false);  // test runs on these DBs
9require __DIR__.'/skipif.inc';
10if (getenv('SKIP_SLOW_TESTS')) die('skip slow tests excluded by request');
11?>
12--FILE--
13<?php
14
15require __DIR__.'/connect.inc';
16
17function sessionid($c)  // determines and returns current session ID
18{
19    $query = "select sid from v\$session where audsid = userenv('sessionid')";
20
21    $stmt = oci_parse($c, $query);
22
23    if (oci_execute($stmt, OCI_DEFAULT)) {
24        $row = oci_fetch($stmt);
25        return oci_result($stmt, 1);
26    }
27
28    return null;
29}
30
31
32function templobs($c, $sid)  // returns number of temporary LOBs
33{
34    $query = "select abstract_lobs from v\$temporary_lobs where sid = " . $sid;
35
36    $stmt = oci_parse($c, $query);
37
38    if (oci_execute($stmt, OCI_DEFAULT)) {
39        $row = oci_fetch($stmt);
40        $val = oci_result($stmt, 1);
41        oci_free_statement($stmt);
42        return $val;
43    }
44    return null;
45}
46
47
48// Read all XML data using explicit LOB locator
49function readxmltab_ex($c)
50{
51    $stmt = oci_parse($c, "select extract(xml, '/').getclobval() from bug43497_tab");
52
53    $cntchk = 0;
54    if (oci_execute($stmt)) {
55        while ($result = oci_fetch_array($stmt, OCI_NUM)) {
56            $result[0]->free();   // cleanup properly
57            ++$cntchk;
58        }
59    }
60    echo "Loop count check = $cntchk\n";
61}
62
63// Read all XML data using explicit LOB locator but without freeing the temp lobs
64function readxmltab_ex_nofree($c)
65{
66    $stmt = oci_parse($c, "select extract(xml, '/').getclobval() from bug43497_tab");
67
68    $cntchk = 0;
69    if (oci_execute($stmt)) {
70        while ($result = oci_fetch_array($stmt, OCI_NUM)) {
71            ++$cntchk;
72        }
73    }
74    echo "Loop count check = $cntchk\n";
75}
76
77// Read all XML data using implicit LOB locator
78function readxmltab_im($c)
79{
80    $stmt = oci_parse($c, "select extract(xml, '/').getclobval() from bug43497_tab");
81
82    $cntchk = 0;
83    if (oci_execute($stmt)) {
84        while ($result = oci_fetch_array($stmt, OCI_NUM+OCI_RETURN_LOBS)) {
85            ++$cntchk;
86        }
87    }
88    echo "Loop count check = $cntchk\n";
89}
90
91function createxmltab($c)  // create table w/ field of XML type
92{
93    @dropxmltab($c);
94    $stmt = oci_parse($c, "create table bug43497_tab (id number primary key, xml xmltype)");
95    oci_execute($stmt);
96}
97
98function dropxmltab($c)  // delete table
99{
100    $stmt = oci_parse($c, "drop table bug43497_tab");
101    oci_execute($stmt);
102}
103
104
105function fillxmltab($c)
106{
107    for ($id = 1; $id <= 100; $id++) {
108
109        // create an XML element string with random data
110        $s = "<data>";
111        for ($j = 0; $j < 128; $j++) {
112            $s .= rand();
113        }
114        $s .= "</data>\n";
115        for ($j = 0; $j < 4; $j++) {
116            $s .= $s;
117        }
118        $data = "<?xml version=\"1.0\"?><records>" . $s . "</records>";
119
120        // insert XML data into database
121
122        $stmt = oci_parse($c, "insert into bug43497_tab(id, xml) values (:id, sys.xmltype.createxml(:xml))");
123        oci_bind_by_name($stmt, ":id", $id);
124        $clob = oci_new_descriptor($c, OCI_D_LOB);
125        oci_bind_by_name($stmt, ":xml", $clob, -1, OCI_B_CLOB);
126        $clob->writeTemporary($data);
127        oci_execute($stmt);
128
129        $clob->close();
130        $clob->free();
131    }
132}
133
134
135// Initialize
136
137createxmltab($c);
138fillxmltab($c);
139
140// Run Test
141
142$sid = sessionid($c);
143
144echo "Explicit LOB use\n";
145for ($i = 1; $i <= 10; $i++) {
146    echo "\nRun              = " . $i . "\n";
147    echo "Temporary LOBs   = " . templobs($c, $sid) . "\n";
148    readxmltab_ex($c);
149}
150
151echo "\nImplicit LOB use\n";
152for ($i = 1; $i <= 10; $i++) {
153    echo "\nRun              = " . $i . "\n";
154    echo "Temporary LOBs   = " . templobs($c, $sid) . "\n";
155    readxmltab_im($c);
156}
157
158echo "\nExplicit LOB with no free\n";
159for ($i = 1; $i <= 10; $i++) {
160    echo "\nRun              = " . $i . "\n";
161    echo "Temporary LOBs   = " . templobs($c, $sid) . "\n";
162    readxmltab_ex_nofree($c);
163}
164
165
166
167// Cleanup
168
169dropxmltab($c);
170
171oci_close($c);
172
173echo "Done\n";
174?>
175--EXPECT--
176Explicit LOB use
177
178Run              = 1
179Temporary LOBs   = 0
180Loop count check = 100
181
182Run              = 2
183Temporary LOBs   = 0
184Loop count check = 100
185
186Run              = 3
187Temporary LOBs   = 0
188Loop count check = 100
189
190Run              = 4
191Temporary LOBs   = 0
192Loop count check = 100
193
194Run              = 5
195Temporary LOBs   = 0
196Loop count check = 100
197
198Run              = 6
199Temporary LOBs   = 0
200Loop count check = 100
201
202Run              = 7
203Temporary LOBs   = 0
204Loop count check = 100
205
206Run              = 8
207Temporary LOBs   = 0
208Loop count check = 100
209
210Run              = 9
211Temporary LOBs   = 0
212Loop count check = 100
213
214Run              = 10
215Temporary LOBs   = 0
216Loop count check = 100
217
218Implicit LOB use
219
220Run              = 1
221Temporary LOBs   = 0
222Loop count check = 100
223
224Run              = 2
225Temporary LOBs   = 0
226Loop count check = 100
227
228Run              = 3
229Temporary LOBs   = 0
230Loop count check = 100
231
232Run              = 4
233Temporary LOBs   = 0
234Loop count check = 100
235
236Run              = 5
237Temporary LOBs   = 0
238Loop count check = 100
239
240Run              = 6
241Temporary LOBs   = 0
242Loop count check = 100
243
244Run              = 7
245Temporary LOBs   = 0
246Loop count check = 100
247
248Run              = 8
249Temporary LOBs   = 0
250Loop count check = 100
251
252Run              = 9
253Temporary LOBs   = 0
254Loop count check = 100
255
256Run              = 10
257Temporary LOBs   = 0
258Loop count check = 100
259
260Explicit LOB with no free
261
262Run              = 1
263Temporary LOBs   = 0
264Loop count check = 100
265
266Run              = 2
267Temporary LOBs   = 0
268Loop count check = 100
269
270Run              = 3
271Temporary LOBs   = 0
272Loop count check = 100
273
274Run              = 4
275Temporary LOBs   = 0
276Loop count check = 100
277
278Run              = 5
279Temporary LOBs   = 0
280Loop count check = 100
281
282Run              = 6
283Temporary LOBs   = 0
284Loop count check = 100
285
286Run              = 7
287Temporary LOBs   = 0
288Loop count check = 100
289
290Run              = 8
291Temporary LOBs   = 0
292Loop count check = 100
293
294Run              = 9
295Temporary LOBs   = 0
296Loop count check = 100
297
298Run              = 10
299Temporary LOBs   = 0
300Loop count check = 100
301Done
302