/** * Invokes JdbcWriterCommands.insert * {@inheritDoc} * @see org.apache.gobblin.writer.DataWriter#write(java.lang.Object) */ @Override public void write(JdbcEntryData record) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Writing " + record); } try { this.commands.insert(this.databaseName, this.tableName, record); this.recordWrittenCount++; } catch (Exception e) { this.failed = true; throw new RuntimeException(e); } }
@Test public void writeFailRollbackTest() throws SQLException, IOException { final String database = "db"; final String table = "users"; JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class); Connection conn = mock(Connection.class); doThrow(RuntimeException.class).when(writerCommands).insert(anyString(), anyString(), any(JdbcEntryData.class)); JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn); try { writer.write(null); Assert.fail("Test case didn't throw Exception."); } catch (RuntimeException e) { Assert.assertTrue(e instanceof RuntimeException); } writer.close(); verify(writerCommands, times(1)).insert(anyString(), anyString(), any(JdbcEntryData.class)); verify(conn, times(1)).rollback(); verify(conn, never()).commit(); verify(conn, times(1)).close(); Assert.assertEquals(writer.recordsWritten(), 0L); } }
@Test public void writeAndCommitTest() throws SQLException, IOException { final String database = "db"; final String table = "users"; final int writeCount = 25; JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class); Connection conn = mock(Connection.class); try (JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn)) { for(int i = 0; i < writeCount; i++) { writer.write(null); } writer.commit(); Assert.assertEquals(writer.recordsWritten(), writeCount); } verify(writerCommands, times(writeCount)).insert(anyString(), anyString(), any(JdbcEntryData.class)); verify(conn, times(1)).commit(); verify(conn, never()).rollback(); verify(writerCommands, times(1)).flush(); verify(conn, times(1)).close(); }
/** * Invokes JdbcWriterCommands.insert * {@inheritDoc} * @see org.apache.gobblin.writer.DataWriter#write(java.lang.Object) */ @Override public void write(JdbcEntryData record) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Writing " + record); } try { this.commands.insert(this.databaseName, this.tableName, record); this.recordWrittenCount++; } catch (Exception e) { this.failed = true; throw new RuntimeException(e); } }