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());
}
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);
}
}
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());
}
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");
}
Aggregations