You can following SessionFactoryUtils to determine whether a transaction is taking place in Session factory and then decide to restart the session factory or not: You will need to import--> org.springframework.orm.hibernate.SessionFactoryUtils in you file, and use the following API.
static boolean hasTransactionalSession(SessionFactory sessionFactory);
Above API returns whether there is a transactional Hibernate Session for the current thread, that is, a Session bound to the current thread by Spring's transaction facilities.There is also another API just in case if you need to check if a session is transactional in session factory currently:
static boolean isSessionTransactional(Session session,SessionFactory sessionFactory);
Above API returns whether the given particular Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.