向 A表和B表 插入数据 ,需要事务控制, A 表插入数据成功 ,B 插入数据不成功 (原因是故意出现插入到表的数据格式错误),查看数据库表 ,A 表的插入的数据还存在 ,事务并没有回滚,我的代码如下:
Service 接口类 :public String saveApp(@WebParam(name="sessionId") String sessionId,@WebParam(name="app") App app);Service 接口实现类public String saveApp(String sessionId,App app) throws TotalException {
try{
AppPojo appPojo=new AppPojo();
appPojo.setAppDesc(app.getAppDesc());
appPojo.setAppFee(app.getAppFee());
appPojo.setAppIcon("");
appPojo.setAppId(UUIDGenerator.getUUID());
appPojo.setAppName(app.getAppName());
appPojo.setAppNo(app.getAppNo());
appPojo.setAppType(app.getAppType());
VersionInfoPojo version=new VersionInfoPojo();
version.setPublishPath("");
version.setVersionId(UUIDGenerator.getUUID());
version.setAppId(appPojo.getAppId());
version.setPublishType("a");
dao.saveApp(appPojo);
dao.saveVersionInfo(version);
}catch(DataAccessException de){
System.out.println("service execute exception");
throw new TotalException("error");
}
return null;
}
TotalException 为自定义一个异常类 Dao 接口类 :public String saveApp(AppPojo apppojo);
public String saveVersionInfo(VersionInfoPojo version);
Dao 接口实现类 public String saveApp(AppPojo appPojo){
this.getSqlMapClientTemplate().insert("store.insertApp",appPojo);
//saveVersionInfo(appPojo.getVersionInfo());
return appPojo.getAppId();
}
public String saveVersionInfo(VersionInfoPojo version){
version.setVersionNo(StringUtil.dateToString("yyyyMMdd", new Date()));
version.setPublishTime(new Date());
this.getSqlMapClientTemplate().insert("store.insertVersion",version);
return null;
}
spring 配置文件 :<bean id="dataSource_apprsal"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbcapprsal.url}"></property>
<property name="username" value="${jdbcapprsal.username}"></property>
<property name="password" value="${jdbcapprsal.password}"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="1000"></property>
<property name="removeAbandoned" value="true"></property>
<property name="removeAbandonedTimeout" value="60"></property>
<property name="logAbandoned" value="true"></property>
</bean>
<!-- sql map client 配置 -->
<bean id="sqlMapClient_apprsal"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:/ibatis-sql-map-config-easepal.xml" />
<property name="dataSource" ref="dataSource_apprsal" />
</bean>
<!-- ************************************************************************-->
<!-- //////////////////////ibatis事务代理配置///////////////////////////////-->
<!-- ************************************************************************-->
<!-- 配置哪些类哪些方法使用事务 -->
<bean id="transactionManager_Apprsal"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_apprsal"></property>
</bean>
<tx:advice id="txAdvice_Apprsal" transaction-manager="transactionManager_Apprsal">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="TotalException" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allDAO_Apprsal"
expression="execution(* com.easepal..service.impl.*SerivceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice_Apprsal" pointcut-ref="allDAO_Apprsal" />
</aop:config>
<bean id="daoAppStore"
class="com.easepal.app.store.dao.impl.StoreDaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient_apprsal" />
</property>
</bean>
<jaxws:endpoint xmlns:tns="http://easepal.com/app/store/"
id="StoreService" implementor="#Impl_StoreService"
endpointName="tns:StorePort" serviceName="tns:StoreService"
address="/app/StoreService"/>
<bean id="Impl_StoreService" class="com.easepal.app.store.service.impl.StoreServiceImpl">
<property name="dao" ref="daoAppStore"></property>
</bean>
Service 接口类 :public String saveApp(@WebParam(name="sessionId") String sessionId,@WebParam(name="app") App app);Service 接口实现类public String saveApp(String sessionId,App app) throws TotalException {
try{
AppPojo appPojo=new AppPojo();
appPojo.setAppDesc(app.getAppDesc());
appPojo.setAppFee(app.getAppFee());
appPojo.setAppIcon("");
appPojo.setAppId(UUIDGenerator.getUUID());
appPojo.setAppName(app.getAppName());
appPojo.setAppNo(app.getAppNo());
appPojo.setAppType(app.getAppType());
VersionInfoPojo version=new VersionInfoPojo();
version.setPublishPath("");
version.setVersionId(UUIDGenerator.getUUID());
version.setAppId(appPojo.getAppId());
version.setPublishType("a");
dao.saveApp(appPojo);
dao.saveVersionInfo(version);
}catch(DataAccessException de){
System.out.println("service execute exception");
throw new TotalException("error");
}
return null;
}
TotalException 为自定义一个异常类 Dao 接口类 :public String saveApp(AppPojo apppojo);
public String saveVersionInfo(VersionInfoPojo version);
Dao 接口实现类 public String saveApp(AppPojo appPojo){
this.getSqlMapClientTemplate().insert("store.insertApp",appPojo);
//saveVersionInfo(appPojo.getVersionInfo());
return appPojo.getAppId();
}
public String saveVersionInfo(VersionInfoPojo version){
version.setVersionNo(StringUtil.dateToString("yyyyMMdd", new Date()));
version.setPublishTime(new Date());
this.getSqlMapClientTemplate().insert("store.insertVersion",version);
return null;
}
spring 配置文件 :<bean id="dataSource_apprsal"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbcapprsal.url}"></property>
<property name="username" value="${jdbcapprsal.username}"></property>
<property name="password" value="${jdbcapprsal.password}"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="1000"></property>
<property name="removeAbandoned" value="true"></property>
<property name="removeAbandonedTimeout" value="60"></property>
<property name="logAbandoned" value="true"></property>
</bean>
<!-- sql map client 配置 -->
<bean id="sqlMapClient_apprsal"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:/ibatis-sql-map-config-easepal.xml" />
<property name="dataSource" ref="dataSource_apprsal" />
</bean>
<!-- ************************************************************************-->
<!-- //////////////////////ibatis事务代理配置///////////////////////////////-->
<!-- ************************************************************************-->
<!-- 配置哪些类哪些方法使用事务 -->
<bean id="transactionManager_Apprsal"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_apprsal"></property>
</bean>
<tx:advice id="txAdvice_Apprsal" transaction-manager="transactionManager_Apprsal">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="TotalException" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allDAO_Apprsal"
expression="execution(* com.easepal..service.impl.*SerivceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice_Apprsal" pointcut-ref="allDAO_Apprsal" />
</aop:config>
<bean id="daoAppStore"
class="com.easepal.app.store.dao.impl.StoreDaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient_apprsal" />
</property>
</bean>
<jaxws:endpoint xmlns:tns="http://easepal.com/app/store/"
id="StoreService" implementor="#Impl_StoreService"
endpointName="tns:StorePort" serviceName="tns:StoreService"
address="/app/StoreService"/>
<bean id="Impl_StoreService" class="com.easepal.app.store.service.impl.StoreServiceImpl">
<property name="dao" ref="daoAppStore"></property>
</bean>
解决方案 »
- htmlpaser抓取博客正文内容
- BVT 用什么工具?
- hibernate执行insert 的sql,为什么要加事务才能录入数据?
- MyEclipse 5.5.1 GA 使用的快捷键
- 在spring的mvc中很多都要依赖于xml的配置是不是很占用资源
- struts框架下多工程问题的解决办法!!
- 能给小弟一个JSP+javaBean+servlet的例子吗?
- 提个比较低能的问题 会servlet 的 进来看看哦
- weblogic+jbuilder2005如何单步调试EJB,有文档,请高手翻译一下
- 请问之中用了什么技术?如何分析当中的每一句的作用?希望大神给我加注释,方便我理解该段hql代码,谢谢
- 求解惑:java方向选择
- 改变图片规格出错
expression="execution(* com.easepal..service.impl.*SerivceImpl.*(..))" />
<bean id="Impl_StoreService" class="com.easepal.app.store.service.impl.StoreServiceImpl">
你仔细对比下就知道了.......SerivceImpl........................