我正在使用Spring/Spring-data-JPA,并发现自己需要在单元测试中手动强制提交.我的用例是我正在进行多线程测试,其中我必须使用在生成线程之前持久化的数据.
不幸的是,鉴于测试在一个@Transactional事务中运行,即使是一个flush也没有让它可以被生成的线程访问.
@Transactional
public void testAddAttachment() throws Exception{
final Contract c1 = contractDOD.getNewTransientContract(15);
contractRepository.save(c1);
// Need to commit the saveContract here, but don't know how!
em.getTransaction().commit();
List<Thread> threads = new ArrayList<>();
for( int i = 0; i < 5; i++){
final int threadNumber = i;
Thread t = new Thread( new Runnable() {
@Override
@Transactional
public void run() {
try {
// do stuff here with c1
// sleep to ensure that the thread is not …Run Code Online (Sandbox Code Playgroud)