@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;
}
}
これは私のビジネスログです
@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 メソッドを呼び出すたびに...新しいオブジェクトが作成され、ビジネス ロジックにアクセスするたびに.. ?? しかし、Singleton Mean ... 1回限りのインスタンス作成????
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 - 終了したクラス名 ::EntityManagerFactoryUtilMethod 名::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] DEBUG com.dms.util.EntityManagerFactoryUtil - 終了したクラス名 ::EntityManagerFactoryUtilMethod 名::getEntityManagerFactory オブジェクト リファレンス :: org.hibernate.ejb. EntityManagerFactoryImpl@5170a8
毎回異なるオブジェクトが作成されます.. :(