Search in sources :

Example 21 with Handle

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));
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) InvocationHandler(java.lang.reflect.InvocationHandler) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 22 with Handle

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);
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 23 with Handle

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);
    });
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 24 with Handle

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);
    });
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 25 with Handle

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);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Handle (org.jdbi.v3.core.Handle)134 Test (org.junit.Test)124 Jdbi (org.jdbi.v3.core.Jdbi)36 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)21 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Namespace (net.sourceforge.argparse4j.inf.Namespace)11 Test (org.junit.jupiter.api.Test)11 SQLException (java.sql.SQLException)10 Query (org.jdbi.v3.core.statement.Query)8 Map (java.util.Map)7 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Rule (org.junit.Rule)5 List (java.util.List)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ArrayList (java.util.ArrayList)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)3