Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:30:53+00:00 2026-06-11T13:30:53+00:00

@Singleton public class EntityManagerFactoryUtil implements Serializable { private static final long serialVersionUID = -5769561190804055727L;

  • 0
@Singleton
public class EntityManagerFactoryUtil implements Serializable {

private static final long serialVersionUID = -5769561190804055727L;

/**
 * This attribute mClassname is for logging the Class Name
 */
private final static String mClassName = "EntityManagerFactoryUtil";

/**
 * This attribute mLog used for write to log.
 */
private static Logger mLog = Logger.getLogger(EntityManagerFactoryUtil.class.getName());

/**
 * This method is used to create an instance of Entity Manager Factory.
 * @throws Exception
 * @return entityManagerFactory
 * @author ashokkumar_ks@solartis.net
 * @since  06-Sep-2012
 */
public  EntityManagerFactory getEntityManagerFactory() throws Exception {
    final String methodName = "getEntityManagerFactory";
    mLog.debug("Entered Class Name ::" + mClassName + " Method Name ::" + methodName);
    EntityManagerFactory entityManagerFactory=null;
    try {
        entityManagerFactory =Persistence.createEntityManagerFactory("StudentManagementSystem");
    } catch (Exception creationError) {
        mLog.error("Entered ::" + mClassName + "Error ::" +creationError.getMessage());
        throw new ExceptionInInitializerError(creationError);
    }
    mLog.debug("Exited Class Name ::" + mClassName + "Method Name ::" + methodName + " Object Reference :: " +entityManagerFactory);
    return entityManagerFactory;
}

}


This is my Business Logi

@EJB 
private EntityManagerFactoryUtil mEntityManagerFactory;

public StudentDetail save(StudentDetail studentInformation) {
    final String methodName = "Save";
    mLog.debug("Entered  ClassName::" + mClassName + " MethodName :: " + methodName);
    EntityManager entityManager = null;
    EntityTransaction entityTransaction = null;
    try { 
        entityManager = mEntityManagerFactory.getEntityManagerFactory().createEntityManager();
        if (entityManager != null && entityManager.isOpen()) {
            entityTransaction = entityManager.getTransaction();
            if(entityTransaction!=null) {
                entityTransaction.begin();
                if(entityTransaction.isActive()) {

                    // save student information into relation model
                    entityManager.persist(studentInformation);  
                    entityTransaction.commit();
                }
            }
        }
    } catch (Exception saveException) {
        try {
            if (entityManager != null && entityManager.isOpen()) {
                if(entityTransaction!=null  && entityTransaction.isActive()) {
                    entityTransaction.rollback();
                }
            }
        }catch (Exception transactionException) {
            mLog.warn("Exception in ClassName :: " + mClassName+ " MethodName::" + methodName + " warn :: "
                    + transactionException.getMessage() + "Student ID :: "+studentInformation.getId());
        }
            mLog.error("Exception in ClassName :: " + mClassName+ " MethodName::" + methodName + " Error :: "
                + saveException.getMessage() + "Student ID :: "+ studentInformation.getId());
    } finally {
        try {
            if (entityManager != null && entityManager.isOpen()) {
                entityManager.close();
            }
        } catch (Exception nullException) {
            mLog.warn("Exception in ClassName :: " + mClassName+ " MethodName::" + methodName + " warn :: "
                    + nullException.getMessage() + " Student ID :: "+ studentInformation.getId());
        }
        mLog.debug("Exited  ClassName::" + mClassName + " MethodName :: "+ methodName + "Student ID :: "+ studentInformation.getId());
    }
    return studentInformation;
}

When ever I calling Save method… every time new object created and access business logic.. ?? But Singleton Mean … One time Instance creation????

10:31:49,413 INFO [stdout] (http–0.0.0.0-8080-2) 22754 [http–0.0.0.0-8080-2] DEBUG com.dms.util.EntityManagerFactoryUtil – Exited Class Name ::EntityManagerFactoryUtilMethod Name ::getEntityManagerFactory Object Reference :: org.hibernate.ejb.EntityManagerFactoryImpl@130df8

10:31:56,232 INFO [stdout] (http–0.0.0.0-8080-3) 29573 [http–0.0.0.0-8080-3] DEBUG com.dms.util.EntityManagerFactoryUtil – Exited Class Name ::EntityManagerFactoryUtilMethod Name ::getEntityManagerFactory Object Reference :: org.hibernate.ejb.EntityManagerFactoryImpl@5170a8

Every time different object created.. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:30:55+00:00Added an answer on June 11, 2026 at 1:30 pm

    Finally I got solution.. Please let me know, anything wrong

    @Startup
    
    @Singleton
    
    public EntityManagerFactory getEntityManagerFactory() throws Exception {
        final String methodName = "getEntityManagerFactory";
        mLog.debug("Entered Class Name ::" + mClassName + " Method Name ::" + methodName);
        try {
            if (!mEntityManagerFactory.isOpen()) {
                createEntityManagerFactory();
            }
        } catch (Exception creationException) {
            mLog.error("Entered ::" + mClassName + " Method Name :: " + methodName + " Error ::" + creationException.getMessage());
            throw new ExceptionInInitializerError(creationException);
        }
        mLog.debug("Exited Class Name ::" + mClassName + " Method Name :: " + methodName);
        return mEntityManagerFactory;
    }
    
     /**
      * This method is used to create Entity Manager Factory to look the persistence unit name.
      * @author ashokkumar_ks@solartis.net
      * @since  08-Sep-2012
      */
    @PostConstruct
    public void createEntityManagerFactory() {
        final String methodName = "createEntityManagerFactory";
        mLog.debug("Entered Class Name ::" + mClassName + " Method Name ::"+ methodName);
        try {
            mEntityManagerFactory = Persistence.createEntityManagerFactory("StudentManagementSystem");
        } catch (Exception exception) {
            mLog.error("Entered ::" + mClassName +" Method Name :: "+ methodName + " Error ::" +exception.getMessage());
        }
        mLog.debug("Exited Class Name ::" + mClassName + " Method Name ::" + methodName + " Object Reference :: " +mEntityManagerFactory);
    }
    
    /**
     * This method is used to destroy the instance of entityManagerFactory.
     * @author ashokkumar_ks@solartis.net
     * @since 08-Sep-2012
     */
    @PreDestroy
    public void destroyEntityManagerFactory() {
        final String methodName = "destroyEntityManagerFactory";
        mLog.debug("Entered Class Name ::" + mClassName + " Method Name :: "+ methodName);
        try {
            if (mEntityManagerFactory != null && mEntityManagerFactory.isOpen()) {
                mEntityManagerFactory.close();
            }
        } catch (Exception exception) {
            mLog.error("Entered ::" + mClassName +" Method Name :: "+ methodName + " Error ::" +exception.getMessage());
        }
        mLog.debug("Exited Class Name ::" + mClassName + " Method Name :: " + methodName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a singleton class public sealed class Singleton { static Singleton instance=null; static
I've got a singleton class: public class Widget { private Mapper mapper; private static
using System; public sealed class Singleton { private static volatile Singleton instance; private static
(From Wikipedia) //Lazy initialization: public class Singleton { private static volatile Singleton instance =
So I have the following: public class Singleton { private Singleton(){} public static readonly
Suppose the following singleton: public class Test{ private static volatile Test test = null;
I implemented a Singleton pattern like this: public sealed class MyClass { ... public
I try to create a multi-threaded singleton pattern class. Header: class HL{ public: static
I structured my class like this: public class MyList: List<MyClass> { internal static bool
template <class T> class Singleton { public: static T& instance() { boost::call_once(init, flag); return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.