Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: StatementHandler.java 56967 2012-04-30 11:59:06Z mrotteveel $
   * 
   * Firebird Open Source J2EE Connector - JDBC Driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.ds;
 
 
 
 import static org.firebirdsql.ds.ReflectionHelper.*;

InvocationHandler for statements.

Using an InvocationHandler together with a Proxy removes the need to create wrappers for every individual JDBC version.

Author(s):
Mark Rotteveel
Since:
2.2
 
 class StatementHandler implements InvocationHandler {
 
     private final PooledConnectionHandler owner;
     private volatile Statement stmt;
     private volatile Statement proxy;

    
Constructor for StatementHandler.

Parameters:
owner The PooledConnectionHandler which owns the Statement
stmt Statement to proxy
 
         this. = owner;
         this. = stmt;
 
          = (Statement) Proxy.newProxyInstance(getClass().getClassLoader(),
                 getAllInterfaces(stmt.getClass()), this);
     }
 
     public Object invoke(Object proxyMethod methodObject[] argsthrows Throwable {
         // Methods from object
         if (method.equals()) {
             return "Proxy for " + ;
         }
         if (method.equals()) {
             // Using parameter proxy (and not field) on purpose as field is
             // nulled after closing
             return Boolean.valueOf(proxy == args[0]);
         }
         if (method.equals()) {
             // Using parameter proxy (and not field) on purpose as field is
             // nulled after closing
             return Integer.valueOf(System.identityHashCode(proxy));
         }
         // Other methods from object
         if (method.getDeclaringClass().equals(Object.class)) {
             try {
                 return method.invoke(args);
             } catch (InvocationTargetException e) {
                 throw e.getTargetException();
             }
         }
 
         // Methods of statement and subinterfaces
         if (method.equals() || method.equals()) {
             return Boolean.valueOf(isClosed());
         }
         if (isClosed() && !method.equals()) {
             throw new FBSQLException("Statement already closed",
                     .);
         }
         
        try {
            if (method.equals()) {
                if (!isClosed()) {
                    handleClose();
                }
                return null;
            }
            if (method.equals()) {
                // Ensure we do not leak the physical connection by returning the proxy
                return .getProxy();
            }
            // All other methods
            return method.invoke(args);
        } catch (InvocationTargetException ite) {
            Throwable inner = ite.getTargetException();
            if (inner instanceof SQLException) {
                .statementErrorOccurred(this, (SQLExceptioninner);
            }
            throw inner;
        } catch (SQLException se) {
            .statementErrorOccurred(thisse);
            throw se;
        }
    }

    

Returns:
Proxy for the Statement object
    protected Statement getProxy() {
        return ;
    }

    
Handle java.sql.Statement.close() method. This method closes the wrapped Statement and notifies the owner it can forget the statement.

Throws:
java.sql.SQLException If closing the statement threw an Exception
    private void handleClose() throws SQLException {
        if (isClosed()) {
            return;
        }
        try {
            .close();
        } finally {
            .forgetStatement(this);
             = null;
             = null;
        }
    }

    
Closes this StatementHandler

    protected void close() throws SQLException {
        handleClose();
    }

    

Returns:
true when this handler is closed
    public boolean isClosed() {
        return  == null ||  == null;
    }
    // Statement methods
    private final static Method STATEMENT_IS_CLOSED = findMethod(Statement.class"isClosed",
            new Class[0]);
    private final static Method FIREBIRDSTATEMENT_IS_CLOSED = findMethod(FirebirdStatement.class"isClosed"
            new Class[0]);
    private final static Method STATEMENT_CLOSE = findMethod(Statement.class"close"new Class[0]);
    private final static Method GET_CONNECTION = findMethod(Statement.class"getConnection"new Class[0]);
    // Object Methods
    private final static Method TO_STRING = findMethod(Object.class"toString"new Class[0]);
    private final static Method EQUALS = findMethod(Object.class"equals",
            new Class[] { Object.class });
    private final static Method HASH_CODE = findMethod(Object.class"hashCode"new Class[0]);
New to GrepCode? Check out our FAQ X