MDA/MDD/TDD/DDD/DDDDDDD
posts - 536, comments - 111, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

aop配置事务

Posted on 2009-12-24 22:04 leekiang 阅读(278) 评论(0)  编辑  收藏 所属分类: spring
事务直接配到DAO上
<bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="baseTxProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        lazy-init="true" abstract="true">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="studentDaoProxy" parent="baseTxProxy">
        <property name="target">
            <ref bean="studentDao" />
        </property>
    </bean>

    <bean id="studentDao" class="com.dao.StudentDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    StudentDao st = (StudentDao) context.getBean("studentDaoProxy");
    
    两个bean也可以合并为
    <bean id="studentDao" parent="baseTxProxy">
        <property name="target">
            <bean class="com.dao.StudentDaoImpl">
                <property name="dataSource" ref="dataSource" />
                <property name="kpiDao" ref="kpiDao" />
            </bean>
        </property>
    </bean>
    StudentDao st = (StudentDao) context.getBean("studentDao");
    上述这种方式必须使用接口,为什么。
    
    
    第二种同样必须用接口。配置起来比第一种麻烦
        <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean id="studentDaoProxy"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="studentDao" />
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>
    
    <bean id="studentDao" class="com.dao.StudentDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentDao st = (StudentDao) context.getBean("studentDaoProxy");
    
    如果使用的都是接口,那么就不需要用cglib-nodep-2.1_3.jar
    如果service调dao没有用到接口,那么必须用cglib-nodep-2.1_3.jar


事务策略: 了解事务陷阱 http://www.ibm.com/developerworks/cn/java/j-ts1.html 事务策略: 高并发策略 http://www.ibm.com/developerworks/cn/java/j-ts5/index.html 这是一个系列.

http://beet.sourceforge.net
Beet records user behavior and performance data for your Spring-based Java application.  It can thus help you to analyze usage patterns and research production performance issues.

只有注册用户登录后才能发表评论。


网站导航: