From e7ea16c9bc8bb3cbe174c8b190fff206b957e1bc Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 13 Nov 2018 00:23:26 +0100 Subject: [PATCH] Add connection parameter to AbstractCursorItemReader#cleanupOnClose This change makes it possible to (re)use the same connection used in the openCursor method to clean up resources when the reader is closed. Resolves #1696 --- .../database/AbstractCursorItemReader.java | 19 +++++++++++++++++-- .../item/database/JdbcCursorItemReader.java | 17 ++++++++++++++++- .../database/StoredProcedureItemReader.java | 17 ++++++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java index d07ab8a94..aefc8f6a9 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/AbstractCursorItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -396,7 +396,7 @@ implements InitializingBean { initialized = false; JdbcUtils.closeResultSet(this.rs); rs = null; - cleanupOnClose(); + cleanupOnClose(con); if(this.con != null && !this.con.isClosed()) { this.con.setAutoCommit(this.initialConnectionAutoCommit); @@ -413,8 +413,23 @@ implements InitializingBean { } } + /** + * Clean up resources. + * @throws Exception If unable to clean up resources + * @deprecated This method is deprecated in favor of + * {@link AbstractCursorItemReader#cleanupOnClose(java.sql.Connection)} and + * will be removed in a future release + */ + @Deprecated protected abstract void cleanupOnClose() throws Exception; + /** + * Clean up resources. + * @param connection to the database + * @throws Exception If unable to clean up resources + */ + protected abstract void cleanupOnClose(Connection connection) throws Exception; + /** * Execute the statement to open the cursor. */ diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java index 94a7d4c1e..3e2c8ade8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import org.springframework.util.ClassUtils; * @author Peter Zozom * @author Robert Kasanicky * @author Thomas Risberg + * @author Mahmoud Ben Hassine */ public class JdbcCursorItemReader extends AbstractCursorItemReader { @@ -143,12 +144,26 @@ public class JdbcCursorItemReader extends AbstractCursorItemReader { /** * Close the cursor and database connection. + * @deprecated This method is deprecated in favor of + * {@link JdbcCursorItemReader#cleanupOnClose(java.sql.Connection)} and will + * be removed in a future release */ @Override + @Deprecated protected void cleanupOnClose() throws Exception { JdbcUtils.closeStatement(this.preparedStatement); } + /** + * Close the cursor and database connection. + * @param connection to the database + */ + @Override + protected void cleanupOnClose(Connection connection) throws Exception { + JdbcUtils.closeStatement(this.preparedStatement); + JdbcUtils.closeConnection(connection); + } + @Override public String getSql() { return this.sql; diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/StoredProcedureItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/StoredProcedureItemReader.java index 27210a055..08079dabc 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/StoredProcedureItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/StoredProcedureItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,6 +55,7 @@ import org.springframework.util.ClassUtils; *

* * @author Thomas Risberg + * @author Mahmoud Ben Hassine */ public class StoredProcedureItemReader extends AbstractCursorItemReader { @@ -239,12 +240,26 @@ public class StoredProcedureItemReader extends AbstractCursorItemReader { /** * Close the cursor and database connection. + * @deprecated This method is deprecated in favor of + * {@link StoredProcedureItemReader#cleanupOnClose(java.sql.Connection)} and + * will be removed in a future release */ @Override + @Deprecated protected void cleanupOnClose() throws Exception { JdbcUtils.closeStatement(this.callableStatement); } + /** + * Close the cursor and database connection. + * @param connection to the database + */ + @Override + protected void cleanupOnClose(Connection connection) throws Exception { + JdbcUtils.closeStatement(this.callableStatement); + JdbcUtils.closeConnection(connection); + } + @Override public String getSql() { if (callString != null) {