001package io.ebean.util;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006import java.sql.Connection;
007import java.sql.ResultSet;
008import java.sql.SQLException;
009import java.sql.Statement;
010
011/**
012 * Utility for closing raw Jdbc resources.
013 */
014public class JdbcClose {
015
016  private static final Logger logger = LoggerFactory.getLogger(JdbcClose.class);
017
018  /**
019   * Close the resultSet logging if an error occurs.
020   */
021  public static void close(Statement statement) {
022    try {
023      if (statement != null) {
024        statement.close();
025      }
026    } catch (SQLException e) {
027      logger.warn("Error closing statement", e);
028    }
029  }
030
031  /**
032   * Close the resultSet logging if an error occurs.
033   */
034  public static void close(ResultSet resultSet) {
035    try {
036      if (resultSet != null) {
037        resultSet.close();
038      }
039    } catch (SQLException e) {
040      logger.warn("Error closing resultSet", e);
041    }
042  }
043
044  /**
045   * Close the connection logging if an error occurs.
046   */
047  public static void close(Connection connection) {
048    try {
049      if (connection != null) {
050        connection.close();
051      }
052    } catch (SQLException e) {
053      logger.warn("Error closing connection", e);
054    }
055  }
056
057  /**
058   * Rollback the connection logging if an error occurs.
059   */
060  public static void rollback(Connection connection) {
061    try {
062      if (connection != null) {
063        connection.rollback();
064      }
065    } catch (SQLException e) {
066      logger.warn("Error on connection rollback", e);
067    }
068  }
069}