Skip navigation links

Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1.5.0)

E13403-06


oracle.javatools.db.execute
Class ConnectionWrapper

java.lang.Object
  extended by oracle.javatools.db.execute.ConnectionWrapper


public class ConnectionWrapper
extends java.lang.Object

Class to wrap a process that uses a Database connection and can execute that process while recovering from a closed connection (by reconnecting). This is automatically used by the StatementWrapper and QueryWrapper so does not need to be used in addition to them.

Example usage 1: Retrieving the user name for the Connection

 final Database db = ...
 try
 {
   final ConnectionWrapper wrapper =
     new ConnectionWrapper( db, "Query username from connection metadata" ); // description should be translated
   final String userName = wrapper.call(
     wrapper.new SQLCallable<String>()
     {
       public String call() throws SQLException
       {
         return getConnection().getMetaData().getUserName();
       }
     } );
 }
 catch( SQLException sqe )
 {
   // process the exception
 }
 

Example usage 2: Setting the autocommit flag on the connection

 final Database db = ...
 try
 {
   final ConnectionWrapper wrapper =
     new ConnectionWrapper( db, "Set the auto commit flag on the Connection" ); // description should be translated
   final String userName = wrapper.run(
     wrapper.new SQLRunnable()
     {
       public String run() throws SQLException
       {
         getConnection().setAutoCommit( true );
       }
     } );
 }
 catch( SQLException sqe )
 {
   // process the exception
 }
 
Since:
11.1.1.4.0

Nested Class Summary
 class ConnectionWrapper.SQLCallable<T>
          Callable class that can throw a SQLException from its call method.
 class ConnectionWrapper.SQLRunnable
          Runnable class that can throw a SQLException from its run method.

 

Constructor Summary
ConnectionWrapper(Database db, java.lang.String desc)
          Creates a ConnectionWrapper using the given Database.

 

Method Summary
<T> T
call(ConnectionWrapper.SQLCallable<T> c)
          Calls the given SQLCallable.
 void run(ConnectionWrapper.SQLRunnable r)
          Runs the given SQLRunnable.

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Constructor Detail

ConnectionWrapper

public ConnectionWrapper(Database db,
                         java.lang.String desc)
Creates a ConnectionWrapper using the given Database.
Parameters:
db - the database to use the Connection from
desc - a translatable description of the nature of the process being executed. This will be wrapped into any DBException thrown if the process fails to indicate to the user what failed.

Method Detail

run

public void run(ConnectionWrapper.SQLRunnable r)
         throws DBException
Runs the given SQLRunnable. If a SQLException is encountered that is identifiable as a "closed connection" exception then the connection will be reconnected and the runnable re-run.
Parameters:
r - the runnable to run as a unit of work on the Connection
Throws:
DBException - if an error with the database occurs

call

public <T> T call(ConnectionWrapper.SQLCallable<T> c)
       throws DBException
Calls the given SQLCallable. If a SQLException is encountered that is identifiable as a "closed connection" exception then the connection will be reconnected and the callable re-called.
Parameters:
c - the callable to run as a unit of work on the Connection
Returns:
the result of the SQLCallable if no exception was encountered
Throws:
DBException - if an error with the database occurs

Skip navigation links

Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1.5.0)

E13403-06


Copyright © 1997, 2011, Oracle. All rights reserved.