Search in sources :

Example 1 with CloseException

use of org.jdbi.v3.core.CloseException in project jdbi by jdbi.

the class StatementContext method close.

@Override
public void close() {
    SQLException exception = null;
    try {
        List<Cleanable> cleanables = new ArrayList<>(this.cleanables);
        this.cleanables.clear();
        Collections.reverse(cleanables);
        for (Cleanable cleanable : cleanables) {
            try {
                cleanable.close();
            } catch (SQLException e) {
                if (exception == null) {
                    exception = e;
                } else {
                    exception.addSuppressed(e);
                }
            }
        }
    } finally {
        if (exception != null) {
            throw new CloseException("Exception thrown while cleaning StatementContext", exception);
        }
    }
}
Also used : SQLException(java.sql.SQLException) CloseException(org.jdbi.v3.core.CloseException) ArrayList(java.util.ArrayList)

Example 2 with CloseException

use of org.jdbi.v3.core.CloseException in project jdbi by jdbi.

the class TestOnDemandSqlObject method testExceptionOnClose.

@Test(expected = TransactionException.class)
public void testExceptionOnClose() throws Exception {
    JdbiPlugin plugin = new JdbiPlugin() {

        @Override
        public Handle customizeHandle(Handle handle) {
            Handle h = spy(handle);
            when(h.createUpdate(anyString())).thenThrow(new TransactionException("connection reset"));
            doThrow(new CloseException("already closed", null)).when(h).close();
            return h;
        }
    };
    db.installPlugin(plugin);
    Spiffy s = db.onDemand(Spiffy.class);
    s.insert(1, "Tom");
}
Also used : TransactionException(org.jdbi.v3.core.transaction.TransactionException) CloseException(org.jdbi.v3.core.CloseException) JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

CloseException (org.jdbi.v3.core.CloseException)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Handle (org.jdbi.v3.core.Handle)1 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)1 TransactionException (org.jdbi.v3.core.transaction.TransactionException)1 Test (org.junit.Test)1