0

I know, that there is a way to descend to a low level - get connection and perform two transaction's by hand in a single hibernate session.

But the question is - how to invoke second nested transaction in the same Session via @Transactional annotations (not using "low level hacks" or handwrited custom transaction management)?

Some possible code:

@Service
public class DoubleTransaction {

@Autowired
private SessionFactory sf;

@Autowired
private NestedTeHandler nestedHandler;

@Transactional
void invokeTransaction() {
    Session cs = sf.getCurrentSession();
    Session nestedCs = nestedHandler.invokeNested(sf);
    System.out.println(cs == nestedCs);
}}

@Service
public class NestedTeHandler {

@Transactional
Session invokeNested(SessionFactory sf) {
    return sf.getCurrentSession();
}}
uptoyou
  • 1,427
  • 19
  • 24

1 Answers1

0

You might be able to do that with

@org.springframework.transaction.annotation.Transactional(propagation = Propagation.NESTED)

on NestedTeHandler.invokeNested. See documenation at https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html.

See also this question: Differences between requires_new and nested propagation in Spring transactions.

Christophe L
  • 13,725
  • 6
  • 33
  • 33