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 java.sql.*;
 import java.util.*;
 
This class represents a cache of prepared statements corresponding to one SQL statement. Instance of this class automatically prepares new java.sql.PreparedStatement instance if no free statements are available.

Author(s):
Roman Rokytskyy
 
     
     private static final boolean CACHE_PREPARED_STATEMENTS = true;
 
     private static final boolean LOG_STATEMENT_IN_POOL = .;
 
     private static Logger logChannel = 
         LoggerFactory.getLogger(XPreparedStatementCache.classfalse);
 
     private XStatementManager owner;
     private BlockingStack freeReferences = new BlockingStack();
     private HashMap workingReferences = new HashMap();
     
     private XPreparedStatementModel key;
     private int maxSize;
    
    
Create instance of this class for the specified SQL statement and result set type and concurrency.

Parameters:
owner instance of XStatementManager that will prepare new statements.
key key of the prepared statement.
maxSize maximum pool size.
 
             XPreparedStatementModel keyint maxSize) {
         
         this. = owner;
         this. = maxSize;
         
         if (key == null)
             throw new NullPointerException("Null objects cannot be guarded.");
 
         this. = key;
     }

    
Reference prepared statement. This method checks prepared statement out of the pool. If no free statement is available, it prepares new one.

Parameters:
connection instance of java.sql.Connection to associate with prepared statement.
Returns:
free instance of java.sql.PreparedStatement.
Throws:
java.sql.SQLException if prepared statement cannot be obtained from the pool.
 
     synchronized XCachablePreparedStatement take(Connection connection
         throws SQLException 
     {
 
         if ( == null)
             throw new IllegalStateException(
                 "This reference guard was already destroyed.");
 
         try {
             XCachablePreparedStatement result;
 
             if (!)
                result = prepareStatement(false);
            else {
                if (.isEmpty()) {
                    if ( &&  != null)
                        .info(
                            "Found no free prepared statements, " + 
                            "preparing new one.");
                    result = prepareStatement(,
                        .size() < );
                } else {
                    if ( &&  != null)
                        .info(
                            "Found free prepared statement in pool.");    
                    result = (XCachablePreparedStatement).pop();
                }
            }
            
            result.setConnection(connection);
            
            .put(result.getOriginal(), result);
            return result;
        } catch(InterruptedException iex) {
            throw new SQLException("Cannot prepare SQL statement in pool");
        }
    }
    
    
Prepare the SQL statement. This method checks the type of the statement (i.e. whether generated keys should be returned or not).

Parameters:
key instance of XPreparedStatementModel that will be used to prepare the statement.
cached true if statement should be cached, otherwise false
Returns:
instance of XCachablePreparedStatement
Throws:
java.sql.SQLException if statement cannot be prepared.
            XPreparedStatementModel keyboolean cachedthrows SQLException {
        
        return .prepareStatement(keycached);
    }

    
Dereference object. If reference is the same as guarded object, counter is simply decreased, otherwise exception is thrown.

Parameters:
reference object to dereference.
Throws:
java.lang.IllegalArgumentException if reference is not the same as guarded object.
java.sql.SQLException if prepared statement cannot be returned into the pool.
    synchronized void put(Object referencethrows SQLException {
        if (reference == null)
            throw new NullPointerException();
        try {
            if () {
                
                XCachablePreparedStatement statement = 
                    (XCachablePreparedStatement)reference;
                
                if (statement.isCached()) {
                    statement.setConnection(null);
                    .push(reference);
                } 
                
                .remove(statement.getOriginal());
                // release statement if it is not cached
                if (!statement.isCached())
                    statement.forceClose();
                
                if ( &&  != null)
                    .info("Returned prepared statement to pool.");
            } else
                ((XCachablePreparedStatement)reference).forceClose();
                
        } catch(InterruptedException ex) {
            .warn("Could not put statement back to pool."ex);
        }
    }

    
Invalidate this pool. After invoking this method no other method can be invoked.
    synchronized void invalidate() throws SQLException {
         = null;
        
        // clear free references
        SQLExceptionChainBuilder chain = new SQLExceptionChainBuilder();
        while(!.isEmpty()) {
            try {
                XCachablePreparedStatement result =
                    (XCachablePreparedStatement).pop();
                result.forceClose();
            } catch(InterruptedException ex) {
                // ignore
            } catch(SQLException ex) {
                chain.append(ex);
            }
        }
        
        // clear working references, even they're currently in use
        // since we are invalidating the pool and there's no way back
        Iterator iter = .entrySet().iterator();
        while(iter.hasNext()) {
            Map.Entry entry = (Map.Entry)iter.next();
            
            XCachablePreparedStatement item = 
                (XCachablePreparedStatement)entry.getValue();
                
            try {
                item.forceClose();
            } catch(SQLException ex) {
                chain.append(ex);
            }
        }
        .clear();
        
        if (chain.hasException())
            throw chain.getException();
    }
New to GrepCode? Check out our FAQ X