You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Yaragalla Muralidhar <ya...@gmail.com> on 2016/04/09 18:36:45 UTC

synchronization problem

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/>*

Re: synchronization problem

Posted by Yaragalla Muralidhar <ya...@gmail.com>.
thank you Doug Erickson. I will look into other areas and will check where
the problem is. Thank you so much.

*Thanks and Regards,*
Muralidhar Yaragalla.

*http://yaragalla.blogspot.in/ <http://yaragalla.blogspot.in/>*

On Sun, Apr 10, 2016 at 11:02 AM, Doug Erickson <er...@part.net> wrote:

> The code you've shown won't allow simultaneous executions of the DAO
> method. Either you've left some important details out of the sample, or you
> have other code accessing the DAO.
>
> This has nothing to do with Struts. The synchronization is implemented in
> lower layers.
>
> > On Apr 9, 2016, at 8:11 PM, Yaragalla Muralidhar <
> yaragallamurali@gmail.com> wrote:
> >
> > 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.nair@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 <
> >> yaragallamurali@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/>*
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: synchronization problem

Posted by Doug Erickson <er...@part.net>.
The code you've shown won't allow simultaneous executions of the DAO method. Either you've left some important details out of the sample, or you have other code accessing the DAO. 

This has nothing to do with Struts. The synchronization is implemented in lower layers.

> On Apr 9, 2016, at 8:11 PM, Yaragalla Muralidhar <ya...@gmail.com> wrote:
> 
> 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.nair@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 <
>> yaragallamurali@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/>*
>> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: synchronization problem

Posted by Yaragalla Muralidhar <ya...@gmail.com>.
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.nair@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 <
> yaragallamurali@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/>*
> >
>

Re: synchronization problem

Posted by "Sreekanth S. Nair" <sr...@egovernments.org>.
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 <
yaragallamurali@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/>*
>