結合前面做的小項目,如果我把test01 test02下面的 service 都加了 事務的注解
這樣啟動時候會報錯!
事務管理器里面不能有兩個事務!!!!
這時候需要用 @Transactional(transactionManager="test1TransactionManager") 指定事務管理器
請看當前目錄結構:
DataSource1Config代碼里面有聲明事務管理器的名字的!
然后我們這個項目多個數據源、多個事務
同理可得:
package com.toov5.test02.service02; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.toov5.test02.mappertest02.UserMapperTest02; import lombok.extern.slf4j.Slf4j; @Service @Slf4j public class UserService02 { @Autowired private UserMapperTest02 userMapperTest02; @Transactional(transactionManager="test2TransactionManager") public int insertUser(String name, Integer age){ int result = userMapperTest02.insert(name, age); log.info("####################",result); return result; } }
當失敗時候,會進行回滾從而不會寫入到數據庫中