銆銆key灞炴х‘瀹氫唬鐞嗗簲璇ョ粰鍝釜鏂規硶澧炲姞浜嬪姟琛屼負銆傝繖鏍風殑灞炴ф渶閲嶈鐨勯儴浠芥槸浼犳挱琛屼負銆傛湁浠ヤ笅閫夐」鍙緵浣跨敤錛?/P>
鍓嶅叚涓瓥鐣ョ被浼間簬EJB CMT錛氬父閲忓悕鐩稿悓錛屽洜姝わ紝瀵笶JB寮鍙戜漢鍛樻潵璇達紝搴旇绔嬪埢灝辨劅鍒扮啛鎮夈傜涓冧釜錛圥ROPAGATION_NESTED錛夋槸Spring鎵鎻愪緵鐨勪竴涓壒孌婂彉閲忋傚畠瑕佹眰浜嬪姟綆$悊鍣ㄦ垨鑰呬嬌鐢↗DBC 3.0 Savepoint API鎻愪緵宓屽浜嬪姟琛屼負錛堝Spring鐨凞ataSourceTransactionManager錛夛紝鎴栬呴氳繃JTA鏀寔宓屽浜嬪姟銆?/P>
銆銆銆銆
浜屻両solation Level(浜嬪姟闅旂絳夌駭):
1銆丼erializable錛氭渶涓ユ牸鐨勭駭鍒紝浜嬪姟涓茶鎵ц錛岃祫婧愭秷鑰楁渶澶э紱
2銆?STRONG>REPEATABLE READ錛?/STRONG>淇濊瘉浜嗕竴涓簨鍔′笉浼氫慨鏀瑰凡緇忕敱鍙︿竴涓簨鍔¤鍙栦絾鏈彁浜わ紙鍥炴粴錛夌殑鏁版嵁銆傞伩鍏嶄簡鈥滆剰璇誨彇鈥濆拰鈥滀笉鍙噸澶嶈鍙栤濈殑鎯呭喌錛屼絾鏄甫鏉ヤ簡鏇村鐨勬ц兘鎹熷け銆?BR>3銆?STRONG>READ COMMITTED:澶у鏁頒富嫻佹暟鎹簱鐨勯粯璁や簨鍔$瓑綰э紝淇濊瘉浜嗕竴涓簨鍔′笉浼氳鍒板彟涓涓茍琛屼簨鍔″凡淇敼浣嗘湭鎻愪氦鐨勬暟鎹紝閬垮厤浜嗏滆剰璇誨彇鈥濄傝綰у埆閫傜敤浜庡ぇ澶氭暟緋葷粺銆?BR>4銆丷ead Uncommitted錛氫繚璇佷簡璇誨彇榪囩▼涓笉浼氳鍙栧埌闈炴硶鏁版嵁銆?BR>
spring涓殑Isolation灞炴э細
1銆両SOLATION_DEFAULT 錛氫嬌鐢ㄥ綋鍓嶆暟鎹簮鐨勯粯璁ょ駭鍒?BR>2銆両SOLATION_READ_UNCOMMITTED 錛欴irty reads, non-repeatable reads, and phantom reads can occur.
3銆両SOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
4銆両SOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5銆両SOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.
涓夈乺eadOnly
浜嬪姟灞炴т腑鐨剅eadOnly鏍囧織琛ㄧず瀵瑰簲鐨勪簨鍔″簲璇ヨ鏈浼樺寲涓哄彧璇諱簨鍔°傝繖鏄竴涓渶浼樺寲鎻愮ず銆傚湪涓浜涙儏鍐典笅錛屼竴浜涗簨鍔$瓥鐣ヨ兘澶熻搗鍒版樉钁楃殑鏈浼樺寲鏁堟灉錛屼緥濡傚湪浣跨敤Object/Relational鏄犲皠宸ュ叿錛堝錛欻ibernate鎴朤opLink錛夋椂閬垮厤dirty checking錛堣瘯鍥鋸滃埛鏂扳濓級銆?BR>
鍥涖乀imeout
鍦ㄤ簨鍔″睘鎬т腑榪樻湁瀹氫箟鈥渢imeout鈥濆肩殑閫夐」錛屾寚瀹氫簨鍔¤秴鏃朵負鍑犵銆傚湪JTA涓紝榪欏皢琚畝鍗曞湴浼犻掑埌J2EE鏈嶅姟鍣ㄧ殑浜嬪姟鍗忚皟紼嬪簭錛屽茍鎹寰楀埌鐩稿簲鐨勮В閲娿?BR>
浜嬪姟鍒掑垎絳栫暐
1銆佹帹鑽愬湪涓氬姟灞備嬌鐢ㄤ簨鍔★紝榪欐牱鍙互鍏佽涓氬姟灞傛崟鑾峰鑷磖ollback鐨勫紓甯革紝騫舵姏鍑烘伆褰撶殑涓氬姟灞傚紓甯革紱涓嶅湪dao灞備嬌鐢ㄤ簨鍔℃槸鍥犱負榪欎細闄愬埗浜哾ao閲嶇敤鍏朵粬浜嬪姟闇姹傦紝騫朵笖dao灞傛病鏈夊疄鐜頒笟鍔¢昏緫錛屽茍涓斿師瀛愭т篃鏄笟鍔″眰鐨勬蹇點?BR>
spring澹版槑鎬т簨鍔$殑鍒掑垎錛?BR>1銆佹湁鍥涗釜鍦版柟闇瑕侀厤緗細The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.
2銆佷嬌鐢≒roxyFactoryBean/Transaction Interceptor錛?SPAN class=fixed>transactionInterceptor錛夐厤緗畇pring浜嬪姟
浠ヤ笅涓洪厤緗疄渚嬶細
<!-- The DBCP DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"><value>${jdbc.url}</value></property> <property name="username"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <!-- The DAO class --> <bean id="dao" class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- The transactionmanager to use for regular non JTA datasource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- TransactionInterceptor --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributeSource"> <value> org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,re adOnly org.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIR ED </value> </property> </bean> <!-- Transactional proxy for the primary business object --> <bean id="boxOffice" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="boxOfficeTarget"/> </property> <property name="proxyInterfaces"> <value>org.springframework.prospring.ticket.service.BoxOffice</value> </property> <property name="interceptorNames"> <value>transactionInterceptor</value> </property> </bean> <!-- Business Object --> <bean id="boxOfficeTarget" class="org.springframework.prospring.ticket.service.BoxOfficeImpl"> <property name="boxOfficeDao"> <ref local="dao"/> </property> </bean>
3銆佷嬌鐢═ransactionProxyFactoryBean閰嶇疆spring浜嬪姟
浠ヤ笅涓洪厤緗疄渚嬶細
<!-- The DBCP DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"><value>${jdbc.url}</value></property> <property name="username"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <!-- The DAO class --> <bean id="dao" class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- The transactionmanager to use for regular non JTA datasource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- Transactional proxy and the primary business object --> <bean id="boxOffice" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="target"> <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl"> <property name="boxOfficeDao"> <ref local="dao"/> </property> </bean> </property> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="allocate*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
4銆佷嬌鐢˙eanNameAutoProxyCreator閰嶇疆spring浜嬪姟
濡傛灉鏈夊ぇ閲忕殑bean闇瑕佷嬌鐢ㄤ簨鐗╋紝閭d箞鍙鍦ㄩ厤緗枃浠朵腑鎻愪緵bean name緇橞eanNameAutoProxyCreator錛宻pring灝變細涓粰璇ean鎻愪緵浜嬪姟浠g悊錛岄厤緗疄渚嬪涓嬶細
<!-- The DBCP DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"><value>${jdbc.url}</value></property> <property name="username"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <!-- The DAO class --> <bean id="dao" class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- The transactionmanager to use for regular non JTA datasource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!-- TransactionInterceptor --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributeSource"> <value> org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS ,readOnly org.springframework.prospring.ticket.service.BoxOffice.allocate*= PROPAGATION_REQUIRED </value> </property> </bean> <!-- BeanNameAutoProxyCreator --> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <value>transactionInterceptor</value> </property> <property name="beanNames"> <list> <idref local="boxOffice"/> </list> </property> </bean> <!-- Business Object --> <bean id="boxOffice" class="org.springframework.prospring.ticket.service.BoxOfficeImpl"> <property name="boxOfficeDao"> <ref local="dao"/> </property> </bean>