<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 何云隆 閱讀(6045) 評論(0)  編輯  收藏 所屬分類: SpringSpring MVC


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


    網站導航:
     
    主站蜘蛛池模板: 国产人成免费视频| 久久久亚洲欧洲日产国码aⅴ| 四虎影视久久久免费观看| 亚洲精品成人片在线观看精品字幕| 18女人水真多免费高清毛片| 亚洲AV永久无码精品一福利| 国产AV无码专区亚洲AWWW| 57pao国产成永久免费视频| 黄页免费视频播放在线播放| 亚洲色四在线视频观看| 国产成人无码免费视频97| 久草免费福利视频| 亚洲精品一卡2卡3卡四卡乱码| 在线观看午夜亚洲一区| 久久这里只有精品国产免费10| 91av免费在线视频| 亚洲色成人网站WWW永久四虎| 国产亚洲精品a在线观看app| 日本特黄特色aa大片免费| 免费在线观影网站| 国产区图片区小说区亚洲区| 久久久久亚洲AV无码专区首JN| 亚洲国产高清在线一区二区三区| 永久免费视频网站在线观看| 久久av免费天堂小草播放| 亚洲精华国产精华精华液好用 | 又黄又爽无遮挡免费视频| 足恋玩丝袜脚视频免费网站| sss在线观看免费高清| 亚洲AV成人影视在线观看| 久久噜噜噜久久亚洲va久| xvideos亚洲永久网址| 青娱乐免费在线视频| 永久在线观看免费视频| 日本永久免费a∨在线视频| 亚洲色偷偷偷综合网| 亚洲伊人久久大香线焦| 日韩精品一区二区亚洲AV观看 | 99国产精品免费观看视频| 丝袜足液精子免费视频| 男女猛烈激情xx00免费视频|