1
@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;
}

}


这是我的商业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;
}

每当我调用 Save 方法时……每次创建新对象并访问业务逻辑时……?? 但是单例意味着......一次实例创建????

10:31:49,413 INFO [stdout] (http--0.0.0.0-8080-2) 22754 [http--0.0.0.0-8080-2] 调试 com.dms.util.EntityManagerFactoryUtil - 退出类名 ::EntityManagerFactoryUtil 方法名::getEntityManagerFactory 对象参考 :: 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] 调试 com.dms.util.EntityManagerFactoryUtil - 退出类名 ::EntityManagerFactoryUtil 方法名::getEntityManagerFactory 对象参考 :: org.hibernate.ejb。EntityManagerFactoryImpl@5170a8

每次创建不同的对象时.. :(

4

4 回答 4

1

@Singleton注解适用于会话 bean,而不适用于实用程序类

Component-defining annotation for a singleton session bean.

您可以使用@PersistenceContext注解来指定实体管理器工厂并注入EntityManager。

 @PersistenceContext(unitName = "StudentManagementSystem")
 private EntityManager entityManager;
于 2012-09-18T05:21:07.167 回答
1

Singleton 类EntityManagerFactoryUtil仍然是唯一的,但它会在每次调用时创建一个新的 EntityManagerFactory 实例。

使用 PersistenceContext 的注入,以便让容器在您的应用程序中传播它。

链接:Java EE 6 教程

于 2012-09-18T05:36:04.393 回答
1

最后我得到了解决方案..请让我知道,有什么问题

@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);
}
于 2012-09-18T09:08:09.440 回答
0

单例是一种设计模式,这意味着您必须以这样的方式实现您想要的单例类,以防止重复它的多个实例。

示例实现例如:

public class Singleton {
        private static volatile Singleton instance = null;

        private Singleton() {   }
        // Each time get Instance is called the actuall one is checked,
        // if exists no new one is created. Only if doesnt a new one is created       
        public static Singleton getInstance() { 
                if (instance == null) {
                        synchronized (Singleton.class){
                                if (instance == null) {
                                        instance = new Singleton();
                                }
                      }
                }
                return instance;
        }
}

在您的示例中,MethodegetEntityManagerFactory等效于getInstanceMethode

于 2012-09-18T05:29:13.980 回答