hyljava

          Spring MVC使用動(dòng)態(tài)代理實(shí)現(xiàn)事務(wù)控制

          Spring MVC使用動(dòng)態(tài)代理實(shí)現(xiàn)事務(wù)控制
          applicationContext.xml文件中配置

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
          xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
          xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
          default-lazy-init="true">
          <!--
          spring在啟動(dòng)的時(shí)候,會(huì)默認(rèn)加載會(huì)默認(rèn)加載整個(gè)對(duì)象實(shí)例圖,從初始化ACTION配置、到
          service配置到dao配置、乃至到數(shù)據(jù)庫連接、事務(wù)等等。這樣可以減少web服務(wù)器在運(yùn)行時(shí)的負(fù)擔(dān),但是對(duì)于開發(fā)者來說無疑是效率極低的一個(gè)設(shè)置了。
          還好,spring提供了default-lazy-init屬性,其配置形式如下,applicationContext.xml中: <
          beans default-lazy-init ="true" > < bean class ="org.xxxx.bean" >
          。。。。。。 </beans>
          spring配置默認(rèn)default-lazy-init為false,當(dāng)配置為true時(shí)sping不會(huì)再去加載整個(gè)對(duì)象實(shí)例圖,大大減少了初始化的時(shí)間,減少了spring的啟動(dòng)速度。
          這樣做只是為了在開發(fā)過程中節(jié)約啟動(dòng)時(shí)間,在部署到實(shí)際環(huán)境中,倒是沒必要設(shè)置default-lazy-init為true。畢竟部署到實(shí)際環(huán)境中不是經(jīng)常的事,每次啟動(dòng)1分鐘倒不是大問題,而且可以提高服務(wù)器效率。
          當(dāng)然,也不是所有的beans都能設(shè)置default-lazy-init成為true.對(duì)于scheduler的bean不能用lazy-init
          < beans default-lazy-init ="true" > < bean class
          ="org.springframework.scheduling.quartz.SchedulerFactoryBean" > <
          property name ="triggers" > < list > < ref bean ="buildHtmlTrigger" />
          < ref bean ="askTrigger" /> < ref bean ="mailSenderTrigger" /> < ref
          bean ="topicDetailBuildTrigger" /> < ref bean ="forumBuildTrigger" />
          < ref bean ="topicBuildTrigger" /> </ list > </ property > </ bean >
          </ beans > 這樣的話。所有的scheduler就都不管用了。所以請(qǐng)大家要注意。
          -->
           
          <!-- 使用annotation 自動(dòng)注冊(cè)bean, 并保證@Required、@Autowired的屬性被注入 -->
          <context:component-scan base-package="com.edufe">
          <context:exclude-filter type="annotation"
          expression="org.springframework.stereotype.Controller" />
          </context:component-scan>
          <context:property-placeholder
          ignore-resource-not-found="true"
          location="classpath*:/application.properties,
                     classpath*:/application.development.properties" />
          <!-- 創(chuàng)建數(shù)據(jù)源 -->
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
          <property name="driverClassName" value="${jdbc.driver}" />
          <property name="url" value="${jdbc.url}" />
          <property name="username" value="${jdbc.username}" />
          <property name="password" value="${jdbc.password}" />
          </bean>
          <!-- 使用嵌入式數(shù)據(jù)庫H2 -->
          <!--
          <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script
          location="classpath:sql/h2/schema.sql" /> <jdbc:script
          location="classpath:sql/h2/import-data.sql" />
          </jdbc:embedded-database>
          -->
          <!-- 創(chuàng)建jdbcTemplate對(duì)象 -->
          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
          <property name="dataSource" ref="dataSource" />
          </bean>
          <!-- 在容器文件中配置bean(service,dao,domain,action,數(shù)據(jù)源), -->
          <!--
          bean的作用是, 當(dāng)我們spring框架加載的時(shí)候,spring就會(huì)自動(dòng)創(chuàng)建一個(gè)bean,并放入內(nèi)存 即產(chǎn)生UserService
          user=new UserService(); user.setName("張三");
          -->
          <!--
          <bean id="userService" class=""> 這里就體現(xiàn)出了注入的概念 <property name="name">
          <value>張三</value> </property> 在UserService中引用ByeService的對(duì)象ref是個(gè)引用
          <property name="byeS" ref="byeService" /> </bean>
          -->
           
           
          <!-- 處理事務(wù) -->
          <!-- 生成一個(gè)事務(wù)管理對(duì)象 -->
          <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <constructor-arg index="0" ref="dataSource">
          </constructor-arg>
          </bean>
          <!-- 生成默認(rèn)事務(wù)定義對(duì)象 -->
          <bean id="def" class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>
           
          </beans>

          在dao中
          @Autowired
          private DataSourceTransactionManager transactionManager;
          @Autowired
          private DefaultTransactionDefinition def;
          public int excuteTrac() {
          int temp = 0;
          // 批插入
          String sql1[] = new String[4];
          // 向第一個(gè)表插入的語句
          sql1[0] = "insert into usermbo( ID, USERNAME, age) values('122','22','22')";
          sql1[1] = "insert into usermbo( ID, USERNAME, age) values('133','33','33')";
          sql1[2] = "insert into usermbo( ID, USERNAME, age) values('144','44','33')";
          sql1[3] = "insert into usermbo( ID, USERNAME, age) values('155','55','33')";
          String[] sql2 = new String[3];
          // 向第二個(gè)表插入的語句
          sql2[0] = "insert into address (NO, NAME) values('33','33')";
          // 此條數(shù)據(jù)是錯(cuò)誤數(shù)據(jù) 插入會(huì)出現(xiàn)異常
          sql2[1] = "insert into address (NO, NAME)  values('eee','44')";
          sql2[2] = "insert into address (NO, NAME)  values('144','44')";
           
          TransactionStatus status = transactionManager.getTransaction(def);
          try {
          int[] a = jdbcTemplate.batchUpdate(sql1);
          int[] b = jdbcTemplate.batchUpdate(sql2);
          try {
          transactionManager.commit(status);
          } catch (Exception e) {
          System.out.println("事務(wù)提交異常");
          }
          } catch (Exception ex) {
          System.out.println("出現(xiàn)事務(wù)異常");
          try {
          transactionManager.rollback(status);
          } catch (IllegalTransactionStateException e) {
          System.out.println("回滾數(shù)據(jù)異常");
          }
          temp = -1;
          }
          return temp;
          }

          posted on 2013-04-19 10:56 何云隆 閱讀(6045) 評(píng)論(0)  編輯  收藏 所屬分類: SpringSpring MVC


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南昌县| 南溪县| 库车县| 家居| 中牟县| 台中市| 聊城市| 开远市| 当雄县| 永善县| 东莞市| 十堰市| 隆昌县| 怀远县| 长子县| 黔东| 自贡市| 岳西县| 保德县| 常宁市| 孙吴县| 九龙坡区| 庐江县| 蒲江县| 宁河县| 镇赉县| 新邵县| 黎平县| 建水县| 祥云县| 同德县| 玉龙| 西吉县| 石河子市| 玛曲县| 资源县| 大冶市| 应城市| 周口市| 青田县| 抚顺市|