JDBC-4 hot features:

Stale connections ---isvalid():

Because a stale connection does not necessarily mean a closed connection (which might be garbage collected), connections that became unusable often took up those valuable connection resources. A new method in JDBC 4's Connection class, isValid(), allows a client to query the database driver if a connection is still valid. This allows a more intelligent management of connection pools by clients.

Preparedstaement pooling---poolable property:

While one database connection is indistinguishable from another, the same does not hold for statements: Some SQL statement are more frequently used than others in any application domain. Prior to JDBC 4, there was no way to tell such statements apart in a statement pooling environment. While every JDBC 4 PreparedStatement is poolable by default, the new poolable property allows an application to offer a hint to the statement pooler as to whether a statement should be pooled. This allows an application developer, or a development tool, to better manage statement-pooling resources, offering pooling preference to frequently accessed statements.

setClientInfo() -

Because a database is a shared resource, many Java applications require concurrent access to the same data store. As each of those applications connect to the database and execute statements on a connection, connections from some applications might take up more resources than others, sometimes unfairly bogging down database performance. JDBC 4 allows an application to associate metadata with a database connection via the setClientInfo() method, which consumes a name/string value pair, or a Properties object. Diagnostic tools can subsequently retrieve the name and characteristics of applications causing database problems.

J2SE Service Provider mechanism to load drivers:

JDBC 4 adds the J2SE Service Provider mechanism as another means to specify database drivers. For this to work, driver JAR files must include the file META-INF/services/java.sql.driver. That file must contain a single line with the name of the JDBC driver's implementation of the Driver interface. Invoking getConnection() on DriverManager will load a driver so packaged, if needed. Upon loading the driver, an instance of the driver is created, and then registerDriver() is invoked to make that driver available to clients.

XML as a First-Class SQL Data Type:
until JDBC 4, Java clients could access XML data elements only as BLOBs, CLOBs, or even TEXT types.
The best way to understand SQLXML, the new JDBC 4 mapping of the SQL XML type, is with an example. Suppose you store a user's blog posts in an XML document format, and you'd like to iterate over those posts and display them in a Web page. The following database table would hold this information:
create table user_has_blog(userid int, blog_entry xml);

Using JDBC 4's SQLXML data type, you could retrieve a user's blog entries as follows:

Connection c = myDataSource.getConnection();
PreparedStatement st = c.prepareStatement("select userid, blog_entry from user_has_blog");
ResultSet rs = st.executeQuery();
while (rs.next()) {
SQLXML blog = st.getSQLXML("blog_entry");
javax.xml.stream.XMLStreamReader reader = blog.createXMLStreamReader();
//read data here


SqlExcetions Extended in jDBC4:

JDBC 4 introduces a finer-grained exception hierarchy via both chained exceptions and by dividing exceptions into transient and non-transient categories.


SQLTransientConnectionException Indicate some change in the connection's communication connection layer. SQLState 08

SQLTimeoutException When timeout specified by a Statement is expired. No corresponding SQLState.

SQLTransactionRollbackException Indicate that the current Statement was automatically rolled back by the database. This caused by a transaction deadlock or serialization problems. SQLState 40

SQLNonTransientExceptions :

SQLDataException Indicates data errors, such as invalid arguments to functions, etc. SQLState 22

SQLIntegrityConstraintViolationException A foreign key, primary key, or unique key constraint was violated. SQLState 23

SQLInvalidAuthorizationSpecException Authorization credentials presented during authorization were not valid. SQLState 28

SQLNonTransientConnectionException A non-transient version of the connection exception, indicating some change in the connection's communications layer. SQLState 08

SQLSyntaxErrorException Indicates that an in-progress query has violated syntax rules.

JDBC-4 chained Excepion-getCause() and getNextException():

 JDBC 4's exceptions are also chained exceptions, and accessing the root cause of an exception can quickly help determine the source of a problem. To support iteration over an exception chain, SQLException defines a new getNextException() method. Invoking this method returns either the next Exception in the exception chain, or null, when the root of the hierarchy is reached. SQLException also supports the getCause() method, which may return a non-SQLException subtype as well (since the cause of an SQLException may be a non SQL-related problem). The following code example illustrates iterating through an SQLException chain:



 catch (SQLException e) {

while (ex != null) {

Throwable t = ex.getCause();

while (t != null) {

t = t.getCause();


 ex = ex.getNextException();



Popular posts from this blog

Spring MVC- Redirect from one controller to another

Integrating Struts2 with Spring Security using Custom Login Form