synchronized block in ejb2 stateless session bean

Go To


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){                       
                if(pinGenerateSessionBeanLocalHome != null){
                    IPINGenerateSessionBeanLocal  pinGenerateSessionBean = pinGenerateSessionBeanLocalHome.create();
                        if(pinGenerateSessionBean != null){
                            resultObject= pinGenerateSessionBean.generatePIN(pinBatchCustomData,iSessionInfo);
                            if(resultObject.getResponseCode() == PINResponseCode.SUCCESS_RESPONSE_CODE){
                                pinBatchCustomData= (PINBatchCustomData)resultObject.getResponseObject();                        
                                bSuccess = true;   
                                bSuccess = false;
                                debugLog(PIN_MODULE_NAME,"Insertions regarding PINs could not be made successfully ");
                            bSuccess = false;
                            debugLog(PIN_MODULE_NAME,"PINGenerateSession Local Is Null ");
                    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.

2012-04-04 07:24
by chetan


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.

2012-04-04 14:18
by fglez