2012-09-25 116 views
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; 
    } 

回答

3

無狀態會話bean是一個對象,它沒有關聯的會話狀態,但可能有實例狀態。它不允許併發訪問該bean。實例變量的內容不保證在方法調用中保留。客戶端應將所有無狀態會話bean的實例視爲相同。 本地無狀態會話Bean的Hello World例子:

import javax.ejb.Stateless; 

@Stateless 
public class HelloWorldBean { 
    public String getHello() { 
     return "Hello World !"; 
    } 
} 
import java.io.*; 
import javax.ejb.EJB; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class TestServlet extends HttpServlet { 
    @EJB 
    private HelloWorldBean helloWorld; 

    public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     resp.getWriter().println(helloWorld.getHello()); 
    } 
} 

狀態會話Bean

一個對象的狀態由它的實例變量。在有狀態會話bean中,實例變量表示唯一客戶bean會話的狀態。客戶與bean的交互被稱爲會話狀態。

實施例:

package test; 
import java.util.concurrent.TimeUnit; 
import java.util.logging.Logger; 
import javax.ejb.PostActivate; 
import javax.ejb.PrePassivate; 
import javax.ejb.Stateful; 
import javax.ejb.StatefulTimeout; 

@Stateful 
@StatefulTimeout(unit = TimeUnit.MINUTES, value = 30) 
public class StatefulTestBean { 
private static final Logger logger = Logger.getLogger("test"); 

private String clientInfo; 

@SuppressWarnings("unused") 
@PrePassivate 
private void prePassivate() { 
    logger.info("In PrePassivate method"); 
} 

@SuppressWarnings("unused") 
@PostActivate 
private void postActivate() { 
    logger.info("In PostActivate method"); 
} 

public String getBeanInfo() { 
    return this.toString(); 
} 

public String getClientInfo() { 
    return clientInfo; 
} 

public void setClientInfo(String clientInfo) { 
    this.clientInfo = clientInfo; 
} 
}