27.10.2013 Views

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If, for example, we want to reuse the statement object for another query, it is not<br />

necessary to completely release the allocated structures. <strong>Jaybird</strong> is required only<br />

to compile a new statement before using it, in other words we can skip the step 1.<br />

This saves us one round-trip to the server over the network, which might improve<br />

the application performance.<br />

If we close the statement completely, the allocated statement handle is no longer<br />

usable. <strong>Jaybird</strong> could allocate a new statement handle, however the<br />

implementation does not allow to use the Statement object after close() method<br />

has been called to comply with the <strong>JDBC</strong> specification.<br />

Statement lifetime and DDL<br />

The step 2 in the previous chapter is probably the most important, and usually,<br />

most expensive part of the statement execution life cycle.<br />

As it was already told, when <strong>Firebird</strong> server receives the “prepare statement” call,<br />

it parses the SQL statement and converts it into the executable form – BLR<br />

representation. BLR, a Binary Language Representation, contains low-level<br />

commands to traverse the database tables, conditions that are used to filter<br />

records, defines the order in which records are accessed, indices that are used to<br />

improve the performance, etc.<br />

When a BLR is prepared, it holds the references to all database object definitions<br />

that are used during that statement execution. This mechanism preserves the<br />

database schema consistency, it saves the statement objects from the “surprises”<br />

like accessing the database table that is been accessed by some application.<br />

However, holding a reference on the database objects has one very unpleasant<br />

effect – it is not possible to upgrade the database schema, if there are active<br />

connections to the database with open statements referencing the objects being<br />

upgraded. In other words, if two application are running and one is trying to<br />

modify the table, view, procedure or trigger definition while another one is<br />

accessing those objects, the first application will receive an error 335544453<br />

“object is in use”.<br />

Therefore it is strongly recommended to close the statement as soon as it is no<br />

longer needed. This invalidates the BLR and release all references to the database<br />

objects, making them available for the modification.<br />

Special care should be taken when the statement pooling is used (check the details<br />

on page 41). In this case statements are not released even if the close() method<br />

is called. The only possibility to close the pooled statements is to close the pooled<br />

connections. Please check the corresponding chapter for more information.<br />

The java.sql.PreparedStatement interface<br />

As we have seen, <strong>Jaybird</strong> already performs internal optimization when it comes to<br />

multiple statement execution – it can reuse the allocated statement handle in<br />

subsequent calls. However this improvement is very small and sometimes can<br />

even be neglected when compared to the time needed to compile the SQL<br />

statement into the BLR form.<br />

The PreparedStatement interface addresses such inefficiency. An object that<br />

implements this interface represents a precompiled statement that can be executed<br />

multiple times. If we use the execution flow described in the “Statement<br />

Chapter 4. Executing statements 38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!