<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hyljava

    Spring MVC使用動態代理實現事務控制

    Spring MVC使用動態代理實現事務控制
    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在啟動的時候,會默認加載會默認加載整個對象實例圖,從初始化ACTION配置、到
    service配置到dao配置、乃至到數據庫連接、事務等等。這樣可以減少web服務器在運行時的負擔,但是對于開發者來說無疑是效率極低的一個設置了。
    還好,spring提供了default-lazy-init屬性,其配置形式如下,applicationContext.xml中: <
    beans default-lazy-init ="true" > < bean class ="org.xxxx.bean" >
    。。。。。。 </beans>
    spring配置默認default-lazy-init為false,當配置為true時sping不會再去加載整個對象實例圖,大大減少了初始化的時間,減少了spring的啟動速度。
    這樣做只是為了在開發過程中節約啟動時間,在部署到實際環境中,倒是沒必要設置default-lazy-init為true。畢竟部署到實際環境中不是經常的事,每次啟動1分鐘倒不是大問題,而且可以提高服務器效率。
    當然,也不是所有的beans都能設置default-lazy-init成為true.對于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就都不管用了。所以請大家要注意。
    -->
     
    <!-- 使用annotation 自動注冊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" />
    <!-- 創建數據源 -->
    <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>
    <!-- 使用嵌入式數據庫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>
    -->
    <!-- 創建jdbcTemplate對象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 在容器文件中配置bean(service,dao,domain,action,數據源), -->
    <!--
    bean的作用是, 當我們spring框架加載的時候,spring就會自動創建一個bean,并放入內存 即產生UserService
    user=new UserService(); user.setName("張三");
    -->
    <!--
    <bean id="userService" class=""> 這里就體現出了注入的概念 <property name="name">
    <value>張三</value> </property> 在UserService中引用ByeService的對象ref是個引用
    <property name="byeS" ref="byeService" /> </bean>
    -->
     
     
    <!-- 處理事務 -->
    <!-- 生成一個事務管理對象 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg index="0" ref="dataSource">
    </constructor-arg>
    </bean>
    <!-- 生成默認事務定義對象 -->
    <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];
    // 向第一個表插入的語句
    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];
    // 向第二個表插入的語句
    sql2[0] = "insert into address (NO, NAME) values('33','33')";
    // 此條數據是錯誤數據 插入會出現異常
    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("事務提交異常");
    }
    } catch (Exception ex) {
    System.out.println("出現事務異常");
    try {
    transactionManager.rollback(status);
    } catch (IllegalTransactionStateException e) {
    System.out.println("回滾數據異常");
    }
    temp = -1;
    }
    return temp;
    }

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


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 妞干网免费视频在线观看| 精品亚洲福利一区二区| 一级毛片高清免费播放| 性色av免费观看| 7777久久亚洲中文字幕| 男男AV纯肉无码免费播放无码| 亚洲无人区码一二三码区别图片 | 99久久99热精品免费观看国产| 色久悠悠婷婷综合在线亚洲| 亚洲系列中文字幕| 国产精品亚洲а∨天堂2021| 国产一级淫片免费播放电影| 男女猛烈激情xx00免费视频| 亚洲精品国产日韩无码AV永久免费网 | 男女男精品网站免费观看 | 亚洲AV永久精品爱情岛论坛| 曰批全过程免费视频在线观看无码 | 亚洲国产V高清在线观看| jizz18免费视频| 亚洲爆乳无码一区二区三区| 最好看的中文字幕2019免费| 亚洲国产成人久久精品99| 又大又硬又粗又黄的视频免费看| 亚洲无线一二三四区手机| 成人A片产无码免费视频在线观看| 亚洲自偷自偷精品| 久久精品女人天堂AV免费观看| 亚洲AV无码一区二区三区久久精品 | 老司机亚洲精品影视www| 国产一区二区免费视频| 日本亚洲精品色婷婷在线影院| 永久免费av无码网站大全| 久久精品无码专区免费| 免费大黄网站在线观看| 国产精品亚洲综合久久 | 亚洲成a人片77777老司机| 免费精品国偷自产在线在线| 亚洲成人高清在线观看| 国产免费观看黄AV片| 男女午夜24式免费视频| 亚洲av色影在线|