0

I have this code and I have a question :

@Service
class SimpleServiceImpl implements SimpleService{

@PersistenceContext
private EntityManager em;

@Transactional
public void doSmth(){
   // here I want to have a new session
   Session session = em.unwrap(Session.class).getSessionFactory().openSession();

   // do smth in new session
   session.close();
}

What about a transaction ??? Actually as I understand if I open session I have to manage this session explicitly and I have to close it. But what's going on with transaction? Should I begin a new transaction and manage it by myself or my action s continue in the same transaction which method doSmth began?

idmitriev
  • 4,619
  • 4
  • 28
  • 44

2 Answers2

0

You have annotated with @Transactional that's why Spring will handle transaction using AOP concept.

Nimesh
  • 794
  • 3
  • 8
  • 18
0

If you use a Transactional annotation your method will be done within one transaction.

For more information you could also check this: spring transactional what happens in background.

Community
  • 1
  • 1
Rufi
  • 2,529
  • 1
  • 20
  • 41