Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.pool;
 
 import static org.firebirdsql.ds.ReflectionHelper.findMethod;
 
 
Wrapper for prepared statements. This invocation handler notifies statement manager about closing prepared statement.

Author(s):
Roman Rokytskyy
 
 public class PooledPreparedStatementHandler implements InvocationHandler {
    
    
Flag whether check is performed in tolerant mode or not. In tolerant mode java.sql.SQLException is thrown, otherwise java.lang.IllegalStateException.
 
     private static final boolean TOLERANT_CHECK_MODE = false;
     
     private final static Method PREPARED_STATEMENT_CLOSE = findMethod(
         PreparedStatement.class"close"new Class[0]);
         
     private final static Method PREPARED_STATEMENT_GET_CONNECTION = findMethod(
         PreparedStatement.class"getConnection"new Class[0]);
         
     private final static Method PREPARED_STATEMENT_FORCE_CLOSE = findMethod(
         XCachablePreparedStatement.class"forceClose"new Class[0]);
 
     private final static Method PREPARED_STATEMENT_SET_CONNECTION = findMethod(
         XCachablePreparedStatement.class"setConnection"new Class[] {
             Connection.class});
 
     private final static Method PREPARED_STATEMENT_GET_ORIGINAL = findMethod(
         XCachablePreparedStatement.class"getOriginal"new Class[0]);
     
     private final static Method PREPARED_STATEMENT_IS_CACHED = findMethod(
             XCachablePreparedStatement.class"isCached"new Class[0]);
     
     private XPreparedStatementModel key;
     private XStatementManager owner;
     private Connection associatedConnection;
     private boolean cached;
     
     private boolean invalid;
     private ObjectCloseTraceException invalidateStackTrace;
        
    
Create instance of this class.

Parameters:
statement SQL statement to which prepared statement corresponds to.
preparedStatement prepared statement corresponding to statement.
owner instance of XConnection that created prepared statement.
 
             PreparedStatement preparedStatementXStatementManager owner,
             boolean cached) {
         
         this. = key;
         this. = preparedStatement;
         this. = owner;
         this. = false;
         this. = cached;
     }
    
    
Handle java.sql.Statement.close() call.

Parameters:
key XPreparedStatementModel to close
proxy dynamic proxy wrapping prepared statement.
Throws:
java.sql.SQLException if something went wrong.
    protected void handleStatementClose(XPreparedStatementModel keyObject proxy
        throws SQLException 
    {
        if ()
            throw new SQLException("Statement is already closed.");
            
        .statementClosed(keyproxy);
    }
    
    protected void handleForceClose() throws SQLException {
        .close();
         = null;
         = true;
    }
    
    protected boolean handleIsCached() throws SQLException {
        return ;
    }
        
    
Invoke method on a specified proxy. Here we check if method is a method java.sql.Statement.close(). If yes, we notify owner that this prepared statement was closed.

Parameters:
proxy proxy on which method is invoked.
method instance of java.lang.reflect.Method describing method being invoked.
args array with arguments.
Returns:
result of method invokation.
Throws:
java.lang.Throwable if invoked method threw an exception.
    public Object invoke(Object proxyMethod methodObject[] args
        throws Throwable 
    {
        checkCorrectness(method);
        
        try {
            if (method.equals()) {
                handleStatementClose(proxy);
                return .;
            } else
            if (method.equals()) {
                return ;
            } else
            if (method.equals()) {
                this. = (Connection)args[0];
                return .;
            } else
            if (method.equals()) {
                return ;
            } else
            if (method.equals()) {
                handleForceClose();
                return .;
            } else
            if (method.equals()) {
                return Boolean.valueOf(handleIsCached());
            } else {
                
                Object result = method.invoke(args);
                
                if (result instanceof ResultSet) {
                    ResultSetHandler handler = new ResultSetHandler(
                            (PreparedStatementproxy, (ResultSetresult);
                    result = Proxy.newProxyInstance(
                            getClass().getClassLoader(),
                            new Class[] { ResultSet.class}, handler);
                } 
                
                return result;
            }
            
        } catch(InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }
    
    
Check if method call is correct. If method was declared in java.sql.PreparedStatement class and there is no connection proxy associated with this statement (i.e. statement in in pool), exception is thrown (this situation usually happens if somebody tries to call method on prepared statement after it has been closed).

Parameters:
method method to check.
Throws:
java.sql.SQLException if statement has incorrect statement.
    protected void checkCorrectness(Method methodthrows SQLException {
        String message =  "Statemenet has illegal state because it "+
            "does not have any associated connection at this time. " + 
            "Usually this means that Statement.getConnection() " + 
            "method was called on a closed statement that currently " + 
            "lives in a statement pool. ";
        if ()
            message += "See the attached exception " +
                "for the information where the object was closed.";
        boolean incorrectState = 
            ( == null &&
             method.getDeclaringClass().equals(PreparedStatement.class)) ||
             ;
            
        if (incorrectState)
            if () {
                FBSQLException ex = new FBSQLException(message);
                if ()
                    ex.setNextException();
                throw ex;
            } else
                throw new IllegalStateException(message);
    }
New to GrepCode? Check out our FAQ X