From 90c63f71dcdff75a3283ce9401c102e61dcf50ae Mon Sep 17 00:00:00 2001 From: dsyer Date: Wed, 19 May 2010 07:55:23 +0000 Subject: [PATCH] BATCH-1571: fix paging queries for Postgres --- spring-batch-infrastructure-tests/pom.xml | 7 +++ .../IbatisPagingItemReaderAsyncTests.java | 7 ++- .../JdbcPagingQueryIntegrationTests.java | 6 +++ .../datasource/DataSourceInitializer.java | 49 ++++++++++--------- .../test/resources/batch-postgres.properties | 14 ++++++ .../src/test/resources/log4j.properties | 1 + ...JpaPagingItemReaderCommonTests-context.xml | 2 +- .../batch/item/database/ibatis-foo.xml | 8 +-- .../database/init-foo-schema-postgres.sql | 24 +++++++++ .../support/PostgresPagingQueryProvider.java | 3 +- .../PostgresPagingQueryProviderTests.java | 8 +-- 11 files changed, 92 insertions(+), 37 deletions(-) create mode 100644 spring-batch-infrastructure-tests/src/test/resources/batch-postgres.properties create mode 100644 spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/init-foo-schema-postgres.sql diff --git a/spring-batch-infrastructure-tests/pom.xml b/spring-batch-infrastructure-tests/pom.xml index 4d3371dc8..f5ff81ef6 100644 --- a/spring-batch-infrastructure-tests/pom.xml +++ b/spring-batch-infrastructure-tests/pom.xml @@ -241,6 +241,13 @@ true runtime + + postgresql + postgresql + 8.3-603.jdbc3 + true + runtime + diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderAsyncTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderAsyncTests.java index 46ac7e987..a6c4f8a75 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderAsyncTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderAsyncTests.java @@ -75,6 +75,7 @@ public class IbatisPagingItemReaderAsyncTests { int max = 10; for (int i = 0; i < max; i++) { try { + logger.info("Testing asynch reader, iteration="+i); doTest(); } catch (Throwable e) { @@ -136,7 +137,11 @@ public class IbatisPagingItemReaderAsyncTests { SqlMapClient sqlMapClient = createSqlMapClient(); IbatisPagingItemReader reader = new IbatisPagingItemReader(); - reader.setQueryId("getPagedFoos"); + if ("postgres".equals(System.getProperty("ENVIRONMENT"))) { + reader.setQueryId("getPagedFoosPostgres"); + } else { + reader.setQueryId("getPagedFoos"); + } reader.setPageSize(2); reader.setSqlMapClient(sqlMapClient); reader.setSaveState(true); diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java index 758b944d6..6103f8323 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java @@ -115,14 +115,20 @@ public class JdbcPagingQueryIntegrationTests { public void testJumpToItem() throws Exception { PagingQueryProvider queryProvider = getPagingQueryProvider(); + + int minId = jdbcTemplate.queryForInt("SELECT MIN(ID) FROM T_FOOS"); List> list = jdbcTemplate.queryForList(queryProvider.generateJumpToItemQuery(pageSize, pageSize)); logger.debug("Jump to page result: " + list); assertEquals(1, list.size()); + String expected = "[{sort_key="+(minId+pageSize-1); + assertEquals(expected, list.toString().substring(0, expected.length())); Object startAfterValue = list.get(0).entrySet().iterator().next().getValue(); list = jdbcTemplate.queryForList(queryProvider.generateRemainingPagesQuery(pageSize), startAfterValue); assertEquals(pageSize, list.size()); + expected = "[{id="+(minId+pageSize); + assertEquals(expected, list.toString().substring(0, expected.length())); } diff --git a/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java b/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java index 40fa615d5..db1f9ad85 100644 --- a/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java +++ b/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java @@ -61,7 +61,7 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean { return; } try { - if (destroyScript!=null) { + if (destroyScript != null) { doExecuteScript(destroyScript); initialized = false; } @@ -78,7 +78,7 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean { public void afterPropertiesSet() throws Exception { Assert.notNull(dataSource); - logger.info("Initializing with scripts: "+Arrays.asList(initScripts)); + logger.info("Initializing with scripts: " + Arrays.asList(initScripts)); if (!initialized && initialize) { try { doExecuteScript(destroyScript); @@ -89,7 +89,7 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean { if (initScripts != null) { for (int i = 0; i < initScripts.length; i++) { Resource initScript = initScripts[i]; - logger.info("Executing init script: "+initScript); + logger.info("Executing init script: " + initScript); doExecuteScript(initScript); } } @@ -100,36 +100,39 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean { private void doExecuteScript(final Resource scriptResource) { if (scriptResource == null || !scriptResource.exists()) return; - TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource)); - transactionTemplate.execute(new TransactionCallback() { - + final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + String[] scripts; + try { @SuppressWarnings("unchecked") - public Object doInTransaction(TransactionStatus status) { - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - String[] scripts; - try { - scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource - .getInputStream())), ";"); - } - catch (IOException e) { - throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); - } - for (int i = 0; i < scripts.length; i++) { - String script = scripts[i].trim(); + String[] list = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource + .getInputStream())), ";"); + scripts = list; + } + catch (IOException e) { + throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); + } + for (int i = 0; i < scripts.length; i++) { + final String script = scripts[i].trim(); + TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager( + dataSource)); + transactionTemplate.execute(new TransactionCallback() { + + public Object doInTransaction(TransactionStatus status) { if (StringUtils.hasText(script)) { try { - jdbcTemplate.execute(scripts[i]); - } catch (DataAccessException e) { + jdbcTemplate.execute(script); + } + catch (DataAccessException e) { if (!script.toUpperCase().startsWith("DROP")) { throw e; } } } + return null; } - return null; - } - }); + }); + } } diff --git a/spring-batch-infrastructure-tests/src/test/resources/batch-postgres.properties b/spring-batch-infrastructure-tests/src/test/resources/batch-postgres.properties new file mode 100644 index 000000000..11cd0e088 --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/resources/batch-postgres.properties @@ -0,0 +1,14 @@ +# Placeholders batch.* +# for Oracle: +batch.jdbc.driver=org.postgresql.Driver +batch.jdbc.url=jdbc:postgresql://localhost:9432/test +batch.jdbc.user=test +batch.jdbc.password=test +batch.jdbc.testWhileIdle=false +batch.jdbc.validationQuery= +batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-postgres.sql +batch.business.schema.script=classpath:/org/springframework/batch/jms/init.sql +batch.data.source.init=true +batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer +batch.database.incrementer.parent=sequenceIncrementerParent +batch.verify.cursor.position=true diff --git a/spring-batch-infrastructure-tests/src/test/resources/log4j.properties b/spring-batch-infrastructure-tests/src/test/resources/log4j.properties index 169df5292..bd323a20d 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/log4j.properties +++ b/spring-batch-infrastructure-tests/src/test/resources/log4j.properties @@ -7,6 +7,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{1}:%L - %m log4j.category.org.apache.activemq=ERROR # log4j.category.org.springframework=DEBUG log4j.category.org.springframework.jdbc=DEBUG +log4j.category.org.springframework.jdbc.datasource=INFO # log4j.category.org.springframework.jms=DEBUG # log4j.category.org.springframework.batch=DEBUG log4j.category.org.springframework.batch.support=INFO diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml index 9b4129cff..4074890d3 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml @@ -12,7 +12,7 @@ - classpath:org/springframework/batch/item/database/init-foo-schema-hsqldb.sql + ${batch.schema.script} diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/ibatis-foo.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/ibatis-foo.xml index d418da081..1eefeb966 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/ibatis-foo.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/ibatis-foo.xml @@ -26,12 +26,8 @@ select LIMIT #_skiprows# #_pagesize# ID, NAME, VALUE from T_FOOS order by ID - - - + select ID, NAME, VALUE from T_FOOS order by ID LIMIT #_pagesize# OFFSET #_skiprows#