简体   繁体   English

了解交易服务-春季

[英]Understanding of Transactional services - spring

I have a service implementation with a particular method like so: 我有一个像这样的特定方法的服务实现:

public class ExampleServiceImpl implements ExampleService {
  @AutoWired
  @Resource
  private RecordRepository recordRepository;

  private void processRecord() {
     // some code here
  }
  @Transactional(readOnly=false)
  public void processRecord(Record a) {
     Record original = getOriginal(a);
     recordRepository.saveChanges(a,original);
  }
}

Where the Record class is the root object of an object graph. 其中Record类是对象图的根对象。 RecordRepository looks something like the following with sub repositories to save various children of the objects in the graph. RecordRepository看起来类似于以下内容,带有子存储库,用于在图形中保存对象的各种子项。

public class RecordRepository extends BaseRepository<Record> {
   @AutoWired
   @Resource
   private IDao databaseDao;

   @AutoWired
   @Resource
   private SubRecordRepository subRecordRepository;

   public void saveChanges(Record a, Record b) {
      //Perform some processing on a, b
      for(SubRecord subA : a.getSubRecords()) {
         subRecordRepository.saveChanges(subA);
      }
      databaseDao.updateRecord(a);
   }
}

public class DatabaseDao extends NamedParameterJdbcDaoSupport implements IDao {

   @Autowired
   public DatabaseDao(@Qualifier("org.somewhere.Datasource") DataSource ds) {
       super();
       this.setDataSource(ds);
   }

   public void updateRecord(Record inRecord) {
       String query = (String) sql.get("updateRecord");
       SqlParameterSource parms = new BeanPropertySqlParameterSource(inRecord);
       getNamedParameterJdbcTemplate().update(query, parms);
   }

   public void insertSubRecord(SubRecord inSubRecord) {
       String query = (String) sql.get("insertSubRecord");
       SqlParameterSource parms = new BeanPropertySqlParameterSource(inSubRecord);
       getNamedParameterJdbcTemplate().insert(query, parms);
   }

   // other update and insert methods
} 

Will the transaction be applied across all involved inserts\\updates from the processRecord call? 事务是否将应用于processRecord调用中的所有涉及的inserts \\ updates? In other words, if an insert or update fails, will all previously called inserts and updates from ExampleServiceImpl.processRecord get rolled back? 换句话说,如果插入或更新失败,是否会回滚ExampleServiceImpl.processRecord中所有以前调用的插入和更新?

Yes. 是。 The transactional aspect makes sure that a transaction is started before the annotated method is called, and that the transaction (if started by this method) is committed or rollbacked once the method returns. 事务方面确保在调用带注释的方法之前启动了事务,并确保一旦方法返回,则提交或回滚事务(如果由该方法启动)。

The transactional interceptor doesn't know (and doesn't care) about which other methods are called inside the annotated method. 事务拦截器不知道(也不在乎)带注释的方法中还调用了哪些其他方法。 Every read and write to the DataSource handled by the Spring transaction manager will be included in the same transaction. 由Spring事务管理器处理的对DataSource的每次读写都将包含在同一事务中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM