事务方法里如何单独添加日志
来源:互联网 发布:2017淘宝男装店铺排行 编辑:程序博客网 时间:2024/06/01 09:07
ps:麦库记事上传不了代码,只能在这写了
遇到的问题:项目中service层一个方法里有很多个添加修改方法,所以把最外层的方法放在事务里,就可以实现全部的回滚操作了。但是问题出现了,如果我想单独生成一个不在这个事务里的日志记录该怎么操作?
解决方法三种都亲测有效:
1,在service层的上一层,无论是impl层还是action在日志表里添加记录,这种代码看起来不是那么整洁,但是最简单粗暴
2,spring环绕通知实现日志保存
切面层配置
3,设置不同的事务级别
springmvc配置文件
遇到的问题:项目中service层一个方法里有很多个添加修改方法,所以把最外层的方法放在事务里,就可以实现全部的回滚操作了。但是问题出现了,如果我想单独生成一个不在这个事务里的日志记录该怎么操作?
解决方法三种都亲测有效:
1,在service层的上一层,无论是impl层还是action在日志表里添加记录,这种代码看起来不是那么整洁,但是最简单粗暴
2,spring环绕通知实现日志保存
applicationContext.xml配置(我只定位到具体的文件夹,缺点就是哪天别的同事不知道存在切面的情况下在这个类写代码,那就坑死他了,哈哈,不过可以定位到具体的类和具体的方法) expression="execution (* com.hrd.appl.biz.intf.api.impl.appl.*.*(..))"。 其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。
<aop:aspectj-autoproxy /> <aop:config><aop:aspect id="applicationLogService" ref="applicationLogService"> 切面 <aop:pointcut id="applicationLogPointcut" expression="execution (* com.hrd.appl.biz.intf.api.impl.appl.*.*(..))"/>切入点,配置在impl包下所有的类在调用之前都会被拦截 <aop:around pointcut-ref="applicationLogPointcut" method="around"/> 环绕通知,调用前后</aop:aspect> </aop:config>
切面层配置
@Service("applicationLogService")public class ApplicationLogService {private static final Logger logger = LoggerFactory.getLogger(ApplicationLogService.class);@Resourceprivate UserAccountApplLogDao userAccountApplLogDao;public Object around(ProceedingJoinPoint pjp) throws Throwable {JiSiApplAppt js = new JiSiApplAppt();for (int i = 0; i < pjp.getArgs().length; i++) {List<Object> li = Arrays.asList(pjp.getArgs()[i]);for (int j = 0; j < li.size(); j++) {js = (JiSiApplAppt) li.get(j);}}UserAccountApplLog userAccountApplLog = new UserAccountApplLog();Object retVal = pjp.proceed();ReturnParam retParam = JsonUtils.toObject(retVal.toString(), ReturnParam.class);userAccountApplLog.setRemark(retParam.getRetInfo());if (ReturnParam.SUCCESSCODE.equals(retParam.getRetCode())) {userAccountApplLog.setStatus("succ");} else {userAccountApplLog.setStatus("err");}userAccountApplLog.setReturnObj("Request:" + js.toString() + ";Response:" + retParam.toString());// userAccountApplLogDao.insert(userAccountApplLog);return retVal;}}
3,设置不同的事务级别
springmvc配置文件
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 拦截器方式配置事物 --><!--DAO数据--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>//隔离级别<tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="merge*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="publish*" propagation="REQUIRED" /><tx:method name="invalidate*" propagation="REQUIRED" /><tx:method name="newtrans*" propagation="REQUIRES_NEW" /><tx:method name="executeEngine*" propagation="REQUIRED" /><tx:method name="batchCreate*" propagation="REQUIRED" /><tx:method name="forward*" propagation="REQUIRED" /><tx:method name="agree*" propagation="REQUIRED" /><tx:method name="refuse*" propagation="REQUIRED" /><tx:method name="complete*" propagation="REQUIRED" /><tx:method name="cancel*" propagation="REQUIRED" /><tx:method name="forward*" propagation="REQUIRED" /><tx:method name="file*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="deploy*" propagation="REQUIRED" /><tx:method name="find*" propagation="REQUIRED" /></tx:attributes></tx:advice>
public ReturnParam<Object> saveApplAppt(JiSiApplAppt param) {UserAccountApplLog userAccountApplLog = new UserAccountApplLog();ReturnParam<Object> revParam = new ReturnParam<Object>();LcAppl appl = param.getAppl();userAccountApplLog.setReturnObj(param.toString());userAccountApplLog.setCooprCode(param.getAppl().getCooprCode());userAccountApplLog.setTransSeq(param.getTransSeq());userAccountApplLog.setUserId(param.getAppl().getUserId());userAccountApplLog.setManageCde(param.getAppl().getManageCde());userAccountApplLog.setAddtime(String.valueOf(DateUtils.getNowTime()));int id = userAccountApplDao.newtransinsert(userAccountApplLog);revParam = this.validate(appl, revParam);if (!ReturnParam.SUCCESSCODE.equals(revParam.getRetCode())) {userAccountApplLog.setRemark(revParam.getRetInfo());userAccountApplLog.setStatus("err");userAccountApplDao.newtransupdate(userAccountApplLog);return revParam;}return revParam;}
阅读全文
0 0
- 事务方法里如何单独添加日志
- nginx单独添加模块|日志记录post数据
- 如何为应用程序建立单独的系统日志
- 在这防止页面缓存的方法里添加日志标题
- 单独事务设置
- 用代码在日志里添加背景音乐
- xcode4 里如何添加Framework?
- 如何查看SQL Server的事务日志
- 如何读懂SQL Server的事务日志
- SQLServer事务同步下如何收缩日志
- SQL Server事务日志的处理方法
- 删除SQL数据库中事务日志方法
- 删除SQL数据库中事务日志方法
- 删除SQL数据库中事务日志方法
- 删除SQL数据库中事务日志方法
- 删除SQL数据库中事务日志方法
- 删除SQL数据库中事务日志方法
- DB2不记录事务日志方法
- PAT 甲级 1132. Cut Integer (20)
- 初识大数据
- 使用boot2docker管理Docker
- SpringCloud之Ribbon+Rest调用负载均衡
- 对数组元素进行由大到小排列
- 事务方法里如何单独添加日志
- Python学习---13
- NOIP模拟(20171102)T3 回文子串
- Java对象流与序列化
- 数据结构
- C++ 11的继承构造函数
- 有了开发板和平台资源,AI语音技能开发没有想象那么难
- 78/90. Subsets (子集合)
- “大”事务引起的锁等待分析案例