1.springmvc配置事务,先在application.xm中配置事务
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- <tx:method name="get*" read-only="true" rollback-for="java.lang.Exception" /> -->
<tx:method name="query*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<!-- <tx:method name="*" rollback-for="java.lang.Exception"/> --><!-- 默认回滚机制是RuntimeException -->
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="service" expression="execution(* com.oracle.sx.businessData.service..*ServiceImpl.*(..))" /><!-- 声明所有包含Service的类的所有方法使用事务 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
</aop:config>
2.在对应的*ServiceImpl(Service实现类)对应的方法添加异常机制,如下:
@Override
public String addDept(JSONObject obj) throws Exception {
DepartmentInfo departmentInfo = new DepartmentInfo();
String retCode ="";
String retMsg = "";
String objStr = "";
// Map<String, Object> params = new HashMap<String, Object>();
JSONObject jsonObject = new JSONObject();
try{
业务块
}catch(Exception e){
retCode = GET_MESSAGE_CODE.UNKNOWN_ERROR;
retMsg = "未知错误";
log.info("接口异常信息:" + e);
throw new Exception();
}
jsonObject.put("response",retCode);
jsonObject.put("message", retMsg);
//转换JSON
JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject);
objStr = jsonArray.toJSONString();
return objStr;
}