4
4
5
5
package io .airbyte .db .jdbc ;
6
6
7
+ import com .google .errorprone .annotations .MustBeClosed ;
7
8
import io .airbyte .commons .functional .CheckedConsumer ;
8
9
import io .airbyte .commons .functional .CheckedFunction ;
9
10
import io .airbyte .db .JdbcCompatibleSourceOperations ;
10
- import java .io .Closeable ;
11
11
import java .sql .Connection ;
12
12
import java .sql .DatabaseMetaData ;
13
13
import java .sql .PreparedStatement ;
@@ -42,11 +42,6 @@ public DefaultJdbcDatabase(final CloseableConnectionSupplier connectionSupplier,
42
42
this .connectionSupplier = connectionSupplier ;
43
43
}
44
44
45
- public DefaultJdbcDatabase (final CloseableConnectionSupplier connectionSupplier ) {
46
- super (JdbcUtils .getDefaultSourceOperations ());
47
- this .connectionSupplier = connectionSupplier ;
48
- }
49
-
50
45
@ Override
51
46
public void execute (final CheckedConsumer <Connection , SQLException > query ) throws SQLException {
52
47
try (final Connection connection = connectionSupplier .getConnection ()) {
@@ -58,12 +53,14 @@ public void execute(final CheckedConsumer<Connection, SQLException> query) throw
58
53
public <T > List <T > bufferedResultSetQuery (final CheckedFunction <Connection , ResultSet , SQLException > query ,
59
54
final CheckedFunction <ResultSet , T , SQLException > recordTransform )
60
55
throws SQLException {
61
- try (final Connection connection = connectionSupplier .getConnection ()) {
62
- return toStream (query .apply (connection ), recordTransform ).collect (Collectors .toList ());
56
+ try (final Connection connection = connectionSupplier .getConnection ();
57
+ final Stream <T > results = toStream (query .apply (connection ), recordTransform )) {
58
+ return results .collect (Collectors .toList ());
63
59
}
64
60
}
65
61
66
62
@ Override
63
+ @ MustBeClosed
67
64
public <T > Stream <T > resultSetQuery (final CheckedFunction <Connection , ResultSet , SQLException > query ,
68
65
final CheckedFunction <ResultSet , T , SQLException > recordTransform )
69
66
throws SQLException {
@@ -101,6 +98,7 @@ public DatabaseMetaData getMetaData() throws SQLException {
101
98
* @throws SQLException SQL related exceptions.
102
99
*/
103
100
@ Override
101
+ @ MustBeClosed
104
102
public <T > Stream <T > query (final CheckedFunction <Connection , PreparedStatement , SQLException > statementCreator ,
105
103
final CheckedFunction <ResultSet , T , SQLException > recordTransform )
106
104
throws SQLException {
@@ -121,38 +119,4 @@ public void close() throws Exception {
121
119
connectionSupplier .close ();
122
120
}
123
121
124
- public interface CloseableConnectionSupplier extends AutoCloseable {
125
-
126
- Connection getConnection () throws SQLException ;
127
-
128
- }
129
-
130
- public static final class DataSourceConnectionSupplier implements CloseableConnectionSupplier {
131
-
132
- private final DataSource dataSource ;
133
-
134
- public DataSourceConnectionSupplier (final DataSource dataSource ) {
135
- this .dataSource = dataSource ;
136
- }
137
-
138
- @ Override
139
- public Connection getConnection () throws SQLException {
140
- return dataSource .getConnection ();
141
- }
142
-
143
- @ Override
144
- public void close () throws Exception {
145
- // Just a safety in case we are using a datasource implementation that requires closing.
146
- // BasicDataSource from apache does since it also provides a pooling mechanism to reuse connections.
147
-
148
- if (dataSource instanceof AutoCloseable ) {
149
- ((AutoCloseable ) dataSource ).close ();
150
- }
151
- if (dataSource instanceof Closeable ) {
152
- ((Closeable ) dataSource ).close ();
153
- }
154
- }
155
-
156
- }
157
-
158
122
}
0 commit comments