3
Transaction
Calvin edited this page 2013-11-14 01:37:15 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##Spring Transactions Spring最可爱的地方之一就是AOP事务控制了,不多说。在SpringSide里,事务坐在Service层,而且因为Service层的方法里,只读的事务居多,因此在Class Level上默认设置@Transactional(readOnly = true),而在save,delete方法再注释@Transactional(readOnly = false)。

Spring有很多TransactionManager实现,通常能管住Hibernate/JPA的高级TransactionManager,也能在JDBC场景里使用,反之则不行。

##多数据源 多数据源分为两种场景:

一种是要求多个数据源使用同一事务-- 一次事务可能访问多个数据源,要求一起提交或回退。 这是JTA规范的事情,应用服务器会自带实现,而Tomcat/Jetty可以另外找独立的开源实现。但在SpringSide里因为不好Demo,没有演示。

一种只是一个应用里有多个数据源并存,但不要求参与同一事务。在showcase里,就有showcase的数据库 和 quartz的数据库两个数据源。 这时候需要定义两个数据源,两个TransactionManager。然后,定义AOP事务使用哪一个TransactionManager。另一个就要自己想办法了.....

	<tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />

然后注意在使用Spring ApplicationContext的测试用例里,也要显式定义自己用到的TransactionManager。

@TransactionConfiguration(transactionManager="defaultTransactionManager")
public class JpaMappingTest extends SpringTxTestCase