Spring多个事务回滚

时间:2016-05-22 15:14:12

标签: mysql spring-mvc transactional

我有一个要求,我必须通过3个DAO类插入3个单独的表。

ClassADAO
ClassBDAO
ClassCDAO

我希望所有三个类都有一个事务,这样如果一个插入失败,我想回滚通过以前的类插入的完整事务。

我有我配置的xml,如下所示

  <tx:annotation-driven transaction-manager="transactionManager" />
  <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
  </bean>
  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            id="dataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
    <property name="username" value="******"></property>
    <property name="password" value="********"></property>
  </bean>

在控制器中注释如下

@Transactional(rollbackFor = { Exception.class }, propagation = Propagation.REQUIRED)

但是如果 ClassCDAO 失败,我可以从 ClassA ClassB

中插入数据库

控制器:

@Controller
@Transactional(rollbackFor = { Exception.class }, propagation = Propagation.REQUIRED)
public class MyController{

@Autowired
private MyBusinessLayer mybusinessLayer;

@RequestMapping(value = "/register.htm", method = RequestMethod.POST)
    public String saveRegistration(
            final @ModelAttribute("registration") @Valid Registration registration,
            final BindingResult result, final Model model) {
            if (result.hasErrors()) {
                return "myPage";
            } else {
                mybusinessLayer.saveRegistration(registration);
            }

        return "myPage";
    }

}

我的业务层:

 @Component
 public class MyBusinessLayer{

    @Autowired
    private ClassA classA;

    @Autowired
    private ClassB classB;

    @Autowired
    private ClassC classC;

    public void saveRegistration(Registration registration) {
        Company company = RegistrationHelper.buildCompany(registration);
        classA.saveCompany(company);
        Contact contact = RegistrationHelper.buildContact(registration, company.getCompanyId());
        classB.saveContact(contact);
        User user = RegistrationHelper.buildUser(registration, contact.getCompanyID(),
                contact.getContactID());
        classC.saveUser(user);
    }    

  }

ClassADAO:

@Component
public class CompanyDAOImpl implements CompanyDAO {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }

    @Override
    public void saveCompany(Company company) {
        String insertCompanySQL = //insert statement;
        //code here
        SqlParameterSource paramSource = new MapSqlParameterSource(bind);
        namedParameterJdbcTemplate.update(insertCompanySQL, paramSource);
    }
}

ClassBDAO和ClassCDAO也如上所示。

1 个答案:

答案 0 :(得分:0)

@Transactional注释移至MyBusinessLayer课程。除非您将DataSource配置为autocommit,否则您的代码似乎是正确的。