Search in sources :

Example 1 with JdbiPlugin

use of org.jdbi.v3.core.spi.JdbiPlugin in project jdbi by jdbi.

the class TestPlugins method testCustomizeConnection.

@Test
public void testCustomizeConnection() throws Exception {
    Connection c = mock(Connection.class);
    dbRule.getJdbi().installPlugin(new JdbiPlugin() {

        @Override
        public Connection customizeConnection(Connection conn) {
            return c;
        }
    });
    assertThat(c).isSameAs(dbRule.getJdbi().open().getConnection());
}
Also used : JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) Connection(java.sql.Connection) Test(org.junit.Test)

Example 2 with JdbiPlugin

use of org.jdbi.v3.core.spi.JdbiPlugin in project jdbi by jdbi.

the class Jdbi method open.

/**
 * Obtain a Handle to the data source wrapped by this Jdbi instance.
 * You own this expensive resource and are required to close it or
 * risk leaks.  Using a {@code try-with-resources} block is recommended.
 *
 * @return an open Handle instance
 * @see #useHandle(HandleConsumer)
 * @see #withHandle(HandleCallback)
 */
public Handle open() {
    try {
        final long start = System.nanoTime();
        Connection conn = connectionFactory.openConnection();
        final long stop = System.nanoTime();
        for (JdbiPlugin p : plugins) {
            conn = p.customizeConnection(conn);
        }
        StatementBuilder cache = statementBuilderFactory.get().createStatementBuilder(conn);
        Handle h = new Handle(config.createCopy(), transactionhandler.get(), cache, conn);
        for (JdbiPlugin p : plugins) {
            h = p.customizeHandle(h);
        }
        LOG.trace("Jdbi [{}] obtain handle [{}] in {}ms", this, h, (stop - start) / 1000000L);
        return h;
    } catch (SQLException e) {
        throw new ConnectionException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) DefaultStatementBuilder(org.jdbi.v3.core.statement.DefaultStatementBuilder) StatementBuilder(org.jdbi.v3.core.statement.StatementBuilder) Connection(java.sql.Connection)

Example 3 with JdbiPlugin

use of org.jdbi.v3.core.spi.JdbiPlugin in project jdbi by jdbi.

the class TestPlugins method testCustomizeHandle.

@Test
public void testCustomizeHandle() throws Exception {
    Handle h = mock(Handle.class);
    dbRule.getJdbi().installPlugin(new JdbiPlugin() {

        @Override
        public Handle customizeHandle(Handle handle) {
            return h;
        }
    });
    assertThat(h).isSameAs(dbRule.getJdbi().open());
}
Also used : JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) Test(org.junit.Test)

Example 4 with JdbiPlugin

use of org.jdbi.v3.core.spi.JdbiPlugin 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

JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)4 Test (org.junit.Test)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)1 CloseException (org.jdbi.v3.core.CloseException)1 Handle (org.jdbi.v3.core.Handle)1 DefaultStatementBuilder (org.jdbi.v3.core.statement.DefaultStatementBuilder)1 StatementBuilder (org.jdbi.v3.core.statement.StatementBuilder)1 TransactionException (org.jdbi.v3.core.transaction.TransactionException)1