Как определить сессионный компонент как имеющий состояние или не имеющий состояния в EJB 2.x? - PullRequest
0 голосов
/ 16 октября 2018

Я работаю над проектом миграции с EJB 2.x на 3.1.Поскольку в EJB 2.x нет комментариев, я не могу определить, является ли сессионный компонент неактивным или не сохраняющим состояние.

Ниже приведен один из компонентов сеанса в приложении:

package com.guycarp.fac.casualty.ejb.copyFacAccount;

import javax.ejb.*;
import weblogic.ejb.*;

import com.guycarp.common.gcerror.GCException;
import java.rmi.RemoteException;
import java.sql.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
import java.math.BigDecimal;
import com.guycarp.fac.casualty.facManager.*;
import com.guycarp.fac.casualty.ejb.company.EBCompany.*; 
import com.guycarp.common.util.*;
import com.guycarp.fac.casualty.util.*;

/**
 * @ejbgen:session enable-call-by-reference="true" default-transaction="Supports"
 *   ejb-name = "SBCopyFacAccount"
 *
 * @ejbgen:jndi-name local="ejb.SBCopyFacAccountLocalHome"
 *   remote = "ejb.SBCopyFacAccountRemoteHome"
 *
 * @ejbgen:file-generation remote-class = "true" remote-class-name="SBCopyFacAccountRemote" remote-home = "true" remote-home-name="SBCopyFacAccountRemoteHome" local-class="true" local-class-name="SBCopyFacAccount" local-home="true" local-home-name="SBCopyFacAccountHome"
 */
