Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004 - 2011 Brian McCallister
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.skife.jdbi.v2;
 
 
 import java.util.Map;

A StatementBuilder which decorates another StatementBuilder and caches

Deprecated:
This should be done in the JDBC driver, not here
 
 public class CachingStatementBuilder implements StatementBuilder
 {
     private Map<StringPreparedStatementcache = new HashMap<StringPreparedStatement>();
     private final StatementBuilder builder;

    
Create a new CachingStatementBuilder which decorates the one passed in.

Parameters:
builder The StatementBuilder used to actual PreparedStatement creation
 
     public CachingStatementBuilder(StatementBuilder builder) {
         this. = builder;
     }

    
Return either a cached PreparedStatement or a new one which has just been added to the cache

Returns:
A new, or cached, PreparedStatement
 
     public PreparedStatement create(Connection connString sqlStatementContext ctxthrows SQLException
     {
         if (.containsKey(sql)) {
             PreparedStatement cached = .get(sql);
             cached.clearParameters();
             return cached;
         }
 
         PreparedStatement stmt = .create(connsqlctx);
         .put(sqlstmt);
         return stmt;
     }

    
NOOP, statements will be closed when the handle is closed
 
     public void close(Connection connString sqlStatement stmtthrows SQLException
     {
     }

    
Iterate over all cached statements and ask the wrapped StatementBuilder to close each one.
 
     public void close(Connection conn)
     {
         for (Map.Entry<String,PreparedStatementstatement : .entrySet()) {
             try {
                 .close(connstatement.getKey(), statement.getValue());
             }
             catch (SQLException e) {
                 // nothing we can do!
             }
         }
     }
 
 	{
 		if (.containsKey(sql)) {
 		    CallableStatement cached = (CallableStatement.get(sql);
 		    cached.clearParameters();
 		    return cached;
 		}
 
 		CallableStatement stmt = .createCall(connsqlctx);
 		.put(sqlstmt);
 		return stmt;
 	}
New to GrepCode? Check out our FAQ X