1 Caché de Statements
When a new statement is created, a SQL sentence to be executed in the database is cached in the system by default to optimize the performance. The limit of this cache is of 500 statements by process. Through the attribute prepare-cache can be indicated explicitly that you do not want to caches a specific statement. The following XSQL-Script performs a query to the database. This query cached in case it can be reused later throughout the execution of the process.
<xsql-script> <body> <set name='codigo'>A</set> <foreach> <select prefix='m_'> <columns>*</columns> <from table='cdiarios' /> <where>codigo = <codigo /></where> </select> <do> <println><m_codigo/>: <m_nomdia/></println> </do> </foreach> </body> </xsql-script>
In the example the system stores the statement.
SELECT * FROM cdiarios WHERE codigo = ?
And the successive times that this same select is needed, is reused changing the value of the ? by the one that corresponds.
2 Statements no reutilizables
Exists statements that, until they are cached, they can not be reused. In this case, it is necessary to indicate it explicitly through the attribute prepare-cache='false' which you do not want to cache. There is two cases in which it happens: The database engine does not support a ?, in certain conditions you have a unquoted string taking part of the sentence. The following example shows an statement no reusable:
<xsql-script> <body> <set name='a'>1</set> <set name='text'>hola</set> <while> <expr> <lt><a />600</lt> </expr> <do> <select> <columns>count(*) x</columns> <from table='clevels' /> <where>#a=1 AND code = <text /></where> </select> <set name='a'><add><a/>1</add></set> <println><a/></println> </do> </while> </body> </xsql-script>
The system caches:
SELECT * FROM table WHERE col = <text /> and #a = 1
When it is reused, the value of the variable a will have changed so that the same sentence will not be obtained and it can not be reused of the cache. For a=1:
SELECT * FROM table WHERE col = ? and 1=1
SELECT * FROM table WHERE col = ? and 2=1
This happens for example when you want to make a query with cond='1=1':
SELECT * FROM table WHERE col = ? and #cond
There is more cases, for example the instruction <nvl>, the comparation = o != to a value which is null, etc... This implies that the statement has a value which can not be replace by the ?, so, if it is in a loop, it will be cached n times (the key is the string and it changes in the successive iterations). This causes the cache to fill with statements that are not optimally cacheable and occupy a place that could be occupied by others that are reusable.
In Oracle each time that a statement is prepared, an open cursor remain is left and therefore it is essential not to cache preparedStatements unless they are going to be reused. In case of exceeding the maximum number of open cursors, Oracle will give the error ORA-0100: Maximum open cursors exceeded.