Struts2 won't make your custom class threadsafe unless your code is
threadsafe, synchronized(this) or synchronized method can be used in your
service implementation if you think your service class is not threadsafe.
Usually Service or DAO layer should be written threadsafe, don't know about
your DAO or Service is threadsafe to make a clear comment on it. Action
classes are thread safe since it creates new instance per request.


--------------------

On Sat, Apr 9, 2016 at 10:06 PM, Yaragalla Muralidhar <
yaragallamur...@gmail.com> wrote:

> Hi,
>
>      I have developed a web application using struts 2. In my struts 2
> action class i am calling the service layer and written the synchronization
> code as below in the service layer. Does this really gets synchronized? Is
> there any problem in my synchronization code?
>
>
>
> *The following is my action class:-*
>
>
>
> public class AbcAction extends ActionSupport{
>
>      private AbcService ser=new AbcServiceImpl(); //AbcService is an
> Interface
>
>
>
>      public String execute(){
>
>      ser.createNewRegistration();
>
>      return "SUCCESS";
>
>      }
>
>
>
> }
>
>
>
> *The following is my service Impl class:-*
>
>
>
> public class AbcServiceImpl implements AbcService{
>
> private AbcDao dao=new AbcDaoImpl();
>
>
>
> public void  .createNewRegistration(){
>
> synchronized(AbcServiceImpl.class){
>
> dao.createTheRegistration();
>
> }
>
> }
>
> }
>
>
>
> by writing the above code i assume that no two threads can run the
> "dao.createTheRegistration()"
> method at the same time. but when we analyze the logs i observed this is
> not happening. Two threads are able to execute the method at the same time.
> What is the problem in my code?
>
>
>
> *Thanks and Regards,*
> Muralidhar Yaragalla.
>
> *http://yaragalla.blogspot.in/ <http://yaragalla.blogspot.in/>*
>

Reply via email to