ITPub博客

首页 > 数据库 > Oracle > osgi 学习系列(十)osgi事务管理和对OracleSequence支持

osgi 学习系列(十)osgi事务管理和对OracleSequence支持

Oracle 作者:martin2008 时间:2013-09-26 18:08:19 0 删除 编辑

在com.ferry.daoService的配置文件增加对事务和OracleSequence的配置,并发布服务

  1. <bean id="transactionManager"
  2. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource"><ref local="oracle_datasource"/>property>
  4. bean>
  5. <osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager" />
  6. <osgi:service ref="localSequenceForPkgid" interface="org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer" />
  7. <bean id="localSequenceForPkgid" class="com.ferry.oracleSequence.OracleSequence">
  8. <property name="dataSource">
  9. <ref local="oracle_datasource"/>
  10. property>
  11. <property name="incrementerName">
  12. <value>PKGIDvalue>
  13. property>
  14. <property name="sequenceLength">
  15. <value>8value>
  16. property>
  17. bean>

        
                
        
        
        
        
        
        
        
                
                        
                
                
                        PKGID
                
                
                        8
                
        


在com.ferry.databaseService用到事务控制,该bundle中spring-dm配置文件如下

  1. <bean id="databaseService" class="com.ferry.databaseService.impl.DatabaseService">
  2. <property name="sqlDao_i" ref="sqlDao_i">property>
  3. bean>
  4. <osgi:service id="database" ref="databaseService"
  5. interface="com.ferry.databaseService.IDatabaseService">
  6. osgi:service>
  7. <osgi:reference id="sqlDao_i" interface="com.ferry.daoService.IbatisGenericDao">
  8. osgi:reference>
  9. <osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager">
  10. osgi:reference>
  11. <bean id="transactionAttributes"
  12. class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
  13. <property name="properties">
  14. <props>
  15. <prop key="*_itransc">PROPAGATION_REQUIREDprop>
  16. props>
  17. property>
  18. bean>
  19. <bean id="transactionInterceptor"
  20. class="org.springframework.transaction.interceptor.TransactionInterceptor">
  21. <property name="transactionManager">
  22. <ref local="transactionManager" />
  23. property>
  24. <property name="transactionAttributeSource">
  25. <ref local="transactionAttributes" />
  26. property>
  27. bean>
  28. <bean id="transcationProxy"
  29. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  30. <property name="interceptorNames">
  31. <list>
  32. <value>transactionInterceptorvalue>
  33. list>
  34. property>
  35. <property name="beanNames">
  36. <list>
  37. <value>*Servicevalue>
  38. list>
  39. property>
  40. bean>
   
        
       
    
       
      
     
    
    
    
    
    
    
     
        
                
                        
                                PROPAGATION_REQUIRED
                        
                
        
        
        
        
                
                        
                
                
                        
                
        
        
        
        
                
                        
                                transactionInterceptor
                        
                
                
                        
                                *Service
                        
                
        

上面定义的对以Service结尾的bean中,_itransc结尾的方法做事务控制。

导入需要的依赖bundle和包


事务方法如下

  1. @Override
  2. public void insertHeroes_itransc(List heroList) throws Exception{
  3. try {
  4. for(Hero hero:heroList){
  5. sqlDao_i.insertRecord("hero.insert", hero);
  6. }
  7. } catch (DataAccessException e) {
  8. logger.error("[DatabaseService:insertHeroes_itransc] insert hero to db error!",e);
  9. throw e;
  10. }
  11. }
@Override
        public void insertHeroes_itransc(List heroList) throws Exception{
                try {
                        for(Hero hero:heroList){
                                sqlDao_i.insertRecord("hero.insert", hero);
                        }
                } catch (DataAccessException e) {
                        logger.error("[DatabaseService:insertHeroes_itransc] insert hero to db error!",e);
                        throw e;
                }
        }


在com.ferry.bussinessService中要用到oracle 的sequence,需要在该bundle的component中添加服务引用

  1. <reference bind="setLocalSequenceForPkgid" cardinality="1..1" interface="org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer" name="DataFieldMaxValueIncrementer" policy="static" unbind="unsetLocalSequenceForPkgid"/>


  1. public void _insertHero(CommandInterpreter ci){
  2. if(databaseService!=null&&localSequenceForPkgid!=null){
  3. List heroList = new ArrayList();
  4. Hero hero = new Hero();
  5. hero.setPkgid(localSequenceForPkgid.nextStringValue());
  6. hero.setName("Tiny");
  7. hero.setBlood(600);
  8. hero.setMana(200);
  9. heroList.add(hero);
  10. heroList.add(hero);
  11. try {
  12. databaseService.insertHeroes_itransc(heroList);
  13. } catch (Exception e) {
  14. logger.error("insert hero to db error!");
  15. }
  16. }else {
  17. logger.error("init error!");
  18. }
  19. }
public void _insertHero(CommandInterpreter ci){
                if(databaseService!=null&&localSequenceForPkgid!=null){
                        List heroList = new ArrayList();
                        Hero hero = new Hero();
                        hero.setPkgid(localSequenceForPkgid.nextStringValue());
                        hero.setName("Tiny");
                        hero.setBlood(600);
                        hero.setMana(200);
                        heroList.add(hero);
                        heroList.add(hero);
                        try {
                                databaseService.insertHeroes_itransc(heroList);
                        } catch (Exception e) {
                                logger.error("insert hero to db error!");
                        }
                }else {
                        logger.error("init error!");
                }
        }

数据库中我将pkgid设为主键,插入两个相同的hero,运行后会报如下错ORA-00001: 违反唯一约束条件 (PLATFORM.PK_PKGID),查看数据库没有插入任何记录,说明回滚了,事务使用正常
<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7317705/viewspace-1116550/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论