|
23 | 23 | import org.apache.flink.api.java.tuple.Tuple2;
|
24 | 24 | import org.apache.flink.connector.jdbc.JdbcDataTestBase;
|
25 | 25 | import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
|
| 26 | +import org.apache.flink.connector.jdbc.JdbcRowOutputFormat; |
26 | 27 | import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
|
27 | 28 | import org.apache.flink.connector.jdbc.internal.executor.JdbcBatchStatementExecutor;
|
28 | 29 | import org.apache.flink.connector.jdbc.internal.options.InternalJdbcConnectionOptions;
|
|
34 | 35 | import org.junit.jupiter.api.Test;
|
35 | 36 | import org.mockito.Mockito;
|
36 | 37 |
|
| 38 | +import java.io.IOException; |
37 | 39 | import java.sql.Connection;
|
38 | 40 | import java.sql.DriverManager;
|
39 | 41 | import java.sql.PreparedStatement;
|
|
44 | 46 | import java.util.Arrays;
|
45 | 47 | import java.util.List;
|
46 | 48 |
|
47 |
| -import static org.apache.flink.connector.jdbc.JdbcTestFixture.OUTPUT_TABLE; |
48 |
| -import static org.apache.flink.connector.jdbc.JdbcTestFixture.TEST_DATA; |
49 |
| -import static org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry; |
| 49 | +import static org.apache.flink.connector.jdbc.JdbcTestFixture.*; |
50 | 50 | import static org.assertj.core.api.Assertions.assertThat;
|
51 | 51 | import static org.mockito.Mockito.doReturn;
|
52 | 52 |
|
@@ -221,6 +221,30 @@ void testJdbcOutputFormat() throws Exception {
|
221 | 221 | check(expected);
|
222 | 222 | }
|
223 | 223 |
|
| 224 | + @Test |
| 225 | + public void testExceptionOnFlush() { |
| 226 | + JdbcRowOutputFormat jdbcOutputFormat = |
| 227 | + JdbcRowOutputFormat.buildJdbcOutputFormat() |
| 228 | + .setDrivername(getMetadata().getDriverClass()) |
| 229 | + .setDBUrl(getMetadata().getJdbcUrl()) |
| 230 | + .setQuery(String.format(INSERT_TEMPLATE, OUTPUT_TABLE_4)) |
| 231 | + .setBatchSize(2) |
| 232 | + .finish(); |
| 233 | + setRuntimeContext(jdbcOutputFormat, true); |
| 234 | + try { |
| 235 | + jdbcOutputFormat.open(0, 1); |
| 236 | + |
| 237 | + jdbcOutputFormat.writeRecord(toRow(TEST_DATA[1])); |
| 238 | + jdbcOutputFormat.writeRecord(toRow(TEST_DATA[1])); |
| 239 | + } catch (IOException e) { |
| 240 | + try { |
| 241 | + jdbcOutputFormat.close(); |
| 242 | + } catch (Exception e1) { |
| 243 | + assertThat(jdbcOutputFormat.getConnection()).isEqualTo(null); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + |
224 | 248 | private void check(Row[] rows) throws SQLException {
|
225 | 249 | check(rows, getMetadata().getJdbcUrl(), OUTPUT_TABLE, fieldNames);
|
226 | 250 | }
|
|
0 commit comments