use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class SqlObjectFactory method attach.
/**
* Create a sql object of the specified type bound to this handle. Any state changes to the handle, or the sql
* object, such as transaction status, closing it, etc, will apply to both the object and the handle.
*
* @param extensionType the type of sql object to create
* @param handle the Handle instance to attach ths sql object to
* @return the new sql object bound to this handle
*/
@Override
public <E> E attach(Class<E> extensionType, HandleSupplier handle) {
Map<Method, Handler> handlers = methodHandlersFor(extensionType, handle.getConfig(Handlers.class), handle.getConfig(HandlerDecorators.class));
ConfigRegistry instanceConfig = handle.getConfig().createCopy();
for (Class<?> iface : extensionType.getInterfaces()) {
forEachConfigurer(iface, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
}
forEachConfigurer(extensionType, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
InvocationHandler invocationHandler = createInvocationHandler(extensionType, instanceConfig, handlers, handle);
return extensionType.cast(Proxy.newProxyInstance(extensionType.getClassLoader(), new Class[] { extensionType }, invocationHandler));
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestJdbiFactoryBean method testFailsViaException.
@Test
public void testFailsViaException() throws Exception {
assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
service.inPropagationRequired(jdbi -> {
Handle h = JdbiUtil.getHandle(jdbi);
final int count = h.execute("insert into something (id, name) values (7, 'ignored')");
if (count == 1) {
throw new ForceRollback();
} else {
throw new RuntimeException("!ZABAK");
}
});
});
try (final Handle h = Jdbi.open(ds)) {
int count = h.createQuery("select count(*) from something").mapTo(int.class).findOnly();
assertThat(count).isEqualTo(0);
}
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestJdbiFactoryBean method testNested.
@Test
public void testNested() throws Exception {
assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
service.inPropagationRequired(outer -> {
final Handle h = JdbiUtil.getHandle(outer);
h.execute("insert into something (id, name) values (7, 'ignored')");
assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
service.inNested(inner -> {
final Handle h1 = JdbiUtil.getHandle(inner);
h1.execute("insert into something (id, name) values (8, 'ignored again')");
int count = h1.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
assertThat(count).isEqualTo(2);
throw new ForceRollback();
});
});
int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
assertThat(count).isEqualTo(1);
throw new ForceRollback();
});
});
service.inPropagationRequired(jdbi -> {
final Handle h = JdbiUtil.getHandle(jdbi);
int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
assertThat(count).isEqualTo(0);
});
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestJdbiFactoryBean method testRequiresNew.
@Test
public void testRequiresNew() throws Exception {
service.inPropagationRequired(outer -> {
final Handle h = JdbiUtil.getHandle(outer);
h.execute("insert into something (id, name) values (7, 'ignored')");
assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
service.inRequiresNewReadUncommitted(inner -> {
final Handle h1 = JdbiUtil.getHandle(inner);
int count = h1.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
assertThat(count).isEqualTo(1);
h1.execute("insert into something (id, name) values (8, 'ignored again')");
throw new ForceRollback();
});
});
int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
assertThat(count).isEqualTo(1);
});
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestMixinInterfaces method testGetHandle.
@Test
public void testGetHandle() throws Exception {
WithGetHandle g = handle.attach(WithGetHandle.class);
Handle h = g.getHandle();
assertThat(h).isSameAs(handle);
}
Aggregations