开发者

Spring+hibernate and @Transactionl annotation, how it works?

开发者 https://www.devze.com 2023-01-23 01:19 出处:网络
what is the difference between use @Transactional over a method and not use it? I write some simple test but seem to be no difference.

what is the difference between use @Transactional over a method and not use it?

I write some simple test but seem to be no difference.

I want to learn how to manage transaction using spring and annotatio开发者_如何学Cn.

thank all.


If you put @Transactional over a method, every method call inside that accesses database (but also every method call inside the other @Transactional methods - depends how you set it up) uses only one database Connection. It means, that you can rollback all the updates/inserts that happened within the transaction. If one of the updates/inserts throws an Exception, you can rollback all the updates/inserts within transaction that happened before that exception -> data integrity

Simply put, if you have a @Transactional method which for instance uses spring JdbcTemplate to do some inserts or updates (no matter how many times), they will be transactional, in terms of if some exception is thrown or something, rollback is applied - you must read the documentation on details.

if you use @Transactional(propagation = Propagation.REQUIRED) on more then one method, if you then call these methods, all the inserts/updates into database withing these methods will be part of one big transaction.

IMPORTANT: as I mentioned in the comment, you must not call these method from the same class where they are declared. The reason is, that it is all based on AOP proxy calls which happens only if you instantiate the DAO object and invoke the @Transactional methods on the object. Otherwise it won't work and you wouldn't probably figure out why.

0

精彩评论

暂无评论...
验证码 换一张
取 消