Re: Are nested transactions supported?

170 views
Skip to first unread message

Eduardo Macarron

unread,
Nov 23, 2012, 12:57:23 PM11/23/12
to mybati...@googlegroups.com
Yes, nested tx are supported. However, given that the test scenario is
complex. I would suggest to test it with plain jdbc, and try MyBatis
when it works.

Eduardo Macarron

unread,
Nov 23, 2012, 3:36:56 PM11/23/12
to mybati...@googlegroups.com
BTW I suppose you are using an SqlSessionTemplate, not a SqlSession
got from the SqlSessionFactory.

2012/11/23 Eduardo Macarron <eduardo....@gmail.com>:

Mark Peters

unread,
Nov 23, 2012, 6:01:46 PM11/23/12
to mybati...@googlegroups.com
Thanks for the response Eduardo.

We actually aren't using either directly, but are rather using MapperScannerConfigurer to provide the DAOs.  And actually, the MapperScannerConfigurer was being wired directly with the SqlSessionFactory.  I switched to passing in a SqlSessionTemplate but got the same result; I think some magic along the line was causing the earlier approach to use SqlSessionTemplate anyway.

I've done some debugging of the MyBatis code and I don't see code where the local cache respects nested transactions.  When SqlSessionTemplate decides which SqlSession to use, it asks SqlSessionUtils.getSqlSession.  This uses a Spring mechanism which associates a resource with a transaction.  However, this mechanism only seems to care about the outer transaction in a nested chain, so one SqlSession is used for the outer and inner transactions.  And I think that would be OK, but SqlSession does not seem to have its cache invalidated when the inner transaction is rolled back.  Nowhere do I see MyBatis take any interest in nested transactions.

Is local caching broken maybe?  It does seem to be overly aggressive, e.g. it caches even when using "read uncommitted" isolation.

If I can boil this down to a reproducible example, it'll probably still be quite a bit of code.  Is this an appropriate venue for that, or should I post it directly as a MyBatis issue?

Eduardo Macarron

unread,
Nov 24, 2012, 1:33:30 AM11/24/12
to mybati...@googlegroups.com
Hi Mark,

You are right. Sorry for my 1st reply, I wrote the "standard reply"
for a transaction question ;)

PROPAGATION_NESTED is not supported, and I am afraid it will require
an enormous effort.

All other propagation mechanisms
(PROPAGATION_REQUIRED/REQUIRES_NEW...) are supported though.

The problem with nested are is not in the Spring integration but in
the Core library. MyBatis does not support savepoints; this API just
does not exist:

SqlSession s1 = ....getSession();
s1.insert()
s1.setSavepoint("sp1");
s1.insert()
s1.rollback("sp1");

The problem is in fact in the caches (both 1st and 2nd level). They
are transactional but they do not suppport savepoints. They cannot
rollback a part of their data.

Mark Peters

unread,
Nov 24, 2012, 11:32:47 AM11/24/12
to mybati...@googlegroups.com
Thanks for the confirmation Eduardo. I think I saw an option for disabling the local cache, so I'll give that a shot. I'm trying to use this for some test infrastructure, not for production (do common test setup in outer transaction, then run test in inner transaction that is rolled back so the common setup only needs to run once).
Reply all
Reply to author
Forward
0 new messages