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

Sharing DataSource Connection with JtaTransactionManager [SPR-16088] #20637

Closed
spring-projects-issues opened this issue Oct 18, 2017 · 1 comment
Closed

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 18, 2017

Greg Potter opened SPR-16088 and commented

When the HibernateTransactionManager is used, each connection is bound in the TransactionSynchronizationManager. This allows for calls to DataSourceUtils (like in the JdbcTemplate) to use the same connection in the same transaction. For example, if we use Hibernate to perform a query and then use JdbcTemplate in the same transaction (and same datasource) the same connection will be used.

However, this does not happen when using the JTATransactionManager. Instead, JdbcTemplate will open a new connection (after DataSourceUtils does not find one in the TransactionSynchronizationManager) which can result in database locking. For example, if the Hibernate makes an insert (or update, or delete) and then (within the same transaction) the JdbcTemplate tries to alter that same row, then it will lock (and ultimately time out) waiting for the row to no longer be locked.


Affects: 4.3.11, 4.3.12, 5.0 GA

Reference URL: spring-projects/spring-framework-issues#172

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 18, 2017

Juergen Hoeller commented

JTA is meant to be used with independent DataSource.getConnection() lookups, with the underlying DataSource being responsible for returning the same - or at least an equivalent - XA-enlisted Connection within the same transaction. In that sense, this works as designed by default, provided that you're using a corresponding XADataSource setup.

If you'd like to enforce the same JDBC Connection handle on the application's side, consider setting up a TransactionAwareDataSourceProxy that both your Hibernate SessionFactory and your JDBC code is being pointed to. Note that his may not be ideal for some JTA setups since the JTA backend doesn't receive individual lookup calls (and can't individually react to them or individually monitor them) but it will be closest to HibernateTransactionManager's behavior at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.