Advertisement
NLinker

HikariCP data source and usage

Nov 29th, 2016
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. public class HikariCPFactory {
  2.     private static final Logger LOGGER = LoggerFactory.getLogger(HikariCPFactory.class);
  3.  
  4.     @Override
  5.     public DataSource apply(Config config) {
  6.         LOGGER.debug("ConnectionPool initializing...");
  7.         HikariConfig hc = new HikariConfig();
  8.         // optional
  9.         if (config.hasPath("jdbc.driver")) {
  10.             hc.setDriverClassName(config.getString("jdbc.driver"));
  11.         } else {
  12.             hc.setDriverClassName("org.postgresql.Driver");
  13.         }
  14.         hc.setJdbcUrl(databaseUrl(config));
  15.         hc.setUsername(config.getString("jdbc.username"));
  16.         hc.setPassword(config.getString("jdbc.password"));
  17.  
  18.         // init cp specific properties
  19.         hc.setInitializationFailFast(true);
  20.         hc.setConnectionTestQuery("SELECT 1");
  21.         if (config.hasPath("hikaricp")) {
  22.             // enumerate all properties
  23.             Config config0 = config.getConfig("hikaricp");
  24.             Set<Map.Entry<String, ConfigValue>> set = config0.entrySet();
  25.             for (Map.Entry<String, ConfigValue> entry : set) {
  26.                 hc.addDataSourceProperty(entry.getKey(), entry.getValue());
  27.             }
  28.         }
  29.         LOGGER.debug("ConnectionPool initialized {}", hc);
  30.         return new HikariDataSource(hc);
  31.     }
  32.  
  33.     @SuppressWarnings("Duplicates")
  34.     public static String databaseUrl(final Config config) {
  35.         String protocol = config.getString("jdbc.protocol");
  36.         String host = config.getString("jdbc.host");
  37.         int port = config.getInt("jdbc.port");
  38.         String database = config.getString("jdbc.database");
  39.         return protocol + "//" + host + ":" + port + "/" + database;
  40.     }
  41. }
  42.  
  43. /// usage
  44.  
  45.         HikariCPFactory hcp = new HikariCPFactory();
  46.         dsApple = hcp.apply(fallback(config, "apple"));
  47.         dsSpotify = hcp.apply(fallback(config, "spotify"));
  48.  
  49. /// where
  50.     public static Config fallback(Config config, String namespace) {
  51.         // we need to be able to get, for example, "spotify.jdbc.username"
  52.         // when we have defined "jdbc.username" only
  53.         return config.getConfig(namespace)
  54.             .withFallback(config);
  55.     }
  56.  
  57. /// config
  58. jdbc {
  59.   driver = org.postgresql.Driver
  60.   protocol = "jdbc:postgresql:"
  61.   host = localhost
  62.   port = 5432
  63.   username = "postgres"
  64.   password = "postgres"
  65.   database = my_db
  66. }
  67. spotify {
  68.   jdbc.host = "cloud.us-west-2.rds.amazonaws.com"
  69.   jdbc.database = spotify_db
  70. }
  71. apple {
  72.   jdbc.host = "cloud.us-west-2.rds.amazonaws.com"
  73.   jdbc.database = apple_db
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement