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.List;
 import java.util.Map;
 
 class BasicHandle implements Handle
 {
 
     private StatementLocator  statementLocator;
     private SQLLog            log;
     private TimingCollector   timingCollector;
     private StatementBuilder  statementBuilder;
 
     private boolean closed = false;
 
     private final Map<StringObject>      globalStatementAttributes;
     private final MappingRegistry          mappingRegistry;
     private final Foreman                  foreman;
     private final TransactionHandler       transactions;
     private final Connection               connection;
 
 
     BasicHandle(TransactionHandler transactions,
                 StatementLocator statementLocator,
                 StatementBuilder preparedStatementCache,
                 StatementRewriter statementRewriter,
                 Connection connection,
                 Map<StringObjectglobalStatementAttributes,
                 SQLLog log,
                 TimingCollector timingCollector,
                 MappingRegistry mappingRegistry,
                 Foreman foreman,
                 ContainerFactoryRegistry containerFactoryRegistry)
     {
         this. = preparedStatementCache;
         this. = statementRewriter;
         this. = transactions;
         this. = connection;
         this. = statementLocator;
         this. = log;
         this. = timingCollector;
         this. = mappingRegistry;
         this. = foreman;
         this. = new HashMap<StringObject>();
         this..putAll(globalStatementAttributes);
         this. = containerFactoryRegistry.createChild();
     }
 
     public Query<Map<StringObject>> createQuery(String sql)
     {
         return new Query<Map<StringObject>>(new Binding(),
                                               new DefaultMapper(),
                                               ,
                                               ,
                                               this,
                                               ,
                                               sql,
                                               new ConcreteStatementContext(),
                                               ,
                                               ,
                                               Collections.<StatementCustomizer>emptyList(),
                                               new MappingRegistry(),
                                              .createChild(),
                                              .createChild());
    }

    
Get the JDBC Connection this Handle uses

Returns:
the JDBC Connection this Handle uses
    public Connection getConnection()
    {
        return this.;
    }
    public void close()
    {
        if (!) {
            .close(getConnection());
            try {
                .close();
            }
            catch (SQLException e) {
                throw new UnableToCloseResourceException("Unable to close Connection"e);
            }
            finally {
                .logReleaseHandle(this);
                 = true;
            }
        }
    }
    boolean isClosed()
    {
        return ;
    }
    public void define(String keyObject value)
    {
        this..put(keyvalue);
    }

    
Start a transaction
    public Handle begin()
    {
        .begin(this);
        .logBeginTransaction(this);
        return this;
    }

    
Commit a transaction
    public Handle commit()
    {
        final long start = System.nanoTime();
        .commit(this);
        .logCommitTransaction((System.nanoTime() - start) / 1000000L, this);
        return this;
    }

    
Rollback a transaction
    public Handle rollback()
    {
        final long start = System.nanoTime();
        .rollback(this);
        .logRollbackTransaction((System.nanoTime() - start) / 1000000L, this);
        return this;
    }

    
Create a transaction checkpoint (savepoint in JDBC terminology) with the name provided.

Parameters:
name The name of the checkpoint
Returns:
The same handle
    public Handle checkpoint(String name)
    {
        .checkpoint(thisname);
        .logCheckpointTransaction(thisname);
        return this;
    }

    
Release the named checkpoint, making rollback to it not possible.

Returns:
The same handle
    public Handle release(String checkpointName)
    {
        .release(thischeckpointName);
        .logReleaseCheckpointTransaction(thischeckpointName);
        return this;
    }
    public void setStatementBuilder(StatementBuilder builder)
    {
        this. = builder;
    }
    public void setSQLLog(SQLLog log)
    {
        this. = log;
    }
    public void setTimingCollector(final TimingCollector timingCollector)
    {
        if (timingCollector == null) {
            this. = .;
        }
        else {
            this. = timingCollector;
        }
    }


    
Rollback a transaction to a named checkpoint

Parameters:
checkpointName the name of the checkpoint, previously declared with Handle.checkpoint
    public Handle rollback(String checkpointName)
    {
        final long start = System.nanoTime();
        .rollback(thischeckpointName);
        .logRollbackToCheckpoint((System.nanoTime() - start) / 1000000L, thischeckpointName);
        return this;
    }
    public boolean isInTransaction()
    {
        return .isInTransaction(this);
    }
    public Update createStatement(String sql)
    {
        return new Update(this,
                          ,
                          ,
                          ,
                          sql,
                          new ConcreteStatementContext(),
                          ,
                          ,
                          ,
                          );
    }
    public Call createCall(String sql)
    {
        return new Call(this,
                        ,
                        ,
                        ,
                        sql,
                        new ConcreteStatementContext(),
                        ,
                        ,
                        Collections.<StatementCustomizer>emptyList(),
                        ,
                        );
    }
    public int insert(String sqlObject... args)
    {
        return update(sqlargs);
    }
    public int update(String sqlObject... args)
    {
        Update stmt = createStatement(sql);
        int position = 0;
        for (Object arg : args) {
            stmt.bind(position++, arg);
        }
        return stmt.execute();
    }
    public PreparedBatch prepareBatch(String sql)
    {
        return new PreparedBatch(,
                                 ,
                                 this,
                                 ,
                                 sql,
                                 new ConcreteStatementContext(this.),
                                 ,
                                 ,
                                 Collections.<StatementCustomizer>emptyList(),
                                 ,
                                 );
    }
    public Batch createBatch()
    {
        return new Batch(this.,
                         this.,
                         ,
                         ,
                         ,
                         .createChild());
    }
    public <ReturnType> ReturnType inTransaction(TransactionCallback<ReturnType> callback)
    {
        return .inTransaction(thiscallback);
    }
    public <ReturnType> ReturnType inTransaction(TransactionIsolationLevel level,
                                                 TransactionCallback<ReturnType> callback)
    {
        final TransactionIsolationLevel initial = getTransactionIsolationLevel();
        boolean failed = true;
        try {
            setTransactionIsolation(level);
            ReturnType result = .inTransaction(thislevelcallback);
            failed = false;
            return result;
        }
        finally {
            try {
                setTransactionIsolation(initial);
            }
            catch (RuntimeException e) {
                if (! failed) {
                    throw e;
                }
                // Ignore, there was already an exceptional condition and we don't want to clobber it.
            }
        }
    }
    public List<Map<StringObject>> select(String sqlObject... args)
    {
        Query<Map<StringObject>> query = this.createQuery(sql);
        int position = 0;
        for (Object arg : args) {
            query.bind(position++, arg);
        }
        return query.list();
    }
    public void setStatementLocator(StatementLocator locator)
    {
        this. = locator;
    }
    public void setStatementRewriter(StatementRewriter rewriter)
    {
        this. = rewriter;
    }
    public Script createScript(String name)
    {
        return new Script(thisname);
    }
    public void execute(String sqlObject... args)
    {
        this.update(sqlargs);
    }
    public void registerMapper(ResultSetMapper mapper)
    {
        .add(mapper);
    }
    public void registerMapper(ResultSetMapperFactory factory)
    {
        .add(factory);
    }
    public <SqlObjectType> SqlObjectType attach(Class<SqlObjectType> sqlObjectType)
    {
        return SqlObjectBuilder.attach(thissqlObjectType);
    }
    {
        setTransactionIsolation(level.intValue());
    }
    public void setTransactionIsolation(int level)
    {
        try {
            .setTransactionIsolation(level);
        }
        catch (SQLException e) {
            throw new UnableToManipulateTransactionIsolationLevelException(levele);
        }
    }
    {
        try {
            return TransactionIsolationLevel.valueOf(.getTransactionIsolation());
        }
        catch (SQLException e) {
            throw new UnableToManipulateTransactionIsolationLevelException("unable to access current setting"e);
        }
    }
    public void registerArgumentFactory(ArgumentFactory argumentFactory)
    {
        this..register(argumentFactory);
    }
    public void registerContainerFactory(ContainerFactory<?> factory)
    {
        this..register(factory);
    }
New to GrepCode? Check out our FAQ X