Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); if (transaction.isActive()) { // do something } transaction.commit(); session.close();
@Transactional public void someTransactionalMethod() { if (TransactionSynchronizationManager.getCurrentTransactionName() != null && TransactionSynchronizationManager.isActualTransactionActive()) { // do something } }In this example, we use the Spring TransactionSynchronizationManager to check if the transaction is active in a Spring-managed transactional method. The org.hibernate.Transaction class is part of the Hibernate ORM library.