This appendix describes methods to optimize a Java Database Connectivity (JDBC) application or applet. It includes the following topics:
Oracle JDBC drivers provide full support for, and are highly optimized for, applications that use Java multithreading. Controlled serial access to a connection, such as that provided by connection caching, is both necessary and encouraged. However, Oracle strongly discourages sharing a database connection among multiple threads. Avoid allowing multiple threads to access a connection simultaneously. If multiple threads must share a connection, use a disciplined begin-using/end-using protocol.
Auto-commit mode indicates to the database whether to issue an automatic
COMMIT operation after every SQL operation. Being in auto-commit mode can be expensive in terms of time and processing effort if, for example, you are repeating the same statement with different bind variables.
By default, new connection objects are in auto-commit mode. However, you can disable auto-commit mode with the
setAutoCommit method of the connection object, either
In auto-commit mode, the
COMMIT operation occurs either when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a
ResultSet object, the statement completes when the last row of the Result Set has been retrieved or when the Result Set has been closed. In more complex cases, a single statement can return multiple results as well as output parameter values. Here, the
COMMIT occurs when all results and output parameter values have been retrieved.
If you disable auto-commit mode with a
setAutoCommit(false) call, then you must manually commit or roll back groups of operations using the
rollback method of the connection object.
The following example illustrates loading the driver and connecting to the database. Because new connections are in auto-commit mode by default, this example shows how to disable auto-commit. In the example,
conn represents the
Connection object, and
stmt represents the
// Connect to the database // You can put a database host name after the @ sign in the connection URL. OracleDataSource ods = new OracleDataSource(); ods.setURL("jdbc:oracle:oci:@"); ods.setUser("scott"); ods.setPassword("tiger"); Connection conn = ods.getConnection(); // It's faster when auto commit is off conn.setAutoCommit (false); // Create a Statement Statement stmt = conn.createStatement (); ...
Oracle JDBC connection and statement objects allow you to specify the number of rows to prefetch into the client with each trip to the database while a result set is being populated during a query. You can set a value in a connection object that affects each statement produced through that connection, and you can override that value in any particular statement object. The default value in a connection object is 10. Prefetching data into the client reduces the number of round-trips to the server.
Similarly, and with more flexibility, JDBC 2.0 enables you to specify the number of rows to fetch with each trip, both for statement objects (affecting subsequent queries) and for result set objects (affecting row refetches). By default, a result set uses the value for the statement object that produced it. If you do not set the JDBC 2.0 fetch size, then the Oracle connection row-prefetch value is used by default.
See Also:"Fetch Size"
Oracle JDBC drivers allow you to accumulate
UPDATE operations of prepared statements at the client and send them to the server in batches. This feature reduces round-trips to the server. You can either use Oracle update batching, which typically processes a batch implicitly once a preset batch value is reached, or standard update batching, where the batch is processed explicitly.
Note:Oracle recommends to keep the batch sizes in the range of 100 or less. Larger batches provide little or no performance improvement and may actually reduce performance due to the client resources required to handle the large batch.
See Also:"Update Batching"
Statement caching improves performance by caching executable statements that are used repeatedly, such as in a loop or in a method that is called repeatedly. Applications use the statement cache to cache statements associated with a particular physical connection. When you enable Statement caching, a Statement object is cached when you call the
close method. Because each physical connection has its own cache, multiple caches can exist if you enable Statement caching for multiple physical connections.
Note:The Oracle JDBC drivers are optimized for use with the Oracle Statement cache. Oracle strongly recommends that you use the Oracle Statement cache (implicit or explicit).
When you enable Statement caching on a connection cache, the logical connections benefit from the Statement caching that is enabled on the underlying physical connection. If you try to enable Statement caching on a logical connection held by a connection cache, then this will throw an exception.
The SQL built-in types are those types with system-defined names, such as
CHAR, as opposed to the Oracle objects, varray, and nested table types, which have user-defined names. In JDBC programs that access data of built-in SQL types, all type conversions are unambiguous, because the program context determines the Java type to which a SQL datum will be converted.
The most efficient way to access numeric data is to use primitive Java types like
double. However, the range of values of these types do not exactly match the range of values of the SQL
NUMBER data type. As a result, there may be some loss of information. If absolute precision is required across the entire value range, then use the
All character data is converted to the UCS2 character set of Java. The most efficient way to access character data is as
java.lang.String. In worst case, this can cause a loss of information when two or more characters in the database character set map to a single UCS2 character. In Oracle Database 11g, all characters in the character set map to the characters in the UCS2 character set. However, some characters do map to surrogate pairs.
For transactions, the Oracle server supports only the
TRANSACTION_SERIALIZABLE transaction isolation levels. The default is
TRANSACTION_READ_COMMITTED. Use the following methods of the
oracle.jdbc.OracleConnection interface to get and set the level: