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
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * 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.

Roman Rokytskyy
     private static final boolean CACHE_PREPARED_STATEMENTS = true;
     private static final boolean LOG_STATEMENT_IN_POOL = .;
     private static Logger logChannel = 
     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.

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.

connection instance of java.sql.Connection to associate with prepared statement.
free instance of java.sql.PreparedStatement.
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)
                            "Found no free prepared statements, " + 
                            "preparing new one.");
                    result = prepareStatement(,
                        .size() < );
                } else {
                    if ( &&  != null)
                            "Found free prepared statement in pool.");    
                    result = (XCachablePreparedStatement).pop();
            .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).

key instance of XPreparedStatementModel that will be used to prepare the statement.
cached true if statement should be cached, otherwise false
instance of XCachablePreparedStatement
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.

reference object to dereference.
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 = 
                if (statement.isCached()) {
                // release statement if it is not cached
                if (!statement.isCached())
                if ( &&  != null)
                    .info("Returned prepared statement to pool.");
            } else
        } 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 =
            } catch(InterruptedException ex) {
                // ignore
            } catch(SQLException 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);
            XCachablePreparedStatement item = 
            try {
            } catch(SQLException ex) {
        if (chain.hasException())
            throw chain.getException();
New to GrepCode? Check out our FAQ X