Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JPA Certification (OCEJPAD)
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
JPA Certification (OCEJPAD)
code modification for chapter 6's extendedPersistenceContext.
Himai Minh
Bartender
Posts: 2418
13
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In this example,
@Stateful //I don't think this is necessary as a new transaction is always created, so I commented it out. //@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class DepartmentManager { //This PC will be extended/propagated/visible to the audit bean @PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED) EntityManager em; Department dept; @EJB AuditService audit; public void addEmployee(int empId) { Employee emp = em.find(Employee.class, empId); dept.getEmployees().add(emp); emp.setDepartment(dept); audit.logTransaction(emp.getId(), "added to department " + dept.getName()); }
@Stateless public class AuditService { @PersistenceContext(unitName="EmployeeService") private EntityManager em; // I added @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) to this example. // Even thought the transaction is new, //the pc is extended or propagated from the department manager bean. //It works. @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void logTransaction(int empId, String action) { // verify employee number is valid if (em.find(Employee.class, empId) == null) { throw new IllegalArgumentException("Unknown employee id"); } LogRecord lr = new LogRecord(empId, action); em.persist(lr); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Persistence Context Collision explanation in chapter 6
getTransaction() on transaction-scoped persistence context.
SessionEdit pattern
Why EntityManager. joinTransaction is not needed for shared application managed JTA
Unable to use @NamedQuery in a session bean?
More...