Skip to content

Commit d46af17

Browse files
AbhinabKanrarzhendrikse
authored andcommitted
HikariCP (eugenp#1851)
* jvm log forging * jvm log forging * jvm log forging * log forging * adding hikariCP module * try-with-resources * adding employee use case * moving HikariCP to libraries
1 parent 0c973f4 commit d46af17

File tree

7 files changed

+235
-13
lines changed

7 files changed

+235
-13
lines changed

core-java/pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,6 @@
178178
<version>2.1.0.1</version>
179179
</dependency>
180180

181-
<dependency>
182-
<groupId>com.zaxxer</groupId>
183-
<artifactId>HikariCP</artifactId>
184-
<version>2.6.1</version>
185-
<scope>compile</scope>
186-
</dependency>
187-
188-
<dependency>
189-
<groupId>org.postgresql</groupId>
190-
<artifactId>postgresql</artifactId>
191-
<version>42.0.0</version>
192-
</dependency>
193-
194181
</dependencies>
195182

196183
<build>

libraries/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@
231231
<artifactId>datanucleus-xml</artifactId>
232232
<version>5.0.0-release</version>
233233
</dependency>
234+
<dependency>
235+
<groupId>com.zaxxer</groupId>
236+
<artifactId>HikariCP</artifactId>
237+
<version>2.6.1</version>
238+
<scope>compile</scope>
239+
</dependency>
240+
<dependency>
241+
<groupId>org.postgresql</groupId>
242+
<artifactId>postgresql</artifactId>
243+
<version>42.0.0</version>
244+
</dependency>
234245
</dependencies>
235246

236247
<properties>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.hikaricp;
2+
3+
import java.sql.Date;
4+
5+
public class Employee {
6+
7+
private int empNo;
8+
private String ename;
9+
private String job;
10+
private int mgr;
11+
private Date hiredate;
12+
private int sal;
13+
private int comm;
14+
private int deptno;
15+
16+
public int getEmpNo() {
17+
return empNo;
18+
}
19+
public void setEmpNo(int empNo) {
20+
this.empNo = empNo;
21+
}
22+
public String getEname() {
23+
return ename;
24+
}
25+
public void setEname(String ename) {
26+
this.ename = ename;
27+
}
28+
29+
public String getJob() {
30+
return job;
31+
}
32+
public void setJob(String job) {
33+
this.job = job;
34+
}
35+
public int getMgr() {
36+
return mgr;
37+
}
38+
public void setMgr(int mgr) {
39+
this.mgr = mgr;
40+
}
41+
public Date getHiredate() {
42+
return hiredate;
43+
}
44+
public void setHiredate(Date hiredate) {
45+
this.hiredate = hiredate;
46+
}
47+
public int getSal() {
48+
return sal;
49+
}
50+
public void setSal(int sal) {
51+
this.sal = sal;
52+
}
53+
public int getComm() {
54+
return comm;
55+
}
56+
public void setComm(int comm) {
57+
this.comm = comm;
58+
}
59+
public int getDeptno() {
60+
return deptno;
61+
}
62+
public void setDeptno(int deptno) {
63+
this.deptno = deptno;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return "Employee [empNo=" + empNo + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate="
69+
+ hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
70+
}
71+
72+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hikaricp;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class HikariCPDemo {
11+
12+
public static List<Employee> fetchData() {
13+
final String SQL_QUERY = "select * from emp";
14+
List<Employee> employees = null;
15+
try (Connection con = DataSource.getConnection();
16+
PreparedStatement pst = con.prepareStatement(SQL_QUERY);
17+
ResultSet rs = pst.executeQuery();) {
18+
employees = new ArrayList<Employee>();
19+
Employee employee;
20+
while (rs.next()) {
21+
employee = new Employee();
22+
employee.setEmpNo(rs.getInt("empno"));
23+
employee.setEname(rs.getString("ename"));
24+
employee.setJob(rs.getString("job"));
25+
employee.setMgr(rs.getInt("mgr"));
26+
employee.setHiredate(rs.getDate("hiredate"));
27+
employee.setSal(rs.getInt("sal"));
28+
employee.setComm(rs.getInt("comm"));
29+
employee.setDeptno(rs.getInt("deptno"));
30+
employees.add(employee);
31+
}
32+
} catch (SQLException e) {
33+
e.printStackTrace();
34+
}
35+
return employees;
36+
}
37+
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
create table dept(
2+
deptno numeric,
3+
dname varchar(14),
4+
loc varchar(13),
5+
constraint pk_dept primary key (deptno)
6+
);
7+
8+
create table emp(
9+
empno numeric,
10+
ename varchar(10),
11+
job varchar(9),
12+
mgr numeric,
13+
hiredate date,
14+
sal numeric,
15+
comm numeric,
16+
deptno numeric,
17+
constraint pk_emp primary key (empno),
18+
constraint fk_deptno foreign key (deptno) references dept (deptno)
19+
);
20+
21+
insert into dept values(10, 'ACCOUNTING', 'NEW YORK');
22+
insert into dept values(20, 'RESEARCH', 'DALLAS');
23+
insert into dept values(30, 'SALES', 'CHICAGO');
24+
insert into dept values(40, 'OPERATIONS', 'BOSTON');
25+
26+
insert into emp values(
27+
7839, 'KING', 'PRESIDENT', null,
28+
to_date('17-11-1981','dd-mm-yyyy'),
29+
7698, null, 10
30+
);
31+
insert into emp values(
32+
7698, 'BLAKE', 'MANAGER', 7839,
33+
to_date('1-5-1981','dd-mm-yyyy'),
34+
7782, null, 20
35+
);
36+
insert into emp values(
37+
7782, 'CLARK', 'MANAGER', 7839,
38+
to_date('9-6-1981','dd-mm-yyyy'),
39+
7566, null, 30
40+
);
41+
insert into emp values(
42+
7566, 'JONES', 'MANAGER', 7839,
43+
to_date('2-4-1981','dd-mm-yyyy'),
44+
7839, null, 40
45+
);
46+
47+
commit;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.hikaricp;
2+
3+
import java.util.List;
4+
5+
import org.junit.Ignore;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class HikariCPTest {
11+
12+
@Test
13+
@Ignore
14+
public void givenConnection_thenFetchDbData() {
15+
List<Employee> employees = HikariCPDemo.fetchData();
16+
assertEquals(4, employees.size());
17+
}
18+
19+
}

0 commit comments

Comments
 (0)