---
title: "spring mvc 事务"
author: "JavaBrain"
date: "2015-03-27T15:52:28Z"
tags: []
categories: ["企业架构"]
slug: "spring-mvc"
draft: false
lastmod: "2015-03-27T15:52:28Z"
id: 2196188
url: "https://cai-bird.iteye.com/blog/2196188"
---

**applicationContext.xml:**

<context:component-scan base-package="com.ssh,com" >
	     
    </context:component-scan>


springMvc-servlet.xml
<!-- 自动扫描的注解包名，用，隔开 -->
	<context:component-scan base-package="com.ssh,com" >
	     <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Service" />
    </context:component-scan>


------------------------------------------
服务类：
@Transactional(rollbackFor=Exception.class) ，若没配，则无事务。在接口配置，也无事务。服务类非接口方法也无事务，如private

//	@Transactional(rollbackFor=Exception.class) 
	public void testRollback() throws Exception{
//插入数据1
		TBatTaskLog bean = new TBatTaskLog();
		bean.setBatchNo("11111");
		bean.setBranchNo("11111");
		bean.setCreateTime(new Date());
		bean.setCreator("a");
		bean.setInstanceId("111111111");
		bean.setMessage("abc");
		bean.setSeqNo(5);
		hbmBaseDao.save(bean);
		LogUtil.println("a#####################1");
		//插入数据2
		TBatInstPre beanpre = new TBatInstPre();
		beanpre.setInstanceId("111111111");
		beanpre.setPreInstanceId("aaaaaaaaaaa");
		this.hbmBaseDao.save(beanpre);
		LogUtil.println("a#####################2");
		
		//抛异常
		throw new Exception("abccc");
｝