0
我開發了無狀態會話bean。但是我的嚮導告訴我這是有狀態的會話bean。因爲我在課堂上宣佈了@EJB。那就是成員變量。如何識別有狀態或無狀態會話bean
說明如何使用有狀態和無狀態會話bean。我需要知道在無狀態會話bean中創建池
@Stateless
@LocalBean
@TransactionManagement(TransactionManagementType.BEAN)
public class StudentManagerBean{
/**
* This attribute mClassname is for logging the Class Name
*/
private static String mClassName = "StudentManagerBean";
/**
* This attribute mLog used for write to log.
*/
private static Logger mLog = Logger.getLogger(StudentManagerBean.class.getName());
/**
* Reference to EntityManagerFactoryUtil, used to create Entity Manager Factory and get persistence unit name.
*/
@EJB
private EntityManagerFactoryUtil mEntityManagerFactory;
/**
* This method is used to save student information such as name,id,phone number,email id,department and year to the Student Management System</br>
* @param studentDetail
* @return studentDetail
* @since 05-Sep-2012
* @author [email protected]
*/
public StudentDetail save(StudentDetail studentDetail) {
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(studentDetail);
entityTransaction.commit();
}
}
}
} catch (Exception saveException) {
try {
if(entityTransaction!=null && entityTransaction.isActive()) {
entityTransaction.rollback();
}
}catch (Exception transactionException) {
mLog.warn("Exception in ClassName :: " + mClassName+ " MethodName::" + methodName + " warn :: "
+ transactionException.getMessage() + "Student ID :: "+studentDetail.getId());
}
mLog.error("Exception in ClassName :: " + mClassName+ " MethodName::" + methodName + " error :: "
+ saveException.getMessage() + "Student ID :: "+ studentDetail.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 :: "+ studentDetail.getId());
}
}
mLog.debug("Exited ClassName::" + mClassName + " MethodName :: "+ methodName + "Student ID :: "+ studentDetail.getId());
return studentDetail;
}