BATCH-970: made the transaction participation configurable

This commit is contained in:
trisberg
2008-12-15 17:30:34 +00:00
parent 6d6f54beea
commit 2a1305358e
2 changed files with 30 additions and 1 deletions

View File

@@ -134,6 +134,8 @@ public class JdbcCursorItemReader<T> extends AbstractItemCountingItemStreamItemR
private boolean driverSupportsAbsolute = false;
private boolean participateInExistingTransaction = false;
public JdbcCursorItemReader() {
setName(ClassUtils.getShortName(JdbcCursorItemReader.class));
}
@@ -171,7 +173,12 @@ public class JdbcCursorItemReader<T> extends AbstractItemCountingItemStreamItemR
Assert.state(dataSource != null, "DataSource must not be null.");
try {
this.con = DataSourceUtils.getConnection(dataSource);
if (participateInExistingTransaction) {
this.con = DataSourceUtils.getConnection(dataSource);
}
else {
this.con = dataSource.getConnection();
}
preparedStatement = this.con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT);
applyStatementSettings(preparedStatement);
@@ -377,6 +384,19 @@ public class JdbcCursorItemReader<T> extends AbstractItemCountingItemStreamItemR
this.driverSupportsAbsolute = driverSupportsAbsolute;
}
/**
* Indicate whether the cursor should be opened as part of an existing transaction or if it
* should be opened in its own transaction. The default is for the cursor to be opened in its
* own transaction. If you set this flag to true then you should wrap the DataSource in a
* {@link org.springframework.jdbc.datasource.SingleConnectionDataSource} to prevent the
* connection from being closed after each commit.
*
* @param participateInExistingTransaction <code>false</code> by default
*/
public void setParticipateInExistingTransaction(boolean participateInExistingTransaction) {
this.participateInExistingTransaction = participateInExistingTransaction;
}
/**
* Check the result set is in synch with the currentRow attribute. This is
* important to ensure that the user hasn't modified the current row.