public class SBCopyFacAccountBean
  extends GenericSessionBean
  implements SessionBean
{
  public void ejbCreate() {
    // Your code here
  }

  private SessionContext m_ctx = null;

    protected final String SBCOPY_JDBC_URL = "COPYACCOUNT_JDBC_URL";
    protected final String CLASS_NAME = "com.guycarp.fac.casualty.ejb.copyFacAccount.SBCopyFacAccountBean";
    private EBCompanyHome          m_EBCompanyHome           =  null;



    /**
     * @ejbgen:local-method
     * @ejbgen:remote-method
    */
    public String copyAccount(String p_OldCedantCompany, String p_OldFacAccountNumber , Double p_OldFacAccountVersion,
                            String p_NewCedantCompany, String p_UserId, 
                            BigDecimal idSubmission,Boolean isCedant,String lines, String renewal) throws GCException
    {

        Connection conn = null;
        CallableStatement cs = null;        
        //UserTransaction utx = null;
        String newFacAccountNumber=null;

        try {
            conn = getConnection();
            /** Using the new procedure for getUID which has an OUTPUT Parameter.
            */      
            //utx = m_ctx.getUserTransaction();
            //utx.begin();
            //System.out.println("lines " + lines);
            cs = conn.prepareCall("{call procCopyFacAccount(?, ?, ?, ?, ?, ?, ?, ?, ?,"+lines+",?,?,?)}");
            //System.out.println("p_OldCedantCompany " + p_OldCedantCompany);
            cs.setString(1,p_OldCedantCompany);
            //System.out.println("p_OldFacAccountNumber " + p_OldFacAccountNumber);
            cs.setString(2,p_OldFacAccountNumber);
            //System.out.println("p_OldFacAccountVersion " + p_OldFacAccountVersion);
            cs.setDouble(3,p_OldFacAccountVersion.doubleValue());
            //System.out.println("p_NewCedantCompany " + p_NewCedantCompany);
            cs.setString(4,p_NewCedantCompany);
            cs.setString(5,"FacExchange");
            cs.setString(6,"SBCopyFacAccount");
            //System.out.println("p_UserId " + p_UserId);
            cs.setString(7,p_UserId);   
            //System.out.println("idSubmission " + idSubmission);            
            cs.setBigDecimal(8,idSubmission); 
            if (isCedant != null) {
            //System.out.println("cedant");
                if (isCedant.booleanValue()) {
                    //System.out.println("cedant true");
                        cs.setInt(9,1);   
                } else {
                    //System.out.println("cedant false");
                        cs.setInt(9,0);     
                }               
            }                                                
            cs.setString(10,renewal);                                
            cs.registerOutParameter(11,java.sql.Types.VARCHAR);  
            cs.registerOutParameter(12,java.sql.Types.VARCHAR);  


            boolean b = cs.execute();   //HRH 2002-08-19 : Unused local variable
            int i = cs.getUpdateCount();//  HRH 2002-08-19 : Unused local variable          

            String ErrorMessage = cs.getString(11);
            //System.out.println("ErrorMessage= " + ErrorMessage);
            newFacAccountNumber = cs.getString(12);

            //System.out.println("SBCopyFacAccountBean newFacAccountNumber " + newFacAccountNumber);
            if ( ErrorMessage != null && !ErrorMessage.equals("") ) {
                throw new GCException( new Exception(ErrorMessage), ErrorMessage, false, false);                
            }

            //utx.commit();
            //utx = null;

        } catch ( Exception e ) {
            GCUtility.errorInfo(e.getMessage());
            throw new GCException( e, "Exception thrown in copyAccount.", false, false);
        } finally {
            /*
            if ( utx!= null ) {
                try {
                    utx.rollback();
                } catch ( Exception e ) {
                    e.printStackTrace();                    
                    throw new GCException( e, "Exception thrown in copyAccount.", false, false);
                }
            }
            */
            try {
                if ( cs != null ) cs.close();               
                if ( conn != null ) conn.close();
            } catch ( Exception e ) {
                GCUtility.errorInfo(e.getMessage());
                throw new GCException( e, "Exception thrown in copyAccount.", false, false);
            }
        }
        //before sending this newFacAccountNumber  back, set the optprimary for this account as 1.
        if (newFacAccountNumber != null) {

            newFacAccountNumber= newFacAccountNumber.trim();
            String accountNumber = p_OldCedantCompany + " " + newFacAccountNumber   + " " + FacConstant.FACACCOUNTVERSION_01;
            Connection con = null;
            PreparedStatement stmt= null;
            try {
                String sql = "UPDATE TBLFACACCOUNT SET OPTACCOUNTPRIMARY = 1 WHERE CDEFACACCOUNTNUMBER="+newFacAccountNumber
                +" and UIDCEDANTCOMPANY = "+getEBCompany(p_NewCedantCompany) +" and NUMFACACCOUNTVERSION="+FacConstant.FACACCOUNTVERSION_01;

                con = getConnection();
                stmt = con.prepareStatement(sql);
                int success = stmt.executeUpdate();

            }   catch (Exception    e) {
                GCUtility.errorInfo("Exception in copy account bean"+e.getMessage());
            }
        }

        return newFacAccountNumber ;
    }


    /**
     * @ejbgen:local-method
     * @ejbgen:remote-method
    */
    public void copyAccount(BigDecimal idSubmission, String newCedantCompany, 
                           String userId) throws GCException {


        Connection conn = null;
        CallableStatement cs = null;        
        //UserTransaction utx = null;

        try {
            conn = getConnection();

            /** Using the new procedure for getUID which has an OUTPUT Parameter.
            */      
            //utx = m_ctx.getUserTransaction();
            //utx.begin(); 

            cs = conn.prepareCall("{call procCopyFacAccountsSubmission(?, ?, ?, ?, ?)}");
            cs.setDouble(1,idSubmission.doubleValue());
            cs.setString(2,newCedantCompany);
            cs.setString(3,"FacExchange");
            cs.setString(4,"SBCopyFacAccountBean");
            cs.setString(5,userId);          
            cs.registerOutParameter(6,java.sql.Types.VARCHAR);          

            boolean b = cs.execute();   
            int i = cs.getUpdateCount();

            String ErrorMessage = cs.getString(6);
            if ( ErrorMessage != null && !ErrorMessage.equals("") ) {
                throw new GCException( new Exception(ErrorMessage), ErrorMessage, false, false);                
            }

            //utx.commit();
            //utx = null;

        } catch ( Exception e ) {
            GCUtility.errorInfo(e.getMessage());
            throw new GCException( e, "Exception thrown in copyAccount of "+ CLASS_NAME, false, false);
        } finally {
            /*
            if ( utx!= null ) {
                try {
                    utx.rollback();
                } catch ( Exception e ) {
                    e.printStackTrace();                    
                    throw new GCException( e, "Exception while rolling back in copyAccount of "+ CLASS_NAME, false, false);
                }
            }
            */
            try {
                if ( cs != null ) cs.close();             
                if ( conn != null ) conn.close();
            } catch ( Exception e ) {
                GCUtility.errorInfo(e.getMessage());
                throw new GCException( e, "Exception while closing connection in copyAccount of "+ CLASS_NAME, false, false);
            }
        }               
    }

    private  Double getEBCompany(String cdeCompanyNumber) throws GCException{
        Double uidCompany = null;

        // @@ uncomment this line when eb is ready
        //
        try {               
            EBCompanyHome m_EBCompanyHome = (EBCompanyHome)GCHomesCache.getHome(EBCompanyHome.class,FacConstant.EBCOMPANY_JNDI_URL);  

            EBCompany ebCompany =   m_EBCompanyHome.findByCompanyNumber(cdeCompanyNumber);
            if (ebCompany != null) {
                // @@ line changed
                //uidCompany = ebCompany.getID();
                uidCompany = new Double(ebCompany.getM_uidCompany().doubleValue());
            }

                return uidCompany;
        } catch (ObjectNotFoundException e) {
                return null;
        } catch(Exception e) {
            throw new GCException (e,"Exception in getEBCompany(String cdeCompanyNumber) of copy account process",false,false);
        }
        //
    }                   

    protected Connection getConnection() throws Exception {
        InitialContext ic = new InitialContext();
        DataSource dataSource = (DataSource) ic.lookup("jdbc.FacExchangeDS");
        return dataSource.getConnection();
    }



}

