-
Notifications
You must be signed in to change notification settings - Fork 41.2k
#7865 Spring Batch : Fix PostgreSQL with defaultAutoCommit=false #7866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#7865 Spring Batch : Fix PostgreSQL with defaultAutoCommit=false #7866
Conversation
JobLauncherCommandLineRunner now wraps all DB call in transaction manager if available.
@dsyer Any chance of a quick review of this one? |
I don't know. Why would you want to set |
I agree with @dsyer and that kind of issue could arise in another part of the codebase anyway. |
My use case was to use a I agree that I may have missed something in Spring Batch or Spring Boot configuration, as the use case seems very common. |
If you are using the |
(Lines below from Spring Batch 3.0.7) We do configure But the connection for
The chunk transaction is only started in the "doExecute" part of AbstractStep
Thank for your patience. I am missing something ? |
I guess it makes sense that the cursor is opened outside a transaction. I had forgotten about that detail. But setting autocommit=false globally isn't really a great idea still. Maybe you could take it up with the Batch team (ask them to set autocommit=false in the reader, or allow the user to configure that explicitly)? |
It clearly makes sense to have the cursor open outside a transaction. I will link this thread to Spring Batch team and submit a way to set auto commit mode in Thanks all for your reviews |
Thanks @gdarmont. I'll close this one for now until the Batch changes are implemented. |
Spring Boot 1.4.3 / JDK 8 / PostgreSQL Driver 9.4.1209
Using a Datasource (Tomcat JDBC in our case) configured with defaultAutoCommit=false leads to
This error is caused by SQL requests being ran out of a transaction by
JobLauncherCommandLineRunner#getNextJobParameters()
.PostgreSQL connection used are neither committed nor rollbacked and as such, are still considered "OPEN" by the PostgreSQL Driver. This connection is then returned to the pool.
The next request is made by Spring Batch using the same connection (the pool returned the same connection), but the transaction isolation needs to be changed due to the configuration defined in
AbstractJobRepositoryFactoryBean#initializeProxy()
configuration. This operation is forbidden by PostgreSQL since the connection is already open.A very simple reproducer is available here : https://gist.github.com/gdarmont/0b796aa06b86c37d147a6cded8065e96
You only need a PostgreSQL DB.
This patch changes
JobLauncherCommandLineRunner
so it now wraps all DB call in transaction manager if available.Fixes #7865
Hope this helps,
Guillaume