我有一个具有特定方法的服务实现,如下所示:
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);
}
}
Record 类是对象图的根对象。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
}
事务是否会应用于 processRecord 调用中所有涉及的插入\更新?换句话说,如果插入或更新失败,之前从 ExampleServiceImpl.processRecord 调用的所有插入和更新都会回滚吗?