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

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

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

    posts - 495,  comments - 11,  trackbacks - 0

    2聲明式管理Hibernate本地事務

    Spring提供了一種統一的IoC方式來管理Hibernate事務(本地或者分布式事務)。從Spring接手hibernate.cfg.xmlHibernate的基本配置文件)起,Hibernate事務便輕易交由Spring拖管了。

    說明:在上一章介紹IBatisDAO的時候,曾經針對事務和DAO的關系簡單的進行了探討。通常DAO的粒度應該都是比較細的,即它們只是一些單步的CRUD操作,所以就需要引入一個業務對象來包裹DAO,這樣,就可以在業務對象的基礎上,提供更粗粒度的事務劃分了(比如跨越多個DAO的方法調用進行事務管理)。

    為了能對DAO進行更粗粒度的事務控制,需要為其增加一個業務對象。下面給出了該業務對象的接口和實現,如代碼10.25~10.26所示。

    代碼10.25 StockFacade.java

    package chapter10.spring.hibernate;

    import chapter10.hibernate.domain.Category;

    public interface StockFacade {

    public void business1(Category category);

    public void someOtherBusiness();

    }

    代碼10.26 BusinessFacadeImpl.java

    public class BusinessFacadeImpl implements StockFacade {

    private StockDao stockDao;

    public void setStockDao(StockDao stockDao) {

    ??? this.stockDao = stockDao;

    }

    public void business1(Category category) {

    ??? stockDao.createCategoryCascade(category);

    ??? stockDao.retrieveProductBy(category);

    ??? stockDao.deleteCategoryCascade(category);

    }

    public void someOtherBusiness() {

    ??? //other implemention

    }

    }

    接著給出關于事務策略的配置,其中使用了Spring針對Hibernate3給出的HibernateTransactionManager,它提供了Hibernate的本地事務管理策略,如代碼10.27所示。

    代碼10.27 transaction-context.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="transactionManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    ??? <property name="sessionFactory" >

    ????? <ref bean="sessionFactory" />

    ??? </property>

    </bean>

    <bean id="business"

    class="chapter10.spring.hibernate.BusinessFacadeImpl">

    ??? <property name="stockDao">

    ????? <ref bean="stockDao"/>

    ??? </property>

    </bean>

    <bean id="businessProxy"

    ??? class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

    ??? <property name="transactionManager">

    ????? <ref bean="transactionManager" />

    ??? </property>

    ??? <property name="target">

    ??? ?? <ref bean="business" />

    ??? </property>

    ??? <property name="transactionAttributes">

    ????? <props>

    ??????? <!--運行在當前事務范圍內,如果當前沒有啟動事務,那么創建一個新的事務-->

    ??????? <prop key="business*">PROPAGATION_REQUIRED</prop>

    ??????? <!--運行在當前事務范圍內,如果當前沒有啟動事務,那么拋出異常-->

    ??? ???? <prop key="someOtherBusiness*">PROPAGATION_MANDATORY</prop>

    ????? </props>

    ??? </property>

    </bean>

    </beans>

    代碼10.28 HibernateTransactionUsageTest.java

    package chapter10.spring.hibernate;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import chapter10.hibernate.domain.Category;

    import junit.framework.TestCase;

    public class HibernateTransactionUsageTest extends TestCase {

    private StockFacade stockBusiness;

    protected void setUp() throws Exception {

    ??? String path = "ch10/spring/hibernate/";

    ??? ApplicationContext ctx = new ClassPathXmlApplicationContext(

    ???????? new String[]{path+"dataAccessContext-support-local.xml",

    ???????????? path+"transaction-context.xml"});

    ??? stockBusiness = (StockFacade)ctx.getBean("businessProxy");

    }

    public void testTransctionUsage() {

    ??? Category category = new Category("RABBIT");

    ??? category.setName("Rabbit");

    ??? category.setDescn("Desciption of Rabbit");

    ??? stockBusiness.business1(category);

    }

    }

    posted on 2009-07-27 01:51 jadmin 閱讀(73) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 毛片在线免费视频| 亚洲人成人无码网www电影首页| 国产无遮挡吃胸膜奶免费看 | 亚洲视屏在线观看| 日韩亚洲综合精品国产| 免费91最新地址永久入口| 国产高清视频在线免费观看| 亚洲VA中文字幕无码毛片| 亚洲精品无码成人| 99热免费在线观看| 亚洲国产精品13p| 男人天堂2018亚洲男人天堂| 久久一本岛在免费线观看2020| 全亚洲最新黄色特级网站 | 免费看美女被靠到爽| 在线免费观看亚洲| 99爱在线精品免费观看| 亚洲ⅴ国产v天堂a无码二区| 视频一区二区三区免费观看| 国产99视频精品免费观看7| 亚洲网站在线观看| 三年片免费高清版 | 久久亚洲AV成人无码| 色窝窝免费一区二区三区 | 久久久免费的精品| 亚洲av无码国产综合专区| 91人人区免费区人人| 亚洲V无码一区二区三区四区观看| 99ee6热久久免费精品6| 亚洲国产成人精品无码区花野真一 | 最近中文字幕2019高清免费| 亚洲熟妇无码一区二区三区导航| 综合在线免费视频| 最新亚洲人成网站在线观看 | 国产亚洲情侣一区二区无| 国产日韩AV免费无码一区二区三区| 亚洲av麻豆aⅴ无码电影| 全免费a级毛片免费看| 水蜜桃亚洲一二三四在线| 成人黄软件网18免费下载成人黄18免费视频| 久久久亚洲AV波多野结衣|