Posted on 2005-12-30 19:49
morcble的blog 阅读(209)
评论(0) 编辑 收藏 所属分类:
Spring
8.2. Transaction strategies
1。使用SPRING平台事务管理器
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
The PlatformTransactionManager definition will look like this:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
使用JTA事务管理器
If we use JTA, as in the dataAccessContext-jta.xml file from the same sample application, we need to use a
container DataSource, obtained via JNDI, and a JtaTransactionManager implementation. The
JtaTransactionManager doesn't need to know about the DataSource, or any other specific resources, as it will
use the container's global transaction management.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/jpetstore"/>>
</bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
以上两种方法实现的功能是等效的。
2。把hibernate和spring结合起来
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">①(局部事务)
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
①处With Hibernate and JTA transactions we could simply use the JtaTransactionManager as with JDBC or any
other resource strategy.
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>(JTA为全局事务)
声明式事务管理
<!-- this example is in verbose form, see note later about concise for multiple proxies! -->
<!-- the target bean to wrap transactionally -->
<bean id="petStoreTarget">
...
</bean>
<bean id="petStore" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager"/>
<property name="target" ref="petStoreTarget"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-MyCheckedException</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>