spring-projects / spring-projects/spring-batch
JdbcCursorItemReader#useSharedExtendedConnection does not attach the connection to the transaction [BATCH-2674]
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 2.5k
- Avg merge
- 6d 53m
- Merged PRs (30d)
- 3
Description
Eduardo Macarron opened BATCH-2674 and commented
According to the javadoc, when useSharedExtendedConnection is set, JdbcCursorItemReader should pick a connection from the transaction and keep it open by filtering futther calls to close() (by the use of the ExtendedConnectionDataSourceProxy. Javadoc:
"Indicate whether the connection used for the cursor should be used by all other processing thus sharing the same transaction"
However seems it is not doing so.
It implements ItemStream and ItemReader. Seems that spring batch is calling ItemStream#open when transaction is not yet active. And connection opening is happening there. This line in AbstractCursorItemReader#initializeConnection()
this.con = DataSourceUtils.getConnection(dataSource);
Ends up creating a new connection not bound to a transaction.
...
(previous transaction ending...)
[DEBUG] org.springframework.jdbc.datasource.DataSourceTransactionManager.doCleanupAfterCompletion(DataSourceTransactionManager.java:368) - Releasing JDBC Connection [org.hsqldb.jdbc.JDBCConnection@4f8caaf3] after transaction
[DEBUG] org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:329) - Returning JDBC Connection to DataSource
(ItemStream#open() called)
[DEBUG] org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110) - Fetching JDBC Connection from DataSource
[DEBUG] org.springframework.jdbc.datasource.SimpleDriverDataSource.getConnectionFromDriver(SimpleDriverDataSource.java:138) - Creating new JDBC Driver Connection to [jdbc:hsqldb:mem:dataSource]
...
Following the execution until the close method gets called confirms it. Synchronization is not active and there is not any connection holder in the thread.
This is quite an old property so it is hard to belive that it is not working as expected so it is more likely that I am missing something important. Sorry if so! and thanks for having a look into this.
No further details from BATCH-2674
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing ItemStream#open into AbstractCursorItemReader#initializeConnection(), especially the DataSourceUtils.getConnection(dataSource) call, and compare it with useSharedExtendedConnection and ExtendedConnectionDataSourceProxy. Reproduce the transaction and connection logs described in the issue; done means the cursor connection is obtained from and remains shared with the transaction as the javadoc specifies.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100