|
|
| Is the JDBC-ODBC Bridge multi-threaded? |
No. The JDBC-ODBC Bridge does not support multi threading. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may use the Bridge, but they won't get the advantages of multi-threading.
|
| How do you handle your own transaction ? |
Connection Object has a method called setAutocommit ( boolean flag) . For handling our own transaction we can set the parameter to false and begin your transaction . Finally commit the transaction by calling the commit method.
|
| Will a call to PreparedStatement.executeQuery() always close the ResultSet from the previous executeQuery()? |
ResultSet is automatically closed by the Statement that generated it when that Statement is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.
|
| What is the difference between client and server database cursors? |
What you see on the client side is the current row of the cursor which called a Result (ODBC) or ResultSet (JDBC). The cursor is a server-side entity only and remains on the server side.
|
| Are prepared statements faster because they are compiled? if so, where and when are they compiled? |
Prepared Statements aren't actually compiled, but they are bound by the JDBC driver. Depending on the driver, Prepared Statements can be a lot faster - if you re-use them. Some drivers bind the columns you request in the SQL statement. When you execute Connection.prepareStatement(), all the columns bindings take place, so the binding overhead does not occur each time you run the Prepared Statement. For additional information on Prepared Statement performance and binding see JDBC Performance Tips on IBM's website.
|
| What advantage is there to using prepared statements if I am using connection pooling or closing the connection frequently to avoid resource/connection/cursor limitations? |
The ability to choose the 'best' efficiency ( or evaluate tradeoffs, if you prefer, ) is, at times, the most important piece of a mature developer's skillset. This is YAA ( Yet Another Area, ) where that maxim applies. Apparently there is an effort to allow prepared statements to work 'better' with connection pools in JDBC 3.0, but for now, one loses most of the original benefit of prepared statements when the connection is closed. A prepared statement obviously fits best when a statement differing only in variable criteria is executed over and over without closing the statement.
However, depending on the DB engine, the SQL may be cached and reused even for a different prepared statement and most of the work is done by the DB engine rather than the driver. In addition, prepared statements deal with data conversions that can be error prone in straight ahead, built on the fly SQL; handling quotes and dates in a manner transparent to the developer, for example.
|
| What is JDBC, anyhow? |
JDBC is Java's means of dynamically accessing tabular data, and primarily data in relational databases, in a generic manner, normally using standard SQL statements.
|
| Can I reuse a Statement or must I create a new one for each query? |
When using a JDBC compliant driver, you can use the same Statement for any number of queries. However, some older drivers did not always "respect the spec." Also note that a Statement SHOULD automatically close the current ResultSet before executing a new query, so be sure you are done with it before re-querying using the same Statement.
|
| What areas should I focus on for the best performance in a JDBC application? |
These are few points to consider:
Use a connection pool mechanism whenever possible.
Use prepared statements. These can be beneficial, for example with DB specific escaping, even when used only once.
Use stored procedures when they can be created in a standard manner. Do watch out for DB specific SP definitions that can cause migration headaches.
Even though the jdbc promotes portability, true portability comes from NOT depending on any database specific data types, functions and so on.
Select only required columns rather than using select * from Tablexyz.
Always close Statement and ResultSet objects as soon as possible.
Write modular classes to handle database interaction specifics.
Work with DatabaseMetaData to get information about database functionality.
Softcode database specific parameters with, for example, properties files.
Always catch AND handle database warnings and exceptions. Be sure to check for additional pending exceptions.
Test your code with debug statements to determine the time it takes to execute your query and so on to help in tuning your code. Also use query plan functionality if available.
Use proper ( and a single standard if possible ) formats, especially for dates.
Use proper data types for specific kind of data. For example, store birthdate as a date type rather than, say, varchar.
|
| Is Class.forName(Drivername) the only way to load a driver? Can I instantiate the Driver and use the object of the driver? |
Yes, you can use the driver directly. Create an instance of the driver and use the connect method from the Driver interface. Note that there may actually be two instances created, due to the expected standard behavior of drivers when the class is loaded.
|
|
|
|
|