Java JDBC Collection Transaction commitAndClose(Connection connection)

Here you can find the source of commitAndClose(Connection connection)

Description

Commits a Connection then closes it, avoid closing if null.

License

Apache License

Parameter

Parameter Description
connection Connection to close.

Exception

Parameter Description
SQLException if a database access error occurs

Declaration

public static void commitAndClose(Connection connection) throws SQLException 

Method Source Code

//package com.java2s;
/**/* w w  w.  j  a  va  2s  .c  o m*/
 * Copyright 2014 www.migratebird.com
 *
 * 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.
 */

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**
     * Commits a <code>Connection</code> then closes it, avoid closing if null.
     *
     * @param connection Connection to close.
     * @throws SQLException if a database access error occurs
     */
    public static void commitAndClose(Connection connection) throws SQLException {
        if (connection != null) {
            try {
                if (!connection.getAutoCommit()) {
                    connection.commit();
                }
            } finally {
                connection.close();
            }
        }
    }

    /**
     * Close a <code>Connection</code>, avoid closing if null.
     *
     * @param conn Connection to close.
     * @throws SQLException if a database access error occurs
     */
    public static void close(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    /**
     * Close a <code>ResultSet</code>, avoid closing if null.
     *
     * @param rs ResultSet to close.
     * @throws SQLException if a database access error occurs
     */
    public static void close(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

    /**
     * Close a <code>Statement</code>, avoid closing if null.
     *
     * @param stmt Statement to close.
     * @throws SQLException if a database access error occurs
     */
    public static void close(Statement stmt) throws SQLException {
        if (stmt != null) {
            stmt.close();
        }
    }
}

Related

  1. commit(Connection conn)
  2. commit(Connection conn)
  3. commit(Connection connection)
  4. commitAndClose(Connection conn)
  5. commitAndClose(Connection conn)
  6. commitAndClose(Connection connection)
  7. commitEL(Connection conn)
  8. commitQuietly(Connection conn)
  9. commitTransaction(Connection connection)