Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring an Exception Handler

You can associate a single exception handling class with each session. This class must implement the `oracle.toplink.exceptions.ExceptionHandler` interface.

Table 77-11 summarizes which sessions support exception handler configuration.

Table 77-11 Session Support for Exception Handler Configuration

Session | Using TopLink Workbench | Using Java
--- | --- | ---
Server and Client Sessions |  |
Session Broker and Client Sessions |  |
Database Sessions |  |

For an example exception handler implementation, see "Using Java".

For more information, see "Exception Handlers".

## Using TopLink Workbench

To specify the exception handler class in a session, use this procedure:

1. Select a session in the Navigator. Its properties appear in the Editor.

2. Click the Options tab. The Options tab appears.

   Figure 77-6 Options Tab, Exception Handler field

   Description of "Figure 77-6 Options Tab, Exception Handler field"

Click Browse and select the exception handler class for this session.

## Using Java

Example 77-6 shows an example exception handler implementation. In this implementation, the exception handler always tries to reestablish the connection if it has been reset by peer, but only retries a query if it is an instance of `ReadQuery`. Note that this exception handler either returns the result of the reexecuted `ReadQuery` or throws an exception.

Example 77-6 Implementing an Exception Handler

    session.setExceptionHandler(
      new ExceptionHandler()
      {
        public Object handleException(RuntimeException exception)
        {
          if (exception instanceof DatabaseException)
          {
            DatabaseException dbex = (DatabaseException) exception;
            if ((dbex.getInternalException() instanceof SQLException) &&
               (((SQLException) dbex.getInternalException()).getErrorCode() == MyDriver.CONNECTION_RESET_BY_PEER))
            {
               dbex.getAccessor().reestablishConnection(dbex.getSession());
               if (dbex.getQuery() instanceof ReadQuery)
               {
                 return dbex.getSession().executeQuery(dbex.getQuery());
               }
               throw exception;
            }
          }
          throw exception;
        }
      }
    );

Note: Unhandled exceptions must be rethrown by the exception handler code.