Java JDBC Collection Transaction commitAndClose(Connection connection)

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

Description

Commit and close.

License

Apache License

Parameter

Parameter Description
connection a parameter

Exception

Parameter Description
SQLException an exception

Declaration

public static void commitAndClose(Connection connection) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;

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

public class Main {
    /**/*from www.j a  v a 2  s .  c o  m*/
     * Commit and close.
     * 
     * @param connection
     * @throws SQLException
     */
    public static void commitAndClose(Connection connection) throws SQLException {
        try {
            commit(connection);
        } finally {
            close(connection);
        }
    }

    /**
     * Commits changes that exists in the {@link Connection}.
     * 
     * @param connection
     * @throws SQLException
     */
    public static void commit(Connection connection) throws SQLException {
        if (connection != null && !connection.getAutoCommit()) {
            connection.commit();
        }
    }

    /**
     * Closes a {@link Connection}.
     * 
     * @param connection
     * @throws SQLException
     */
    public static void close(Connection connection) throws SQLException {
        if (connection != null) {
            connection.close();
            connection = null;
        }
    }

    /**
     * Closes a {@link Statement}/{@link PreparedStatement}/{@link CallableStatement}.
     * 
     * @param statement
     * @throws SQLException
     */
    public static void close(Statement statement) throws SQLException {
        if (statement != null) {
            statement.close();
            statement = null;
        }
    }

    /**
     * Closes a {@link ResultSet}.
     * 
     * @param resultSet
     * @throws SQLException
     */
    public static void close(ResultSet resultSet) throws SQLException {
        if (resultSet != null) {
            resultSet.close();
            resultSet = null;
        }
    }
}

Related

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