Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.skife.jdbi.v2;
 
 
Resource management for JDBI. Cleanables can be registered on a SQL statement and they get cleaned up when the statement finishes or (in the case of a ResultIterator), the object representing the results is closed. Resources managed by JDBI are java.sql.ResultSet, java.sql.Statement, Handle and org.skife.jdbi.v2.tweak.StatementBuilder for historical reasons.
    static Cleanable forResultSet(final ResultSet rs) {
        return new Cleanable() {
            public void cleanup() throws SQLException {
                if (rs != null) {
                    rs.close();
                }
            }
        };
    }
    static Cleanable forStatement(final Statement stmt) {
        return new Cleanable() {
            public void cleanup() throws SQLException {
                if (stmt != null) {
                    stmt.close();
                }
            }
        };
    }
    static Cleanable forHandle(final Handle handlefinal TransactionState state) {
        return new Cleanable() {
            public void cleanup() throws SQLException {
                if (handle != null) {
                    if (handle.isInTransaction()) {
                        if (state == .) {
                            handle.commit();
                        }
                        else {
                            handle.rollback();
                        }
                    }
                    handle.close();
                }
            }
        };
    }

    
In the SQLStatement derived classes, the java.sql.Statement is not managed directly but through the org.skife.jdbi.v2.tweak.StatementBuilder, which allows the CachingStatementBuilder to hook in and provide java.sql.PreparedStatement caching.
    static class StatementBuilderCleanable implements Cleanable
    {
        private final StatementBuilder statementBuilder;
        private final Connection conn;
        private final String sql;
        private final Statement stmt;
        StatementBuilderCleanable(final StatementBuilder statementBuilder,
                                  final Connection conn,
                                  final String sql,
                                  final Statement stmt)
        {
            this. = statementBuilder;
            this. = conn;
            this. = sql;
            this. = stmt;
        }
        public void cleanup() throws SQLException
        {
            if ( != null) {
                .close();
            }
        }
    }
New to GrepCode? Check out our FAQ X