Created
September 9, 2015 19:08
-
-
Save bigdragon1977/9f7e20b9ded67cfeb106 to your computer and use it in GitHub Desktop.
HikariCP + Sql2o + log4jdbc-log4j2 + Wicket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 -- | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //https://github.com/google/guice/wiki/JustInTimeBindings | |
| @ImplementedBy(DBCP.class) | |
| public interface IDBCP { | |
| public HikariDataSource getHikariDataSource() throws SQLException; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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