spring學習之事務

來源:互聯網
上載者:User

研究struts和spring也有些日子了,雖然有些地方還不甚瞭解,不過大體已經瞭解struts+spring+hibernate的開發流程了和架構了,對我以後開發.net和java都有不小的協助,感覺收穫不小.下面介紹一下幾個關鍵的地方:
    一:利用spring控制事務
        1首先在applicationContext.xml配置DataSource
           <bean id="dataSource"
           class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                  <property name="driverClassName">
                       <value>com.mysql.jdbc.Driver</value>
                  </property>
                  <property name="url">
                       <value>jdbc:mysql://localhost:3306/test</value>
                  </property>
                  <property name="username">
                       <value>root</value>
                 </property>
                 <property name="password">
                       <value>sa</value>
                 </property>
            </bean>
         以上配置資訊主要是用來串連資料庫的配置.
        2其次在applicationContext.xml配置sessionFactory
  <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="mappingResources">
   <list>
    <value>com/java/crud/Hibernate/Curdtest.hbm.xml</value>
    <value>com/java/crud/Hibernate/Curdtemp.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
</bean>
以上主要配置hibernate得session
         3配置事務          
<bean id="crudtestDao" class="com.java.crud.Dao.CrudtestImpl">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 
bean"crudtestDao"是本程式的業務方法用於實現增,刪,改,查,它需要引用session bean.
 
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
配置hibernate的事務,需要引用session bean.

 <bean id="crudtestDaoTarget"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="target">
   <ref bean="crudtestDao" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <bean name = "/crud" class = "com.java.curd.struts.action.CrudAction">
  <property name="crudTestDao" ref="crudtestDaoTarget"></property>
 </bean>
這個配置是spring控制事務的重點首先這個bean是事務工廠代理類,它需要引用三個屬性來配置.
一:transactionManager即hibernate的transactionManager.
二:target你要管理事務的業務bean即要對此bean中的方法進行事務控制.
三:事務工廠屬性.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.