I'm working on a project where I want to use a very specific transaction propagation strategy. The database has two sets of tables, active and archive. Each set of tables is implemented with its own Entities and Interfaces that extend CrudRepository<T, ID>
. The goal is to have a set of entities inserted into the active tables and to have all the data in the active tables inserted into the archive tables in a single transaction. The entities in the tables are not the same, and will have different table structures.
Given two separate repositories similar to the form
public interface FooRepository extends CrudRepository<Foo, Integer>
public interface FooArchiveRepository extends CrudRepository<FooArchive, Integer>
and an implementation similar to
@Autowired FooRepository fooRepo;
@Autowired FooArchiveRepository fooArchiveRepo;
@Autowired BarService barService;
List<Foo> newData = barService.doThing();
fooRepo.saveAll(newData);
// fooData is a list of FooArchive from earlier
fooArchiveRepo.saveAll(fooData);
The goal is to have fooRepo.saveAll(newData)
and fooArchiveRepo.saveAll(fooData)
be guaranteed to execute in a single database transaction. Spring's default transaction propagation of Required
has different Transactional
methods execute in the same physical transaction - does that apply to all Transactional
methods within the Application Context, or only on a per-entity basis?