0 Replies Latest reply on Jul 21, 2013 11:36 PM by jackramzi

    nested transactions not supported

    jackramzi

      Although after each commit I close the session but sometimes my code runs into following error. Once I do the same operation for few times it surpass the error and works.

       

       

          Messages:

          nested transactions not supported

          File:           org/hibernate/engine/transaction/spi/AbstractTransactionImpl.java

          Line number:           152

       

       

       

       

      **My Code**

       

       

          session = HibernateUtil.getSession();

          session.getTransaction().begin();

                 ...   to do ....

          session.getTransaction().commit();

          session.close();

       

       

      **HibernateUtil**

       

       

          import org.hibernate.HibernateException;

          import org.hibernate.Session;

          import org.hibernate.SessionFactory;

          import org.hibernate.cfg.Configuration;

          import org.hibernate.service.ServiceRegistry;

          import org.hibernate.service.ServiceRegistryBuilder;

         

          public class HibernateUtil {

         

             private static ServiceRegistry serviceRegistry;

             private static final ThreadLocal<Session> threadLocal = new ThreadLocal();

             private static SessionFactory sessionFactory;

              private static SessionFactory configureSessionFactory() {

                  try {

       

       

                      Configuration configuration = new Configuration();

                      configuration.configure();

                      serviceRegistry = new ServiceRegistryBuilder()

                                           .applySettings(configuration.getProperties())

                                           .buildServiceRegistry();

                      sessionFactory = configuration.buildSessionFactory(serviceRegistry);

       

       

                      return sessionFactory;

                  } catch (HibernateException e) {

                      System.out.append("** Exception in SessionFactory **");

                      e.printStackTrace();

                  }

                 return sessionFactory;

            }

          

         

            static {

              try {

                sessionFactory = configureSessionFactory();

              } catch (Exception e) {

                System.err.println("%%%% Error Creating SessionFactory %%%%");

                e.printStackTrace();

              }

            }

         

            private HibernateUtil() {

            }

         

            public static SessionFactory getSessionFactory() {

              return sessionFactory;

            }

         

            public static Session getSession() throws HibernateException {

              Session session = threadLocal.get();

         

              if (session == null || !session.isOpen()) {

                if (sessionFactory == null) {

                  rebuildSessionFactory();

                }

                session = (sessionFactory != null) ? sessionFactory.openSession() : null;

                threadLocal.set(session);

              }

         

              return session;

            }

         

            public static void rebuildSessionFactory() {

              try {

                sessionFactory = configureSessionFactory();

              } catch (Exception e) {

                System.err.println("%%%% Error Creating SessionFactory %%%%");

                e.printStackTrace();

              }

            }

         

            public static void closeSession() throws HibernateException {

              Session session = (Session) threadLocal.get();

              threadLocal.set(null);

         

              if (session != null) {

                session.close();

              }

            }

          }