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