--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public List<String> getNames(final int minCredits, final String type) {
final DBI dbi = new DBI("jdbc:h2:mem:test");
try (Handle h = dbi.open()) {
String sql = "select name from Customers where credits >= :minCredits";
if (type != null) {
sql += " and type = :type";
}
final Query<Map<String, Object>> q = h.createQuery(sql);
q.bind("minCredits", minCredits);
if (type != null) {
q.bind("type", type);
}
final List<String> names = q.map(StringColumnMapper.INSTANCE).list();
return names;
}
}
org.skife.jdbi.v2.sqlobject.mixins.GetHandlecode here...
Then, I made my class implement GetHandle, by doing that, I can then do
this.getHandle();
public class MyDb {
/** * A functional interface that defines a callback that takes a database handle. */ @FunctionalInterface public interface MyHandleCallback { /** * This callback will be invoked with an open Handle. The handle will be closed when this * callback returns. Any exception thrown will be wrapped in a * {@link org.skife.jdbi.v2.exceptions.CallbackFailedException} * * @param handle Handle to be used only within scope of this callback * * @throws Exception will result in a * {@link org.skife.jdbi.v2.exceptions.CallbackFailedException} wrapping the * exception being thrown */ void withHandle(Handle handle) throws Exception; }
private static DBI _jdbi = null;
/** * Execute a single database request, defined in a callback function. * * @param callback The database callback function, which takes a * {@code org.skife.jdbi.v2.Handle} as its argument and returns an object of type * {@code ReturnType} * @return The return value of the callback. */ public static <ReturnType> ReturnType exec(final HandleCallback<ReturnType> callback) { return _jdbi.withHandle(callback); }
/** * Execute one or more statements, defined in a callback function. * * @param callback The database callback function, which takes a * {@code org.skife.jdbi.v2.Handle} as its argument and returns nothing. */ public static void execN(final MyHandleCallback callback) { try (final Handle h = _jdbi.open()) { callback.withHandle(h); } catch (final RuntimeException e) { throw e; } catch (final Exception e) { throw new CallbackFailedException(e); } }
/** * Starts the database connection manager. * * @param config The Dropwizard runtime configuration. * @param environment The Dropwizard runtime environment. */ public static void start(final MyConfiguration config, final Environment environment) {
final DataSourceFactory dsf = config.database;
/* * (Note: If you need to do database initialization or schema migration do it here.) */
_jdbi = new DBIFactory().build(environment, dsf, "database"); }}
public class MyApplication extends Application<MyConfiguration> {
// ...
@Override public void run(final MyConfiguration config, final Environment environment) {
// ...
MyDb.start(config, environment);
// ... }}
MyDb.exec(h -> h.attach(MyDbDocuments.class)
.getDocsForReqNewestVersion(project.getProjectId(), req.getReqId()))
.stream()
.forEach(doc -> _visitor.visit(doc, null));
MyDb.execN(h -> {
_srsHighestVersion = h.attach(MyDbLocalDocs.class)
.getHighestVersionNumberOfLocalDoc(project.getProjectId(), MyDocType.SRS);
_srsDoc = h.attach(MyDbLocalDocs.class)
.getOneLocalDocNewestVersion(project.getProjectId(), MyDocType.SRS);
});