hi, if you look at my code in the service class i have written a synchronized block. what i am asking is, whether my code does the synchronization properly or does it have to be changed?
I know that struts2 creates a new action class per thread. so there is every possibility that two threads can execute the same service layer method at a time. I want to avoid that so i have written synchronized block in my service class as shown in my first post. *Thanks and Regards,* Muralidhar Yaragalla. *http://yaragalla.blogspot.in/ <http://yaragalla.blogspot.in/>* On Sat, Apr 9, 2016 at 11:18 PM, Sreekanth S. Nair < sreekanth.n...@egovernments.org> wrote: > 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/>* > > >