首先spring-hibernate.xml里配置事务:

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务增强处理Bean,指定事务管理器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<!-- 配置详细事务处理语义 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="load*" propagation="SUPPORTS" read-only="true" /> <!-- 其他采用默认事务方式 -->
<tx:method name="*" /> </tx:attributes>
</tx:advice>

然后,使用的时候要注意,要用注解的方式在Service层配置事务:

@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public String Save(String template_code, String block_code, String prop_code, String rule_code, String tpl,
String par_prop, String title) {
// TODO Auto-generated method stu
return teValidationRuleDao.Save(template_code, block_code, prop_code, rule_code, tpl, par_prop, title);
}

最后,要注意如果需要事务回滚,一定要在Dao层抛出RuntimeException这个运行时错误,否则不好使!

@Override
public String Save(String template_code, String block_code, String prop_code, String rule_code, String tpl,
String par_prop, String title) {
// TODO Auto-generated method stub
String json = "{status: 'OK', msg: '保存成功!'}";
Session session = this.getCurrentSession();
try {
TeValidationRule vModel = new TeValidationRule();
List<Map> tpls = session.createSQLQuery("select * from te_template_sql t where t.sql_id = '"+tpl+"'")
.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
String muban = tpls.get(0).get("SQL_TEMPLATE").toString();
String mainSql = "select b.table_name,p.data_field,p.prop_name from te_template a "
+ "left join te_template_block b on b.template_code = a.template_code "
+ "left join te_template_property p on p.block_code = b.block_code "
+ "where a.template_code = '"+template_code+"' and b.block_code = '"+block_code+"' and p.prop_code = '"+prop_code+"'";
List<Map> mainProp = session.createSQLQuery(mainSql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();
String table = mainProp.get(0).get("TABLE_NAME").toString();
String datafield = mainProp.get(0).get("DATA_FIELD").toString();
String pname = mainProp.get(0).get("PROP_NAME").toString(); muban = muban.replaceAll("tableName", table);
muban = muban.replaceAll("dataField", datafield);
muban = muban.replaceAll("parField", par_prop); String rid = "";
if(rule_code!=null && !rule_code.equals("")){
rid = rule_code;
vModel.setRuleCode(rule_code);
}else{
rid = UUID.randomUUID().toString();
}
vModel.setRuleName(title);
vModel.setRuleType(2);
vModel.setRuleContent(muban);
vModel.setErrorMsg(pname+"格式错误!");
vModel.setRuleCategoryCode("8e267df45a7a4f59b257f5c15cc09bbb");
vModel.setRuleStatus(1);
vModel.setCreateUser("admin");
vModel.setCreateTime(new Date());
vModel.setUpdateUser("admin");
vModel.setUpdateTime(new Date()); if(rule_code!=null && !rule_code.equals("")){
session.update(vModel);
}else{
session.save(vModel);
} String numSql = "select count(*) nums from te_template_validation_rule "
+ "where block_code = '"+block_code+"' and prop_code = '"+prop_code+"' and rule_code = '"+rule_code+"'";
List nums = session.createSQLQuery(numSql)
.addScalar("NUMS").list();
int has = Integer.parseInt(nums.get(0).toString());
TeTemplateValidationRule teTemplateValidationRule = new TeTemplateValidationRule();
String tbp = ""; teTemplateValidationRule.setBlockCode(block_code);
teTemplateValidationRule.setPropCode(prop_code);
teTemplateValidationRule.setRuleCode(rid);
teTemplateValidationRule.setRuleType(2);
teTemplateValidationRule.setRuleContent(muban);
teTemplateValidationRule.setErrorMsg(pname+"格式错误!");
teTemplateValidationRule.setCreateUser("admin");
teTemplateValidationRule.setCreateTime(new Date());
teTemplateValidationRule.setUpdateUser("admin");
teTemplateValidationRule.setUpdateTime(new Date()); if(has == 0){
tbp = UUID.randomUUID().toString();
teTemplateValidationRule.setTemplateRuleCode(tbp);
session.save(teTemplateValidationRule);
}else{
String hasTbpSql = "select template_rule_code from te_template_validation_rule where block_code = '"+block_code+"' and prop_code = '"+prop_code+"' and rule_code = '"+rule_code+"'";
List tbp_code = session.createSQLQuery(hasTbpSql).addScalar("TEMPLATE_RULE_CODE").list();
tbp = tbp_code.get(0).toString();
teTemplateValidationRule.setTemplateRuleCode(tbp);
session.update(teTemplateValidationRule);
} } catch (Exception e) {
// TODO: handle exception
json = "{status: 'ERROR', msg: '保存失败!'}";
throw new RuntimeException();
}
return json;
}

SpringMVC+hibernate4事务处理的更多相关文章

  1. springmvc+hibernate4事务管理配置

    1.事务的特性 事务的四种特性: 原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行. 一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态.最典型的就是转账,两个账户A.B总金 ...

  2. Spring4+SpringMVC+Hibernate4整合入门与实例

    配置web.xml <? xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...

  3. Spring4 SpringMVC Hibernate4 Freemaker 集成示例

    变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...

  4. springMVC+Hibernate4+Spring整合一(配置文件部分)

    本实例采用springMvc hibernate 与 spring 进行整合, 用springmvc 取代了原先ssh(struts,spring,hibernate)中的struts来扮演view层 ...

  5. Spring4 SpringMVC Hibernate4 Freemaker 整合样例

    更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...

  6. 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目

    首先新建一个Maven项目.百度一下会有非常多实例,这里就不介绍了,直接奔主题. 如题:这里使用的是Hibernate4和Spring3,使用的JPA和Spring注解,然后JDK版本号是1.7 以下 ...

  7. springMVC+Hibernate4+spring整合实例二(实例代码部分)

    UserController.java 代码: package com.edw.controller; import java.io.IOException; import java.io.Print ...

  8. Maven下Spring + SpringMvc + Hibernate4 配置实例

    1. 开发环境 IDEA 2. 在pom.xml中配置引用相关的包. <properties> <junit.version>4.10</junit.version> ...

  9. maven学习日记(三)-------开发环境搭建(springmvc+hibernate4)各种maven错误汇总

    1.maven编码 gbk 的不可映射字符 解决这个问题的思路: 在maven的编译插件中声明正确的字符集编码编码——编译使用的字符集编码与代码文件使用的字符集编码一致!! 安装系统之后,一般中文系统 ...

随机推荐

  1. firefox_flash_install_on_kali

    手动安装firefox的flash的步骤 1> 下载flash的tar.gz安装包 firefox http://get2.adobe.com/cn/flashplayer/otherversi ...

  2. root权限和sudo得到权限的区别

    参考: 知乎 命令前加sudo执行和用真正的root用户执行有什么区别?pansz的回答 root用户和sudo使用root权限的区别 变换用户身份为root的方法su 与 sudo root权限和s ...

  3. UVa 11404 回文子序列(LCS求最长回文串长度)

    https://vjudge.net/problem/UVA-11404 题意: 给定一个由小写字母组成的字符串,删除其中的0个或多个字符,使得剩下的字母(顺序不变)组成一个尽量长的回文串.如果有多解 ...

  4. c++ 判断list是否为空(empty)

    #include <list> #include <iostream> using namespace std; int main() { list<int> nu ...

  5. json.dump()和json.load()

    import json,time # save data to json file def store(data): with open('data.json', 'w') as fw: # 将字典转 ...

  6. Docker简单部署Ceph测试集群

    通过docker可以快速部署小规模Ceph集群的流程,可用于开发测试. 以下的安装流程是通过linux shell来执行的;假设你只有一台机器,装了linux(如Ubuntu)系统和docker环境, ...

  7. window.frames && iframe 跨页面通信

    1.定义 frames[]是窗口中所有命名的框架组成的数组.这个数组的每个元素都是一个Window对象,对应于窗口中的一个框架. 2.用法 假设iframe 是一个以存在的 iframe 的 ID 和 ...

  8. linux 查看日志最后几行

    tail -n 50 wx.log 示例:查看/var/log/boot.log,只显示最后一行.则执行 tail -n 1  /var/log/boot.log tail -n 1000:显示最后1 ...

  9. 雷林鹏分享:Ruby 文件的输入与输出

    Ruby 文件的输入与输出 Ruby 提供了一整套 I/O 相关的方法,在内核(Kernel)模块中实现.所有的 I/O 方法派生自 IO 类. 类 IO 提供了所有基础的方法,比如 read. wr ...

  10. uva-1636-概率

    https://vjudge.net/problem/UVA-1636 给出一个左轮手枪的弹夹串,第一枪是空的,问是继续打还是转一转再打下一枪还为空的概率大.继续打为空的概率就是 '00'的个数比上' ...