WebSphere Extended Deployment, Version 6.0.x     운영 체제: AIX, HP-UX, Linux, Solaris, Windows

세션 Bean 프로그래밍

WSAD V5.1에서 AccountTransactionBean.java를 열고 코드를 조사하십시오. 세션 Bean에서 자원 참조 이름 및 분석된 해당 JNDI 이름이 정의됩니다. 이 Bean에 있는 다음의 코드가 이러한 속성을 정의합니다.
    /** The proxy datasource resource reference name */
    private static String proxyResRef = "jdbc/proxyDS";

    	/** DataSource resource reference 1 */
    	private static String resRef1 = "jdbc/myDS1";

    	/** DataSource resource reference 2 */
    	private static String resRef2 = "jdbc/myDS2";

    	/** Global datasource JNDI name for the DataSource resource reference 1 */
    	private String globalDSJNDIName1 = null;

    	/** Global datasource JNDI name for the DataSource resource reference 2 */
    	private String globalDSJNDIName2 = null;
    
    /** Globle datasource JNDI name for the Proxy Datasource resource reference */
    private String globalProxyDSJNDIName = null;
또한 WSProxyDataSourceHelper 인스턴스를 정의하면 세션 Bean에서 이 인스턴스를 사용하여 데이터 소스 JNDI 이름을 해석하고 트랜잭션의 데이터 소스 JNDI 이름을 설정할 수 있습니다.
	/** 
	 * the WSProxyDataSourceHelper instance used to set the datasource JNDI name
	 * for the current transaction and resolve the datasource resource reference 
	 * to the global JNDI name
	 */
	WSProxyDataSourceHelper dsHelper = null;

setSessionContext 메소드는 다음과 같습니다.
	public void setSessionContext(javax.ejb.SessionContext ctx) {
                  mySessionCtx = ctx;

        // Lookup the WSProxyDataSourceHelper, and the EJB local homes
        try {   
            			InitialContext ic = new InitialContext();
            dsHelper = (WSProxyDataSourceHelper) ic.lookup(WSProxyDataSourceHelper.JNDI_NAME);
            			accountHome = (AccountLocalHome) ic.lookup(accountHomeJNDIName);
            			accountOwnerHome = (AccountOwnerLocalHome) ic.lookup(accountOwnerHomeJNDIName);
        }
              catch(Exception e){
            			throw new EJBException(e);
        }
        
        		// Get the server name
        		AdminService service = AdminServiceFactory.getAdminService();
        SERVER_NAME =
            			service.getCellName()
                + "/"
                				+ service.getNodeName()
                + "/"
                				+ AdminServiceFactory.getAdminService().getProcessName();

        		// Resolve the resource references to the global datasource JNDI names
        try {   
            			globalDSJNDIName1 = dsHelper.resolveDataSourceReference(resRef1);
            			globalDSJNDIName2 = dsHelper.resolveDataSourceReference(resRef2);
            globalProxyDSJNDIName = dsHelper.resolveDataSourceReference(proxyResRef);
        }
        		catch (ResRefNotFoundException rrnfe) {
            			throw new EJBException(rrnfe);
다음 명령문을 사용하여 JNDI 네임 스페이스에서 WSProxyDataSourceHelper를 검토하십시오.
dsHelper = (WSProxyDataSourceHelper ) ic.lookup(WSProxyDataSourceHelper .JNDI_NAME);
그런 다음 보통 때와 같이 AccountLocalHomeAccountOwnerLocalHome을 검토하십시오. 그리고 AdminService API를 사용하여 서버 이름을 확보하십시오. 이 서버 이름을 사용하여 파티션 라우팅이 올바른지 확인하십시오. 사용자 응용프로그램에서는 이 작업을 수행하지 않아도 됩니다. 파티션 기능 런타임이 올바른 파티션 라우팅 작동을 보장합니다.
다음 명령문을 호출하여 데이터 소스 참조를 해석하십시오.
globalDSJNDIName1 = dsHelper.resolveDataSourceReference(resRef1);
응용프로그램 개발 중 개발자는 이 자원 참조를 맵핑할 JNDI 이름을 알지 못합니다. 프록시 데이터 소스 컴포넌트에서는 데이터 소스의 JNDI 이름을 확보하도록 응용프로그램에 API를 제공합니다. 이 JNDI 이름 데이터 소스는 데이터 소스에 사용해야 하는 런타임을 알려주는 데 사용됩니다.
이제 비즈니스 메소드를 봅니다. 다음은 AccountTransactionBean.java에 있는 비즈니스 메소드 withdraw 중 하나의 코드 스니펫입니다.
	/**
	 * withdraw money from an account
	 * @param accountId
	 * @param amount
	 * @return
	 * @throws InSufficientFundException
	 */
	public String withdraw(String accountId, float amount) throws 
  InSufficientFundException {
		// Set the datasource this transaction will access.
		setDataSource(accountId);

		try {
			AccountLocal account = accountHome.findByPrimaryKey(accountId);
			account.withdraw(amount);
		}
		catch (ObjectNotFoundException onfe) {
			throw new EJBException(onfe);
		}
		catch (FinderException fe) {
			throw new EJBException(fe);
		}
		return SERVER_NAME;
	}

이 메소드와 표준 메소드의 차이는 다음 명령문을 추가한다는 점입니다.
		// Set the datasource this transaction will access.
		setDataSource(accountId);
setDataSource 메소드는 다음과 같습니다.
	    /**
     	 * Set the datasource the CMP is going to use for the current transaction.
     	 * If the accountID starts with W, datasource 1 will be used. If the accountID 
     	 * starts with E, datasource 2 will be used. 
     * 
     	 * @param s
     */
    	private void setDataSource(String s) {
        		if (s.startsWith("W")) {
            dsHelper.setCurrentDataSourceJndiName(globalProxyDSJNDIName, globalDSJNDIName1);
        }
        else {
            dsHelper.setCurrentDataSourceJndiName(globalProxyDSJNDIName, globalDSJNDIName2);
        }
    }
setDataSource 메소드는 WSProxyDataSourceHelper를 사용하여 현재 트랜잭션의 현재 데이터 소스 JNDI 이름을 설정합니다. accountId가 W로 시작하는 경우(계정 ID를 서부 해안 데이터베이스에 저장해야 함을 의미함) JNDI 이름 globalDSJNDIName1이 스레드에서 설정됩니다. accountId가 E로 시작하는 경우(계정 ID를 동부 해안 데이터베이스에 저장해야 함을 의미함) JNDI 이름 globalDSJNDIName2가 스레드에서 설정됩니다.

이제 응용프로그램이 프록시 데이터 소스 지원에서 사용 가능합니다. 다음 단계에서는 DB2 데이터 소스 및 프록시 데이터 소스를 구성하는 방법을 표시합니다.




Related concepts
WSAD의 프록시 데이터 소스 지원을 사용한 응용프로그램 개발

참조 주제:    

이용 약관 | 피드백 마지막 갱신 날짜: Mar 21, 2006 11:38:53 AM EST
http://publib.boulder.ibm.com/infocenter/wxdinfo/v6r0/index.jsp?topic=?topic=/com.ibm.websphere.xd.doc/info/WPF51/rwpfprgramsessionbean.html

© Copyright IBM 2005, 2006. All Rights Reserved.
이 Information Center는 Eclipse 테크놀러지로 강화되었습니다. (http://www.eclipse.org)