• Post Reply 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

JPA/JTA in a non EJB project

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys

Im working on a java struts project. each struts action instantiates each attribute. No injection happens
So simply each action class instantiates service layer




I want to introduce JPA/JTA and be able to set @Transactional at a service class level (javax.transaction.Transactional) or similar
Can i inject entity manager into POJOs or do something like



Only way I can get a entity manager is by



but every time i do this a new EM is created and therefore a new transaction scope
service class creates a EM and i begin a transaction (RESOURCE_LOCAL)
service class calls Method in DAO class
Dao Class creates a new EM but this is not in same transaction - em.joinTransaction Method does not work

I dont want to pass em round as parameters. If i can acquire em from InitialContext then i believe ill be using same em at service and dao layers

Can i inject EM into pojos using CDI? any concrete example of this as I cant find one or anyone have a solution I could use. Ive tried javax.transaction.UserTransaction but this gave sporadic tranaction issues


 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do this in JSF. JPA didn't exist back when I worked with Struts. The same concepts apply, however.

What I do is actually define 2 persistence tiers: The business persistence tier (services) and the per-table persistence tier (DAOs). Although classes in both tiers are defined transactional, the business persistence tier is the one normally entered from the business logic (and hence from the Struts Action), so when business persistence invokes DAOs, they're part of the business persistence transaction.

Just in case it isn't clear - a lot of operations require a graph of several different tables in order to do their thing, My DAOs are intended for per-table CRUD operations (although I sometimes include parent-child table constructs). The Service methods work with the total graph, and invoke the DAOs for their sub-components.

Once the service has returned to its caller, not only is the transaction completed (committed or rolled back), but also, if it returns a graph, that graph consists of detached objects. I don't believe in keeping "live" ORM model objects at the higher levels or passing live objects to the rendering phase. It's extra overhead and somewhat dangerous.

I also cheat. I use the Spring Framework to manage the transactions and inject the EntityManagers into the DAOs. The service methods do not persist directly (they invoke DAOs), so even though they are transactional, EntityManagers are not injected into them.
 
Ally Cavs
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply Tim. So I don't want to introduce spring into the system.

So what options have I. Can I inject into pojos using CDI (beans.XML) or programmatically. I'd love to see a good example of either where the entitymanager can be reused across various dao classes.

 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dunno. I use Spring because it not only handles manufacture and injection, but because it also manages a lot of the grunt work needed to avoid things like free-floating connections when an operation fails, does transaction management for non-EJB entities and so forth.

CDI hasn't impressed me so far. It's not as compatible with Spring as I would have expected and not as powerful either, from what I can see. I have the (possibly incorrect) impression that CDI was intended to support full-fledged Version 3 EJBs and expects the EJB container to deal with that kind of stuff. Which, of course, a vanilla Tomcat or jetty server cannot do.
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic