2016-05-22 15 views
2

私は3つのDAOクラスを通して3つの別々のテーブルに挿入する必要があります。複数のトランザクションをロールバックする

ClassADAO 
ClassBDAO 
ClassCDAO 

私は1つの挿入が失敗した場合、私は、以前のクラスを介して挿入され、完全なトランザクションをロールバックするように、3つのすべてのクラスのための単一のトランザクションを持っていると思います。

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

を次のように注釈付きのコントローラで

<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> 

以下のように私は私の構成されたXMLを持っている。しかし、まだClassCDAOが失敗した場合、私はにClassAから私のデシベルのC挿入をすることができますおよびクラスB

コントローラ:

@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もあります。

+0

すべての関連コードを表示してください。この場合、DAOメソッドの呼び出し。 – Nikem

+0

@Nikem:私自身のコードを更新してください – Pathfinder

答えて

0

@Transactional注釈をMyBusinessLayerクラスに移動します。 autocommitとしてDataSourceを設定していない限り、コードは正しいようです。

+0

私は@ Transactionalをビジネス層に移しましたが、それでもPreの挿入は起こっていますし、config xmlデータソースでfalseまたはtruに設定された自動コミットもありません – Pathfinder

関連する問題