/** * Performs a rollback on the <code>Connection</code> then closes it, * avoid closing if null and hide any SQLExceptions that occur. * * @param conn Connection to rollback. A null value is legal. * @since DbUtils 1.1 */ public static void rollbackAndCloseQuietly(Connection conn) { try { rollbackAndClose(conn); } catch (SQLException e) { // NOPMD // quiet } }
/** * Rolls back and closes a connection. * @param conn Connection * @throws JuDbException If the rollback and/or close fails */ public static void rollbackAndClose(Connection conn) throws JuDbException { try { log.debug("Rolling back and closing connection [ID=" + JuObjectUtils.getIdentityString(conn) + "]"); DbUtils.rollbackAndClose(conn); } catch (SQLException ex) { throw new JuDbException("Couldn't roll back and close connection", ex); } }
@Test public void rollbackAndClose() throws Exception { Connection mockConnection = mock(Connection.class); DbUtils.rollbackAndClose(mockConnection); verify(mockConnection).rollback(); verify(mockConnection).close(); }
@Test public void rollbackAndCloseNull() throws Exception { DbUtils.rollbackAndClose(null); }
@Test public void rollbackAndCloseWithException() throws Exception { Connection mockConnection = mock(Connection.class); doThrow(SQLException.class).when(mockConnection).rollback(); try { DbUtils.rollbackAndClose(mockConnection); fail("DbUtils.rollbackAndClose() swallowed SQLException!"); } catch (SQLException e) { // we expect this exeption } verify(mockConnection).rollback(); verify(mockConnection).close(); }