自己第一次配置事务,搞了一天一直不回滚。代码如下,麻烦大牛给看看  service是通过IOC注入,有调度框架quartz去调用
xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="40" />
<property name="idleConnectionTestPeriod" value="300" />
</bean> <!-- 事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>

<bean id="dao" class="dao.TestDao">
<property name="template">
<ref local="jdbcTemplate" />
</property>
</bean>

<bean id="service" class="service.TestService">
<property name="td">
<ref local="dao" />
</property>
</bean> <!-- quartz总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="task1" />
<!-- <ref bean="task2" /> -->
</list>
</property>
</bean> <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resource/develop.properties" />
</bean>
<import resource="classpath:quartz/*.xml" />
</beans>
service代码如下,我把没用的代码都删掉了,只留下与问题有关的
package service;@Transactional
public class TestService { TestDao td; public TestDao getTd() {
return td;
} public void setTd(TestDao td) {
this.td = td;
} @SuppressWarnings("unchecked")
public void insert(File temp, String xmlPath) { try {
for (int i = 0; i < 2; i++) {
this.getTd().insertBatch(r.getSql(), r.getList());
// throw new RuntimeException("111111111");
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Dao代码如下public class TestDao {
JdbcTemplate template; public JdbcTemplate getTemplate() {
return template;
} public void setTemplate(JdbcTemplate template) {
this.template = template;
} public void insert(String sql, String[] params) {
this.template.update(sql, params);
} public void insertBatch(String sql, final List<String[]> list) throws SQLException {
this.getTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i)
throws SQLException {
String[] params = list.get(i);
for (int j = 0; j < params.length; j++) {
ps.setString(j + 1, params[j]);
}
} public int getBatchSize() {
return list.size();
}
});
}
}

解决方案 »

  1.   

    默认下运行时异常才回滚的,抛出SQLException不回滚。
      

  2.   

    请参考我的基于spring,security,struts的开源项目
    http://blog.csdn.net/shadowsick
      

  3.   


    是啊,我故意抛了一个runtime异常
      

  4.   

    你没用AOP去拦截方法,怎么实现事物啊。<!-- 事务管理模块 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>
    <aop:config>
    <aop:pointcut id="defaultServicePointcut"
    expression="execution(* dome.service.impl.*ServiceImpl.*(..))" />
    <aop:advisor pointcut-ref="defaultServicePointcut"
    advice-ref="defaultServiceAdvice" />
    </aop:config>
    <tx:advice id="defaultServiceAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
    </tx:advice>
      

  5.   

    就像楼上说的,你都没失误切面,没有切到指定的类或者方法,怎么控制事务呢?
    配置了事务管理器,然后,还需要告诉spring这个事务管理器用在那些方法,或者类上。
      

  6.   


    我是用的注解的方式,在Service上都加了注解了。
      

  7.   

    找到原因了,事务配置的没问题,原因是我在service的方法里直接try catch了,所以没有触发回滚,我把异常抛到service调用出那里,OK,可以回滚!困扰了好几天的问题终于解决了