Skip to content

Persistence Context shares between non-transactional and REQUIRES_NEW transactions unexpectedly #3807

@sedmehdi

Description

@sedmehdi

In the following example, when the POST /foo/test1 endpoint is called, the entity will be saved to the database. However, calling POST /foo/test2 will not trigger any updates.

`
@RestController
@RequestMapping("/foo")
public class FooController {

	@Autowired
	BarService barService;

	@PersistenceContext
	EntityManager entityManager;

	@PostMapping("/test1")
	public void post1() {
		System.out.println("\n".repeat(10));
		FileEntity fileEntity = entityManager.find(FileEntity.class, 2L);
		fileEntity.setDescription(fileEntity.getDescription() + "-1");
		barService.bar();
	}

	@Transactional(Transactional.TxType.SUPPORTS)
	@PostMapping("/test2")
	public void post2() {
		System.out.println("\n".repeat(10));
		FileEntity fileEntity = entityManager.find(FileEntity.class, 2L);
		fileEntity.setDescription(fileEntity.getDescription() + "-1");
		barService.bar();
	}
}


@Service
public class BarService {
	@Transactional(Transactional.TxType.REQUIRES_NEW)
	public void bar() {
		System.out.println("log");
	}
}

`
I expect that no updates occur in either call

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions