在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),查看数据库没有插入任何记录,说明回滚了,事务使用正常