简体   繁体   中英

synchronized block in ejb2 stateless session bean

So far i have heard that synchronized is not recommended for ejb session bean.

I have one problem that i resolved by using synchronized.

Code where i used synchronized.

if(strBatchID != null && strBatchNumber != null){                       
                pinGenerateSessionBeanLocalHome=getPINGenerateSessionBeanLocalHome();
                if(pinGenerateSessionBeanLocalHome != null){
                    IPINGenerateSessionBeanLocal  pinGenerateSessionBean = pinGenerateSessionBeanLocalHome.create();
                    synchronized(pinGenerateSessionBean){
                        if(pinGenerateSessionBean != null){
                            resultObject= pinGenerateSessionBean.generatePIN(pinBatchCustomData,iSessionInfo);
                            if(resultObject.getResponseCode() == PINResponseCode.SUCCESS_RESPONSE_CODE){
                                pinBatchCustomData= (PINBatchCustomData)resultObject.getResponseObject();                        
                                bSuccess = true;   
                            }else{
                                bSuccess = false;
                                debugLog(PIN_MODULE_NAME,"Insertions regarding PINs could not be made successfully ");
                            }   
                        }else{
                            bSuccess = false;
                            debugLog(PIN_MODULE_NAME,"PINGenerateSession Local Is Null ");
                        }
                    }
                }else{
                    bSuccess = false;
                    debugLog(PIN_MODULE_NAME,"PINGenerateSession Local Home Is Null ");
                }
            }

Check line where i used synchronized(pinGenerateSessionBean) to synchronized pingeneration session bean object.

It works fine. Before that i have problem when i tried to generate two batch simultaneously.

Is it create any problem? Performance is not an issue for me.

synchronized keyword is not allowed there by the EJB 2.1 specification .

25.1.2 Programming Restrictions

This section describes the programming restrictions that a Bean Provider must follow to ensure that the enterprise bean is portable and can be deployed in any compliant EJB 2.1 container. The restrictions apply to the implementation of the business methods.

[...]

  • An enterprise bean must not use thread synchronization primitives to synchronize execution of multiple instances.

I think it would be better to use a singleton pattern ( Singleton Beans are available in EJB 3.1) for 'pin generation'.

You can follow this approach also with EJB 2.1, by sizing the corresponding pool in your EJB container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM