Skip to content

Instantly share code, notes, and snippets.

@bigdragon1977
Created September 9, 2015 19:08
Show Gist options
  • Select an option

  • Save bigdragon1977/9f7e20b9ded67cfeb106 to your computer and use it in GitHub Desktop.

Select an option

Save bigdragon1977/9f7e20b9ded67cfeb106 to your computer and use it in GitHub Desktop.
HikariCP + Sql2o + log4jdbc-log4j2 + Wicket
@Singleton
public class DBCP implements IDBCP {
private final Sql2o sql2o;
public DBCP() {
String configPath =
getClass().getClassLoader().getResource("").getPath() + "hikari.properties";
sql2o =
new Sql2o(new HikariDataSource(new HikariConfig(filePath)), new PostgresQuirks());
}
@Override
public sql2o getSql2o() throws SQLException {
Return sql2o;
}
public class FooPage extends WebPage {
@Inject
private IDBCP dbcp;
public FooPage() {
// 書籍テーブルからISBNのリストを取得する想定.
// 実際の利用では 別クラスのメソッドの返値をModelなどでラッピングする.
try (Connection con = dbcp.getSql2o.open()) {
List<String> isbns = con
.createQuery("select isbn from books")
.executeScalarList(String.class);
}
// -- snip --
}
}
//https://github.com/google/guice/wiki/JustInTimeBindings
@ImplementedBy(DBCP.class)
public interface IDBCP {
public HikariDataSource getHikariDataSource() throws SQLException;
}
public class MyApplication extends WebApplication {
@Override
protected void init() {
super.init();
getComponentInstantiationListeners()
.add(new GuiceComponentInjector(this, getGuiceModule()));
//-- snip --
}
protected Module getGuiceModule() {
return new Module() {
@Override
public void configure(Binder binder) {
binder.bind(IDBCP.class).to(DBCP.class).asEagerSingleton();
}
};
}
//-- snip --
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment