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

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

<!-- 配置数据库的事务管理器,用于ibatis -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource"><ref local="oracle_datasource"/></property>
	</bean>
	
	<osgi:service ref="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager"  />
	
	<osgi:service ref="localSequenceForPkgid" interface="org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer"  />
	
	<bean id="localSequenceForPkgid" class="com.ferry.oracleSequence.OracleSequence">
		<property name="dataSource">
			<ref local="oracle_datasource"/>
		</property>
		<property name="incrementerName">
			<value>PKGID</value>
		</property>
		<property name="sequenceLength">
			<value>8</value>
		</property>
	</bean>


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

<bean id="databaseService" class="com.ferry.databaseService.impl.DatabaseService">   
    	<property name="sqlDao_i" ref="sqlDao_i"></property>
    </bean>   
    
    <osgi:service id="database" ref="databaseService"  
        interface="com.ferry.databaseService.IDatabaseService">   
    </osgi:service>  
     
    <osgi:reference id="sqlDao_i" interface="com.ferry.daoService.IbatisGenericDao">
    </osgi:reference>
    
    <osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager">
    </osgi:reference>
    
    <!-- 配置数据库的事务属性。如哪些方法需要进行事务管理,用于ibatis--> 
	<bean id="transactionAttributes"
		class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
		<property name="properties">
			<props>
				<prop key="*_itransc">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置数据库事务拦截器,用于ibatis -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref local="transactionManager" />
		</property>
		<property name="transactionAttributeSource">
			<ref local="transactionAttributes" />
		</property>
	</bean>
	
	<!-- 配置aop代理,当符合拦截条件时,自动激发拦截-->
	<bean id="transcationProxy"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
	</bean>

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

导入需要的依赖bundle和包


事务方法如下

@Override
	public void insertHeroes_itransc(List<Hero> 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中添加服务引用

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

public void _insertHero(CommandInterpreter ci){
		if(databaseService!=null&&localSequenceForPkgid!=null){
			List<Hero> heroList = new ArrayList<Hero>();
			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),查看数据库没有插入任何记录,说明回滚了,事务使用正常

点击下载demo,里面有依赖的jar

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值