BATCH-964: added fetchSize property

This commit is contained in:
trisberg
2008-12-09 22:19:56 +00:00
parent eaa359b916
commit 194e1ab737
3 changed files with 81 additions and 0 deletions

View File

@@ -57,6 +57,8 @@ import java.sql.SQLException;
*/
public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> implements InitializingBean {
public static final int VALUE_NOT_SET = -1;
private DataSource dataSource;
private PagingQueryProvider queryProvider;
@@ -73,6 +75,8 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
private Object startAfterValue;
private int fetchSize = VALUE_NOT_SET;
public JdbcPagingItemReader() {
setName(ClassUtils.getShortName(JdbcPagingItemReader.class));
}
@@ -81,6 +85,19 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
this.dataSource = dataSource;
}
/**
* Gives the JDBC driver a hint as to the number of rows that should be
* fetched from the database when more rows are needed for this
* <code>ResultSet</code> object. If the fetch size specified is zero, the
* JDBC driver ignores the value.
*
* @param fetchSize the number of rows to fetch
* @see ResultSet#setFetchSize(int)
*/
public void setFetchSize(int fetchSize) {
this.fetchSize = fetchSize;
}
public void setQueryProvider(PagingQueryProvider queryProvider) {
this.queryProvider = queryProvider;
}
@@ -114,6 +131,9 @@ public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> impleme
super.afterPropertiesSet();
Assert.notNull(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
if (fetchSize != VALUE_NOT_SET) {
jdbcTemplate.setFetchSize(fetchSize);
}
jdbcTemplate.setMaxRows(pageSize);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(jdbcTemplate);
Assert.notNull(queryProvider);

View File

@@ -0,0 +1,32 @@
package org.springframework.batch.item.database;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.util.ReflectionTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class JdbcPagingItemReaderConfigTests {
@Autowired
private JdbcPagingItemReader<Object> jdbcPagingItemReder;
@Test
public void testConfig() {
assertNotNull(jdbcPagingItemReder);
SimpleJdbcTemplate simpleJdbcTemplate = (SimpleJdbcTemplate) ReflectionTestUtils.getField(jdbcPagingItemReder, "simpleJdbcTemplate");
JdbcTemplate jdbcTemplate = (JdbcTemplate) simpleJdbcTemplate.getJdbcOperations();
assertEquals(1000, jdbcTemplate.getMaxRows());
assertEquals(100, jdbcTemplate.getFetchSize());
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:testdb" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jdbcPagingItemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader">
<property name="dataSource" ref="dataSource"/>
<property name="pageSize" value="1000"/>
<property name="fetchSize" value="100"/>
<property name="queryProvider">
<bean class="org.springframework.batch.item.database.support.HsqlPagingQueryProvider">
<property name="selectClause" value="select id, bar"/>
<property name="fromClause" value="foo"/>
<property name="sortKey" value="id"/>
</bean>
</property>
</bean>
</beans>