<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
    default-lazy-init="true" default-autowire="byName">
<!-- Spring 管理数据库连接池 用于在tomcat下做测试使用-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass"
value="${jdbc.connection.driver_class}" />
<property name="jdbcUrl" value="${jdbc.connection.url}" />
<property name="minPoolSize"
value="${jdbc.c3p0.minPoolSize}" />
<property name="maxPoolSize"
value="${jdbc.c3p0.maxPoolSize}" />
<property name="initialPoolSize"
value="${jdbc.c3p0.initialPoolSize}" />
<property name="maxIdleTime"
value="${jdbc.c3p0.maxIdleTime}" />
<property name="acquireIncrement"
value="${jdbc.c3p0.acquireIncrement}" />
<property name="maxStatements"
value="${jdbc.c3p0.maxStatements}" />
<property name="idleConnectionTestPeriod"
value="${jdbc.c3p0.idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts"
value="${jdbc.c3p0.acquireRetryAttempts}" />
<property name="acquireRetryDelay" 
value="${jdbc.c3p0.acquireRetryDelay}" />
<property name="breakAfterAcquireFailure"
value="${jdbc.c3p0.breakAfterAcquireFailure}" />
<property name="testConnectionOnCheckout"
value="${jdbc.c3p0.testConnectionOnCheckout}" />
<property name="properties" ref="dataSourceProperties"/>
    </bean>
<bean id="dataSourceProperties" class="com.servicehall.service.encrype.PropertiesEncryptFactoryBean">   
        <property name="properties">   
            <props>   
                <prop key="user"><![CDATA[18006535799af1e259add7539daac53122cb7bc4df058e71141500eddb25e2d5]]></prop>   
                <prop key="password"><![CDATA[41da7cdd89702ccbefb1b20af46321e8b3289a8df61bd5e8375c76c97404de20]]></prop>      
            </props>   
        </property>   
    </bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resources/ibatis.properties</value>
</list>
</property>
</bean>

   <!-- Spring iBatis相关 -->
   <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
  
   <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" abstract="false" scope="singleton" lazy-init="default" autowire="default" dependency-check="default">
   <description>Spring iBatis结合</description>
     <property name="configLocation" value="classpath:resources/sqlMap-config.xml"/>
     <!-- 注意下面的通配符配置方式 -->
<property name="mappingLocations" value="classpath:com/**/sqlmap/*.xml"/>
     <property name="dataSource" ref="dataSource"/>
     <property name="lobHandler" ref="lobHandler"/>
   </bean>
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" scope="singleton" >
   <description>Spring 事务管理</description>
     <property name="dataSource" ref="dataSource"/>
   </bean>
  
   <bean id="baseSqlMapDAO" class="com.servicehall.base.ibatis.BaseSqlMapDAO" scope="singleton" >
<description>数据库基础操作工具包</description>
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="transactionManager" ref="transactionManager"/>
</bean> <!-- 系统事务处理AOP配置 -->
<aop:config proxy-target-class="false">
<!-- 需事务控制切入点 -->
<aop:pointcut id="comm-txPointcut" expression="execution(* com..service.*(..))" />
<aop:advisor advice-ref="comm-txAdvice" pointcut-ref="comm-txPointcut"/>
</aop:config>
<tx:advice id="comm-txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
</tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
   <context:annotation-config/>
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com"  />
<!-- ecache 缓存管理 -->
<bean id="cacheManager" class="com.servicehall.service.cache.CacheManager" factory-method="getInstance">
</bean>
<bean id="cacheService" class="com.servicehall.service.cache.impl.EhCacheService">
</bean>

