Spring学习笔记(九)-----事务管理(宣告式事务管理)

1、先声明一个数据源
<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  <property name="url"
   value="jdbc:sqlserver://192.168.1.101:1433;DatabaseName=citydb" />
  <property name="username">
   <value>sa</value>
  </property>
  <property name="password">
   <value></value>
  </property>
  <property name="maxActive">
   <value>20</value>
  </property>
  <property name="maxIdle">
   <value>10</value>
  </property>
  <property name="maxOpenPreparedStatements">
   <value>5</value>
  </property>
 </bean>
2、将数据源注入到transactionManager中,交给DataSourceTransactionManager进行事务管理
<bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
  <property name="nestedTransactionAllowed">
   <value>true</value>
  </property>
 </bean>
3、将数据源注入到sqlMapClient中
<bean id="sqlMapClient"
  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation"
   value="WEB-INF/sql-map-config.xml" />
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
 </bean>
4、声明事务
<bean id="baseTransactionProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  abstract="true">
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_NESTED</prop>
    <prop key="update*">PROPAGATION_NESTED</prop>
    <prop key="delete*">PROPAGATION_NESTED</prop>
    <prop key="*">PROPAGATION_NESTED,readOnly</prop>
   </props>
  </property>
 </bean>
5、创建DAO
<bean id="reportDAO" class="com.bodhiya.report.dao.ReportDAO">
  <property name="sqlMapClient">
   <ref bean="sqlMapClient" />
  </property>
 </bean>
6、创建代理使DAO与事务关联
<bean id="countyProxy" parent="baseTransactionProxy">
  <property name="target">
   <bean class="com.bodhiya.unit.proxy.CountyInfoProxy">
    <property name="ci" ref="countyDAO"></property>
   </bean>
  </property>
 </bean>


 






你可能感兴趣的:(spring)