Reputation: 1058
@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 [email protected]
* @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.. :(
Upvotes: 1
Views: 528
Reputation: 1058
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 [email protected]
* @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 [email protected]
* @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);
}
Upvotes: 1
Reputation: 16273
The Singleton class EntityManagerFactoryUtil
is still unique, but it is creating a new Instance of EntityManagerFactory on each call.
Use injection of the PersistenceContext in order to have it propagated by the container across your application.
Links: Java EE 6 Tutorial
Upvotes: 1
Reputation: 37566
A Singleton is a Design pattern, this means you have to implement your desired Singleton class in s uch a way that it will prevent rceating more than one Instance of it.
an Example Implementation would be for Example:
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;
}
}
In your example the Methode getEntityManagerFactory
is equivalent to the getInstance
Methode
Upvotes: 0
Reputation: 19185
@Singleton annotation is for session beans not for utility classes
Component-defining annotation for a singleton session bean.
You can use @PersistenceContext annotation to specify entity manager factory and inject EntityManager.
@PersistenceContext(unitName = "StudentManagementSystem")
private EntityManager entityManager;
Upvotes: 1