Skip to content

Commit 6947d2f

Browse files
committed
Adding Hikari Connection Pool support
1 parent f38592b commit 6947d2f

File tree

9 files changed

+191
-146
lines changed

9 files changed

+191
-146
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747
<dependency>
4848
<groupId>org.springframework.boot</groupId>
4949
<artifactId>spring-boot-starter-jdbc</artifactId>
50+
<exclusions>
51+
<exclusion>
52+
<groupId>org.apache.tomcat</groupId>
53+
<artifactId>tomcat-jdbc</artifactId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
58+
<!-- exclude tomcat-jdbc, Spring Boot will use HikariCP automatically -->
59+
<dependency>
60+
<groupId>com.zaxxer</groupId>
61+
<artifactId>HikariCP</artifactId>
5062
</dependency>
5163
<dependency>
5264
<groupId>org.springframework.boot</groupId>

src/main/java/com/example/ApplicationConfig.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.example;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.jdbc.core.JdbcTemplate;
8-
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
98
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
109
import org.springframework.jdbc.support.rowset.SqlRowSet;
1110

12-
import javax.sql.DataSource;
13-
import java.sql.Driver;
1411
import java.util.Iterator;
1512
import java.util.NoSuchElementException;
1613
import java.util.Spliterator;
@@ -25,29 +22,8 @@
2522
@Configuration
2623
public class ApplicationConfig {
2724

28-
@Autowired
29-
private DataSourceProperties dataSourceProperties;
30-
31-
@Autowired
32-
private ApplicationProperties applicationProperties;
33-
34-
@Bean
35-
DataSource dataSource() {
36-
Driver driver = new org.apache.phoenix.jdbc.PhoenixDriver();
37-
String url = dataSourceProperties.getUrl();
38-
DataSource dataSource = new SimpleDriverDataSource(driver,url);
39-
return dataSource;
40-
}
41-
42-
@Bean
43-
JdbcTemplate jdbcTemplate(final DataSource dataSource) {
44-
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
45-
jdbcTemplate.setFetchSize(applicationProperties.getDatasource().getFetchSize());
46-
return jdbcTemplate;
47-
}
48-
4925
@Bean
50-
public QueryStream streamer(JdbcTemplate jdbcTemplate) {
26+
public QueryStream streamer(final JdbcTemplate jdbcTemplate) {
5127
return new QueryStream() {
5228
@Override
5329
public <T> T streamQuery(String sql, Function<Stream<SqlRowSet>, ? extends T> streamer, Object... args) {
@@ -63,7 +39,7 @@ public <T> T streamQuery(String sql, Function<Stream<SqlRowSet>, ? extends T> st
6339
private boolean first = true;
6440
@Override
6541
public boolean hasNext() {
66-
return !rowSet.isLast();
42+
return rowSet.next();
6743
}
6844

6945
@Override

src/main/java/com/example/DataSourceResource.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/main/java/com/example/JdbcStream.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.example;
22

33

4-
import org.apache.phoenix.jdbc.PhoenixResultSet;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import com.zaxxer.hikari.pool.HikariProxyResultSet;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.jdbc.core.JdbcTemplate;
78
import org.springframework.jdbc.datasource.DataSourceUtils;
89
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
910
import org.springframework.jdbc.support.rowset.SqlRowSet;
1011
import org.springframework.stereotype.Component;
1112

12-
import javax.sql.DataSource;
1313
import java.io.Closeable;
14-
import java.io.IOException;
1514
import java.sql.Connection;
1615
import java.sql.PreparedStatement;
1716
import java.sql.SQLException;
@@ -31,7 +30,7 @@
3130
public class JdbcStream extends JdbcTemplate {
3231

3332
@Autowired
34-
public JdbcStream(DataSource dataSource) {
33+
public JdbcStream(HikariDataSource dataSource) {
3534
super(dataSource);
3635
}
3736

@@ -49,9 +48,13 @@ public boolean hasNext() {
4948
@Override
5049
public SqlRow next() {
5150
ResultSetWrappingSqlRowSet resultSetWrappingSqlRowSet = (ResultSetWrappingSqlRowSet) rowSet;
52-
PhoenixResultSet resultSet = (PhoenixResultSet) resultSetWrappingSqlRowSet.getResultSet();
53-
if (resultSet.getCurrentRow() == null) {
54-
throw new NoSuchElementException();
51+
HikariProxyResultSet resultSet = (HikariProxyResultSet) resultSetWrappingSqlRowSet.getResultSet();
52+
try {
53+
if (resultSet.isClosed()) {
54+
throw new NoSuchElementException();
55+
}
56+
} catch (SQLException e) {
57+
throw new RuntimeException("Error handling the resultSet");
5558
}
5659
return sqlRow;
5760
}
@@ -90,9 +93,13 @@ public boolean hasNext() {
9093
@Override
9194
public SqlRow next() {
9295
ResultSetWrappingSqlRowSet resultSetWrappingSqlRowSet = (ResultSetWrappingSqlRowSet) rowSet;
93-
PhoenixResultSet resultSet = (PhoenixResultSet) resultSetWrappingSqlRowSet.getResultSet();
94-
if (resultSet.getCurrentRow() == null) {
95-
throw new NoSuchElementException();
96+
HikariProxyResultSet resultSet = (HikariProxyResultSet) resultSetWrappingSqlRowSet.getResultSet();
97+
try {
98+
if (resultSet.isClosed()) {
99+
throw new NoSuchElementException();
100+
}
101+
} catch (SQLException e) {
102+
throw new RuntimeException("Error handling the resultSet");
96103
}
97104
return sqlRow;
98105
}
@@ -103,7 +110,7 @@ public SqlRow next() {
103110
;
104111

105112
@Override
106-
public void close() throws IOException {
113+
public void close() {
107114
DataSourceUtils.releaseConnection(connection, getDataSource());
108115
}
109116
}

src/main/java/com/example/JerseyConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class JerseyConfig extends ResourceConfig{
1111

1212
public JerseyConfig() {
13-
register(DataSourceResource.class);
13+
register(WebStatResource.class);
1414
}
1515

1616

src/main/java/com/example/SpringBootPhoenixApplication.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.CommandLineRunner;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
67
import org.springframework.boot.builder.SpringApplicationBuilder;
78
import org.springframework.boot.context.properties.EnableConfigurationProperties;
89
import org.springframework.boot.web.support.SpringBootServletInitializer;
10+
import org.springframework.context.annotation.Bean;
11+
12+
import javax.sql.DataSource;
913

1014
@SpringBootApplication
1115
@EnableConfigurationProperties(ApplicationProperties.class)
@@ -18,4 +22,14 @@ public static void main(String[] args) {
1822
.configure(new SpringApplicationBuilder(SpringBootPhoenixApplication.class))
1923
.run(args);
2024
}
25+
26+
@Bean
27+
CommandLineRunner commandLineRunner(final DataSource dataSource){
28+
return new CommandLineRunner() {
29+
@Override
30+
public void run(String... strings) throws Exception {
31+
System.out.println(dataSource);
32+
}
33+
};
34+
}
2135
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example;
2+
3+
import com.google.gson.Gson;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Repository;
6+
import org.springframework.util.Assert;
7+
8+
import javax.ws.rs.WebApplicationException;
9+
import javax.ws.rs.core.StreamingOutput;
10+
import java.io.*;
11+
import java.sql.SQLException;
12+
13+
@Repository
14+
public class WebStatDao {
15+
16+
@Autowired
17+
JdbcStream jdbcStream;
18+
19+
public StreamingOutput getStreamingOutputForSql(String sql, Object[] args) throws SQLException {
20+
JdbcStream.StreamableQuery streamableQuery = jdbcStream.streamableQuery(sql, args);
21+
StreamingOutput streamingOutput = getStreamingOutput(streamableQuery);
22+
streamableQuery.close();
23+
return streamingOutput;
24+
}
25+
26+
private StreamingOutput getStreamingOutput(final JdbcStream.StreamableQuery streamableQuery) {
27+
28+
Gson gson = new Gson();
29+
30+
return new StreamingOutput() {
31+
@Override
32+
public void write(OutputStream os) throws IOException, WebApplicationException {
33+
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
34+
writer.write("[");
35+
final boolean[] first = {false};
36+
try {
37+
streamableQuery
38+
.stream()
39+
.map(sqlRow -> {
40+
try {
41+
WebStat webStat = WebStatMapper.mapWebStat(sqlRow);
42+
return webStat;
43+
} catch (RuntimeException e) {
44+
throw new RuntimeException("Cannot convert SqlRom to WebStat");
45+
}
46+
}).reduce((webStat1, webStat2) -> {
47+
Assert.notNull(webStat1, "Webstat cannot be null");
48+
try {
49+
if (first[0]) {
50+
writer.write(",");
51+
} else if (!first[0]){
52+
writer.write(gson.toJson(webStat1));
53+
writer.write(",");
54+
}
55+
first[0] = true;
56+
writer.write(gson.toJson(webStat2));
57+
writer.flush();
58+
//TimeUnit.MILLISECONDS.sleep(500);
59+
} catch (IOException e) {
60+
throw new RuntimeException("Cannot write to Stream back");
61+
}
62+
return webStat1;
63+
});
64+
} catch (SQLException e) {
65+
e.printStackTrace();
66+
}
67+
writer.write("]");
68+
writer.flush();
69+
}
70+
};
71+
}
72+
}

0 commit comments

Comments
 (0)