Skip to content
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

Doc: JdbcTransactionManager vs DataSourceTransactionManager #30802

Closed
manueljordan opened this issue Jul 3, 2023 · 4 comments
Closed

Doc: JdbcTransactionManager vs DataSourceTransactionManager #30802

manueljordan opened this issue Jul 3, 2023 · 4 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@manueljordan
Copy link

Through the javadoc of PlatformTransactionManager I did realize about the JdbcTransactionManager class. The latter is a subclass of DataSourceTransactionManager.

Normally and practically always the DataSourceTransactionManager class is used to define the TX infrastructure for JDBC and MyBatis.

Now, to be honest, is not clear in the javadoc of the JdbcTransactionManager class when it is mandatory over DataSourceTransactionManager or what is/are the advantages over its superclass.

I read all the sections of the Transaction Management section of the Reference documentation and the JdbcTransactionManager term does not appear.

Therefore is not clear with the current documentation when is mandatory use JdbcTransactionManager .

Thanks for your understanding.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 3, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Jul 4, 2023

The only difference is exception translation: Like with JpaTransactionManager, there is DataAccessException translation on commit here. Where DataSourceTransactionManager will only ever throw TransactionSystemException on commit, JdbcTransactionManager is more sophisticated and translates locking failures etc to corresponding DataAccessException subclasses. Note that calling code needs to be prepared for that, not exclusively expecting TransactionSystemException in such scenarios. That's the main reason why those are two separate transaction manager classes to choose from.

It is effectively never mandatory to use JdbcTransactionManager. That said, it is a good idea to use it in application setups that are used to JPA-style exception translation (e.g. having pluggable repository implementations with JPA and JDBC variants). It is also a good idea to use it with callers that are able to handle specific DataAccessException subclasses in catch blocks around transactions, providing clearer and more expressive exceptions for concurrency failures (e.g. with PostgreSQL). I'll make sure to mention this in the reference documentation, generally recommending it over DataSourceTransactionManager these days.

@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 4, 2023
@jhoeller jhoeller changed the title Improve documentation: When use JdbcTransactionManager? Doc: JdbcTransactionManager vs DataSourceTransactionManager Jul 4, 2023
@jhoeller jhoeller self-assigned this Jul 4, 2023
@jhoeller jhoeller added this to the 6.0.11 milestone Jul 4, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jul 4, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Jul 4, 2023
@manueljordan
Copy link
Author

Thanks for the feedback/information. Just a friendly observation, pls take it with the best intentions, could be added some Test classes to show in action all the theory explained? Or better if snippet codes are included - the point is make clear how JdbcTransactionManager is better than DataTransactionManager, but as a complement of your current theory through Java code. Thanks for your understanding

@jhoeller
Copy link
Contributor

jhoeller commented Jul 7, 2023

It's not that straightforward to illustrate since transaction managers are usually kicking in behind an @Transactional method. The caller of such an @Transactional method would receive a TransactionSystemException on commit failures from DataSourceTransactionManager, whereas it would commonly receive DataAccessException subclasses (such as ConcurrencyFailureException) from JdbcTransactionManager. That's the difference in a nutshell.

Of course, the difference won't matter much if the caller is not specifically handling exceptions to begin with. However, if the caller expects to handle ConcurrencyFailureException or the like, this will work reliably with a JdbcTransactionManager setup, consistently propagating ConcurrencyFailureException and co from JdbcTemplate operations (when happening for a specific statement execution) as well as from commit (when happening late in the database commit step).

try {
    myTransactionalMethodCalled();
}
catch (ConcurrencyFailureException ex) {
    // some specific handling
}

@manueljordan
Copy link
Author

Thanks for the extra explanation ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants