|
| 1 | +package com.baeldung.hikaricp; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.util.Properties; |
| 7 | + |
| 8 | +import com.zaxxer.hikari.HikariConfig; |
| 9 | +import com.zaxxer.hikari.HikariDataSource; |
| 10 | + |
| 11 | +public class DataSource { |
| 12 | + |
| 13 | + private static HikariConfig config = new HikariConfig(); |
| 14 | + private static HikariDataSource ds; |
| 15 | + |
| 16 | + static { |
| 17 | +// config = new HikariConfig("datasource.properties"); |
| 18 | + |
| 19 | +// Properties props = new Properties(); |
| 20 | +// props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource"); |
| 21 | +// props.setProperty("dataSource.user", "postgres"); |
| 22 | +// props.setProperty("dataSource.password", "postgres"); |
| 23 | +// props.setProperty("dataSource.databaseName", "postgres"); |
| 24 | +// props.setProperty("dataSource.portNumber", "5432"); |
| 25 | +// props.setProperty("dataSource.serverName", "localhost"); |
| 26 | +// props.put("dataSource.logWriter", new PrintWriter(System.out)); |
| 27 | +// config = new HikariConfig(props); |
| 28 | + |
| 29 | + config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); |
| 30 | + config.setUsername("postgres"); |
| 31 | + config.setPassword("postgres"); |
| 32 | + config.addDataSourceProperty("cachePrepStmts", "true"); |
| 33 | + config.addDataSourceProperty("prepStmtCacheSize", "250"); |
| 34 | + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); |
| 35 | + ds = new HikariDataSource(config); |
| 36 | + |
| 37 | +// ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); |
| 38 | +// ds.setUsername("postgres"); |
| 39 | +// ds.setPassword("postgres"); |
| 40 | + } |
| 41 | + |
| 42 | + private DataSource() {} |
| 43 | + |
| 44 | + public static Connection getConnection() throws SQLException { |
| 45 | + return ds.getConnection(); |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments