Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2 Actions having Transaction Management capabilities(calling 2 or more Service Layers)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All

I am implementing my app very similar to this tutorial at http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html

Some of my action classes attempt to persist data into 2 or more tables(i.e: 2 or more spring beans declared in the applicationContext.xml)
My applicationContext.xml is pretty much the exact same as that tutorial:



My issue is that I sometimes need 1 action class to invoke and update data in 2 or more tables.
Example from above. userAdministrationAction has a save() which will update the projectServiceImpl & userProjectServiceImpl beans by calling there respective service layers.


I need to find out how to do 2 things:

1) Since the save() in the userAdministrationAction calls 2 different service layers, how can I put this save() in a transaction? So that if the call to the 2nd service layers fails, it will roll back the call to the first service layer? I have researced all over the web and i cant seem to figure it out. The example I mentioned above at that URL does not have this scenario.

My userAdministrationServiceImpl class is transactional(spring will make sure its methods runs inside a transaction). However, since its the action that calls these service methods, the action itself is not in a transaction.

How can I implement this?

2) When an action calls multiple services, I used the shared entity manager to ensure its set on the services before calling the services methods. I have a feeling I am doing this incorrectly at the moment. What I currently do is as follows:

In userAdministrationAction:



If I do not implement the line above, and try and access methods in other services, I get a null pointer exception. This happens because the EntityManager is null.

Is there a better way of doing this than what I am doing at the moment?


I would appreicate help in those 2 questions. It would really help me move on considerably as I am stuck at the moment.

kind regards

Mick
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This isn't really Struts-related, since the underlying technology isn't relevant.

In any case, I (almost) always do this by creating a composite service and using JTA to nest transactions. Another option is to make an additional service that calls non-transactional operations and put the new service's call under a single transaction.

I'm moving this to... Spring?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Mick, did you ever find a solution to your problem?
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic