spring+mybatis 手动开启和提交事务
spring配置文件
事务控制管理器transactionManager
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
示例java代码
// 保存信息
public MessageTpl saveMessage(Map<String, Object> mqInfo, Message message) throws Exception{
// 保存信息
//获取Spring容器的对象
ClassPathXmlApplicationContext contextLoader = new ClassPathXmlApplicationContext(
new String[] { "classpath*:spring/applicationContext.xml", "classpath*:spring/spring-mybatis.xml"});
//1.获取事务控制管理器
DataSourceTransactionManager transactionManager = contextLoader.getBean(
"transactionManager", DataSourceTransactionManager.class);
//2.获取事务定义
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//3.设置事务隔离级别,开启新事务
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
//4.获得事务状态
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
MessageTpl messageTpl = null;
try {
Date time = new Date();
String status = Constant.MESSAGE_STATUS_TO_BE_CONFIRMED;
String remark = "optId:"+mqInfo.get("userId")+"|预发布:待确认";
if (Constant.MQ_IS_CONFIRM_NO == (Integer) mqInfo.get("isConfirm")) {
status = Constant.MESSAGE_STATUS_TO_BE_SEND;
remark = "optId:"+mqInfo.get("userId")+"|待发送:消息不需要确认";
} messageTpl = new MessageTpl((Long) mqInfo.get("mqId"), message.getContent(), 0,
(Long) mqInfo.get("userId"), status, time, time, Constant.MSG_TABLE + message.getQueueName(),
MD5Util.MD5_32(message.getContent()));
messageDao.saveMessage(messageTpl);
// TODO添加缓存
// 保存日志
MessageLogTpl log = new MessageLogTpl(null, "预发布", status, remark, time,
Constant.MSG_LOG_TABLE + message.getQueueName());
this.messageDao.saveMessageLog(log); } catch (Exception e) {
transactionManager.rollback(transactionStatus);
messageTpl = null;
logger.error("saveMessage_Exception ", e);
}finally {
transactionManager.commit(transactionStatus);
}
return messageTpl;
}
spring+mybatis 手动开启和提交事务的更多相关文章
- SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别
SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...
- 记录一次bug解决过程:resultType和手动开启事务
一.总结 二.BUG描述:MyBatis中resultType使用 MyBatis中的resultType类似于入参:parameterType.先看IDCM项目中的实际使用案例代码,如下: // L ...
- Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...
- SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现
上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1. 外部架包依赖引入 外部依赖包引入 ...
- Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...
- Spring+Mybatis+SpringMVC后台与前台分页展示实例
摘要:本文实现了一个后台由spring+Mybatis+SpringMVC组成,分页采用PageHelper,前台展示使用bootstrap-paginator来显示效果的分页实例.整个项目由mave ...
- SpringMVC Spring Mybatis整合篇
1.创建WEB项目 创建项目:(ssmbuild)步骤略........ 给项目添加lib文件夹,用于存放jar包: 在WEB-INF目录下创建lib文件夹: 创建完成:运行项目时需要把jar导入到l ...
- Idea SpringMVC+Spring+MyBatis+Maven调整【转】
Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...
随机推荐
- 2.goldengate日常维护命令(转载)
goldengate日常维护命令 发表于 2013 年 7 月 4 日 由 Asysdba 1.查看进程状态 GGSCI (PONY) 2> info all 2.查看进程详细状态,有助于排错 ...
- maven-replacer-plugin
今天多认识了下这个maven插件. 基本用法: <plugin> <groupId>com.google.code.maven-replacer-plugin</grou ...
- 11.ok6410之led驱动程序编写
led驱动程序编写 本文主要包含三部分,led驱动程序led.c编写,编译驱动程序的makefile的编写,以及使用驱动程序的应用程序led_app的编写 一.led.c编写 #include < ...
- Spring整合web开发
正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet { public void doGet(HttpServlet ...
- Android开发环境(IDE)
一:Eclipse 1.装JDK: 2.装Eclipse://应与JDK同为32/64位. 3.装ADT: 4.装android sdk: 推荐下载ADT bundle包(包含Eclipse,Andr ...
- 简单的网络引导安装CentOS7
实验室有几台电脑,里边装有windows,因为实验需求要给其装入CentOS7.但是这几个电脑无法用U盘引导系统的安装,虽然带有光驱,但是又不想麻烦去买碟片,所以便想到用网络引导系统的安装. 1. 软 ...
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- Codeforces Round B. Buttons
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...
- VB将JSON映射到表格实现解析
现在抓取网页数据的时候,经常会遇到JSON的数据,相对于繁杂无标签名的HTML源,用JSON传回的数据比较直观好看点.但是从其中提炼数据也让人觉得很烦躁,基本上就是不断的查找,截取,或者组装成JS代码 ...
- istringstream的操作
今天在stackoverflow上看到这么个问题,写完之后看了看别人的提交的答案,感觉自己的答案虽然能得出正确结果但是有点啰嗦,对于c++还是没有熟练,没有想起有istringstream,而且提问的 ...