Я использую JDeveloper в качестве IDE.У меня есть некоторые сомнения по поводу миграции.Я вижу, что в проекте EJB 2.x есть несколько объектов управления данными.Но в EJB 3.1 нет сущностных компонентов.Какова цель бина сущности и его альтернатива?

1 Ответ

0 голосов
/ 26 октября 2018

Возможно, у вас есть ejb-jar.xml внутри вашего проекта .ear или .jar, у этого XML есть свойства о ваших EJB.Там должна быть информация о лицах без гражданства или о состоянии там.В этом XML вам может понадобиться другая информация, такая как удаленный или домашний интерфейсы, ссылки и имена классов.

Здесь у меня есть пример:

    ...
    ...
    <session>
        <display-name>myEjbExample</display-name> 
        <ejb-name>myEjbExample</ejb-name>
        <home>com.test.myEjbExampleHome</home>
        <remote>com.test.myEjbExampleRemote</remote>
        <ejb-class>com.test.myEjbExample</ejb-class>

        <!-- this tag indicates what you need -->
        <session-type>Stateless</session-type>

        <transaction-type>Bean</transaction-type>
        <ejb-ref>
            <ejb-ref-name>EjbExample</ejb-ref-name>
            <ejb-ref-type>Entity</ejb-ref-type>
            <home>com.test.myEjbExampleHome</home>
            <remote>com.test.myEjbExample</remote>
            <ejb-link>myEjbExample</ejb-link>
        </ejb-ref>
        <resource-ref>
            <res-ref-name>jdbc/myConn</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Application</res-auth>
        </resource-ref>
   </session>
   ...
   ...
...