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

mybatis-spring dose not obey the spring transaction propagation behavior #183

Open
gaohanghbut opened this issue Jan 9, 2017 · 3 comments

Comments

@gaohanghbut
Copy link

I've met a problem when I hope my service method shouldn't be wrapped by a transaction.Because there's several http requests and the method took a long time to finish itself and then, many threads hold the jdbc connections too long that my connection pool is empty and many CannotGetJdbcConnectionException were be thrown. So I marked the method with @transactional(propagation = Propagation.NOT_SUPPORTED), but it dosn't work.

I checked the SqlSessionTemplate and found that it'll invoke SqlSessionUtils.registerSessionHolder method to bind the SqlSession to TransactionSynchronizationManager if the TransactionFactory is implemented by SpringManagedTransactionFactory. It only works well then my transaction propagation behavior is Propagation.REQUIRED I bet.

So I think we should reimplement the SqlSessionTemplate and writer a MybatisTransactionManager to take the place of SqlSessionSynchronization.

Or if there's a better choice for me?

@kazuki43zoo
Copy link
Member

Hi @gaohanghbut

If you want to return a connection to connection pool after invoking a query, you don't add the @Transactional on your service method. If you add the @Transactiona on you service method, a connection is reused on each queries.This behavior is same with the JdbcTemplate provided by Spring Framework.
Hence; I think it's working as designed.

@eddumelendez @harawata @emacarron
What do you think for my opinion ?

@gaohanghbut
Copy link
Author

gaohanghbut commented Jan 11, 2017

@kazuki43zoo ,thanks for your attention, I know if I remove the @transactional,it can works perfect, but what they could do if someone else use the spring aop configured by xml?

But you are right, this behavior is the same with the JdbcTemplate. After read the JdbcTemplate and the DataSourceTransactionTemplate again, I think, both SqlSessonUtils and DataSourceUtils provided by Spring Framework should use both TransactionSynchronizationManager.isSynchronizationActive and TransactionSynchronizationManager.isActualTransactionActive because it will be false if no transaction needed, am I right?

@IamRichie
Copy link

What's aop?I can't understanded..

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

No branches or pull requests

3 participants