mybatis / spring

Spring integration for MyBatis 3
Apache License 2.0
2.81k stars 2.6k forks source link

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

Open gaohanghbut opened 7 years ago

gaohanghbut commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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