Java Code Examples for org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter

The following examples show how to use org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] {100, 200};
	final int[] rowsAffected = new int[] {1, 2};

	given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
	mockDatabaseMetaData(true);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, times(2)).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 2
Source Project: spring-analysis-note   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] {100, 200};
	final int[] rowsAffected = new int[] {1, 2};

	given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
	mockDatabaseMetaData(false);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, never()).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 3
Source Project: java-technology-stack   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] {100, 200};
	final int[] rowsAffected = new int[] {1, 2};

	given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
	mockDatabaseMetaData(true);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, times(2)).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 4
Source Project: java-technology-stack   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] {100, 200};
	final int[] rowsAffected = new int[] {1, 2};

	given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
	mockDatabaseMetaData(false);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, never()).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 5
Source Project: spring4-understanding   Source File: JdbcTemplateTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] { 100, 200 };
	final int[] rowsAffected = new int[] { 1, 2 };

	given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
	mockDatabaseMetaData(true);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, times(2)).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 6
Source Project: spring4-understanding   Source File: JdbcTemplateTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] { 100, 200 };
	final int[] rowsAffected = new int[] { 1, 2 };

	given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
	mockDatabaseMetaData(false);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, never()).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 7
Source Project: effectivejava   Source File: JdbcTemplateTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClass() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] { 100, 200 };
	final int[] rowsAffected = new int[] { 1, 2 };

	given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
	mockDatabaseMetaData(true);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, times(2)).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}
 
Example 8
Source Project: effectivejava   Source File: JdbcTemplateTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() throws Exception {
	final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
	final int[] ids = new int[] { 100, 200 };
	final int[] rowsAffected = new int[] { 1, 2 };

	given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
	mockDatabaseMetaData(false);

	BatchPreparedStatementSetter setter =
			new AbstractInterruptibleBatchPreparedStatementSetter() {
				@Override
				protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
					if (i < ids.length) {
						ps.setInt(1, ids[i]);
						return true;
					}
					else {
						return false;
					}
				}
			};

	JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

	int[] actualRowsAffected = template.batchUpdate(sql, setter);
	assertTrue("executed 2 updates", actualRowsAffected.length == 2);
	assertEquals(rowsAffected[0], actualRowsAffected[0]);
	assertEquals(rowsAffected[1], actualRowsAffected[1]);

	verify(this.preparedStatement, never()).addBatch();
	verify(this.preparedStatement).setInt(1, ids[0]);
	verify(this.preparedStatement).setInt(1, ids[1]);
	verify(this.preparedStatement).close();
	verify(this.connection, atLeastOnce()).close();
}