-
Notifications
You must be signed in to change notification settings - Fork 206
/
WriteJdbcP.java
185 lines (162 loc) · 5.68 KB
/
WriteJdbcP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.jet.impl.connector;
import com.hazelcast.jet.core.Inbox;
import com.hazelcast.jet.core.Outbox;
import com.hazelcast.jet.core.Processor;
import com.hazelcast.jet.core.ProcessorMetaSupplier;
import com.hazelcast.jet.core.processor.SinkProcessors;
import com.hazelcast.jet.function.DistributedBiConsumer;
import com.hazelcast.jet.function.DistributedSupplier;
import com.hazelcast.jet.impl.util.ExceptionUtil;
import com.hazelcast.logging.ILogger;
import com.hazelcast.util.concurrent.BackoffIdleStrategy;
import com.hazelcast.util.concurrent.IdleStrategy;
import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.util.ArrayList;
import java.util.List;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Use {@link SinkProcessors#writeJdbcP}.
*/
public final class WriteJdbcP<T> implements Processor {
private static final IdleStrategy IDLER =
new BackoffIdleStrategy(0, 0, SECONDS.toNanos(1), SECONDS.toNanos(10));
private static final int BATCH_LIMIT = 50;
private final DistributedSupplier<Connection> connectionSupplier;
private final DistributedBiConsumer<PreparedStatement, T> bindFn;
private final String updateQuery;
private ILogger logger;
private Connection connection;
private PreparedStatement statement;
private List<T> itemList = new ArrayList<>();
private int idleCount;
private boolean supportsBatch;
private int batchCount;
private WriteJdbcP(
@Nonnull String updateQuery,
@Nonnull DistributedSupplier<Connection> connectionSupplier,
@Nonnull DistributedBiConsumer<PreparedStatement, T> bindFn
) {
this.updateQuery = updateQuery;
this.connectionSupplier = connectionSupplier;
this.bindFn = bindFn;
}
/**
* Use {@link SinkProcessors#writeJdbcP}.
*/
public static <T> ProcessorMetaSupplier metaSupplier(
@Nonnull String updateQuery,
@Nonnull DistributedSupplier<Connection> connectionSupplier,
@Nonnull DistributedBiConsumer<PreparedStatement, T> bindFn
) {
return ProcessorMetaSupplier.preferLocalParallelismOne(() ->
new WriteJdbcP<>(updateQuery, connectionSupplier, bindFn));
}
@Override
public void init(@Nonnull Outbox outbox, @Nonnull Context context) {
logger = context.logger();
connectAndPrepareStatement();
}
@Override
public void process(int ordinal, @Nonnull Inbox inbox) {
inbox.drainTo(itemList);
while (!itemList.isEmpty()) {
if (!reconnectIfNecessary()) {
continue;
}
try {
for (T item : itemList) {
bindFn.accept(statement, item);
addBatchOrExecute();
}
executeBatch();
connection.commit();
itemList.clear();
idleCount = 0;
} catch (Exception e) {
if (e instanceof SQLNonTransientException ||
e.getCause() instanceof SQLNonTransientException) {
throw ExceptionUtil.rethrow(e);
} else {
logger.warning("Exception during update", e.getCause());
idleCount++;
}
}
}
}
@Override
public boolean isCooperative() {
return false;
}
@Override
public void close() {
closeWithLogging(statement);
closeWithLogging(connection);
}
private boolean connectAndPrepareStatement() {
try {
connection = connectionSupplier.get();
connection.setAutoCommit(false);
supportsBatch = connection.getMetaData().supportsBatchUpdates();
statement = connection.prepareStatement(updateQuery);
} catch (Exception e) {
logger.warning("Exception during connecting and preparing the statement", e);
idleCount++;
return false;
}
return true;
}
private void addBatchOrExecute() throws SQLException {
if (!supportsBatch) {
statement.executeUpdate();
return;
}
statement.addBatch();
if (++batchCount == BATCH_LIMIT) {
statement.executeBatch();
batchCount = 0;
}
}
private void executeBatch() throws SQLException {
if (supportsBatch) {
statement.executeBatch();
batchCount = 0;
}
}
private boolean reconnectIfNecessary() {
if (idleCount == 0) {
return true;
}
IDLER.idle(idleCount);
close();
return connectAndPrepareStatement();
}
private void closeWithLogging(AutoCloseable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (Exception e) {
logger.warning("Exception during closing " + closeable, e);
}
}
}