<!-- 查询业务数据 使用 ref="sqlMapClient" -->
<bean name="commonDAO"
class="com.servicehall.base.dao.CommonDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>   
</beans>
上面是 spring 配置文件 下面是Service层 事务方法 在中间手动抛一个异常 前面的方法照样提交数据库。数据库是使用ibatIS@Service
@Transactional
public class DeviceInfoServiceImpl implements DeviceInfoService{
@Autowired
private DeviceInfoDAO deviceInfoDAO;
public void setDeviceInfoDAO(DeviceInfoDAO deviceInfoDAO) {
this.deviceInfoDAO = deviceInfoDAO;
} public DeviceInfoDAO getDeviceInfoDAO() {
return deviceInfoDAO;
}
@Override
public void addDevInfo(HttpServletRequest request) { try {  Map<String,Object> map =new HashMap<String,Object>();
 String devId=request.getParameter("country")+request.getParameter("area")+request.getParameter("sbId");
 map.put("devId", devId);
 map.put("area", request.getParameter("area"));
//保存网元
 map= svaeWy(request, map);
 String rec_id=deviceInfoDAO.insertWy(map); String []wyljId= request.getParameterValues("wyljId");
    String []bkId= request.getParameterValues("bkId");
    String []type =request.getParameterValues("type");
    //保存逻辑网元
    addWy(devId, rec_id, wyljId, bkId);
    //保存逻辑网元类型
    addLjwg(devId, wyljId, bkId, type);
        //保存板卡
    addBk(devId, wyljId, bkId, type);  
} catch (Exception e) {
e.printStackTrace();
} }
public void addWy(String devId, String rec_id, String[] wyljId,
String[] bkId) {
//存放逻辑网元
List<String> list = new ArrayList<String>();  
//去重复
for (int i=0;i<bkId.length;i++){
i=6;
 if(!list.contains(devId+wyljId[i])) {  
         list.add(devId+wyljId[i]);  
     }  
}
 Map<String,String> map2 =new HashMap<String,String>();
for(int i=0;i<list.size();i++){
map2.put("devId", list.get(i));
map2.put("type", "5");
map2.put("parentId",rec_id);
map2.put("deviceName", "逻辑网元"+i);
deviceInfoDAO.insertLjwy(map2);
}
} public void addLjwg(String devId, String[] wyljId, String[] bkId,
String[] type) {
List<String> list;
Map<String, String> map2;
//存放逻辑网元类型
//去重复
list = new ArrayList<String>();  ;
 map2=new HashMap<String,String>();
for (int i=0;i<bkId.length;i++){
 if(!list.contains(devId+wyljId[i]+type[i])) {  
 list.add(devId+wyljId[i]+type[i]);  
     }  
}
for(int i=0;i<list.size();i++){
map2.put("devId", list.get(i));
    String parentId=deviceInfoDAO.findIdByDevId(list.get(i).substring(0,10));
map2.put("type", list.get(i).substring(9,10));
map2.put("parentId",parentId);
map2.put("deviceName", "网元"+i);
deviceInfoDAO.insertLjwy(map2);
}
}}
Spring事务 

解决方案 »

  1.   

    楼主的问题我不知道答案,我就想问下
    http://www.springframework.org/schema/tx/spring-tx.xsd"
    这个XSD后面没有2.5会影响提醒和使用吗?
      

  2.   

    事务重复配置了吧,把@Transactional注解给去了试试看。
      

  3.   

    comm-txPointcut为什么要加中划线??不符合命名规则呀!!仅仅只是事务无效吗??
    你看你操作的表 是不是被锁住了。。我以前常犯的错。。
      

  4.   


    <!-- 配置传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="modify*" propagation="REQUIRED" />
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="remove*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="find*" propagation="NOT_SUPPORTED"
    read-only="true" />
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice> <!-- 配置事务过滤 -->
    <aop:config>
    <aop:pointcut id="methodPointcut"
    expression="execution(* com.shadow.mvc.business.*Service.*(..))" />
    <aop:advisor pointcut-ref="methodPointcut"
    advice-ref="txAdvice" />
    </aop:config><bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dynamicDataSource" />
    </bean>
    就是这样简单而已,这才是写的够精炼的
      

  5.   

    而且你配置又注解又xml...为什么要配置两个...难道两个你想要保险点?还是怎么的?不明所以
      

  6.   

    你数据持久化在spring中是交给ibatis处理的,那么你应该砸ibatis配置文件中配置事务
      

  7.   

    Service需要抛出异常 ,不是捕获异常 。还回滚不了看看是不是切点没切入