I'm currently trying to use an application-driven context context by manually creating an entity manager and saving them to include a transaction that spans multiple query requests (possibly something like an extended persistence context) in a JSE application.
But I am wondering if I can avoid sending the entityManager object throughout the service and DAO methods as an additional parameter using spring @PersistenceContext injection and mark the methods with @Transactional annotation to use a manual transaction with this entity manager.
I think I can somehow handle this using ThreadLocal for this function, but I will be happier if I can attach this to the spring structure.
This is an example of what I mean:
User Interface Action Method:
Here we see that the transaction begins with the ui logic, since the facade / command method is not used in the backend to group these calls into business logic:
Long transactionid = tool.beginTransaction(); // calling business methods tool.callBusinessLogic("purchase", "receiveGoods", paramObject1, transactionid); tool.callBusinessLogic("inventory", "updateInventory", paramObject2, transactionid); tool.commitTransaction(transactionid);
Inside the tool:
public Long beginTransaction() { // create the entity --> for the @PersistentContext Entitymanager entityManager = createEntityManagerFromFactory(); long id = System.currentTimeMillis(); entityManagerMap.put(id, entitymanager); // start the transaction --> for the @Transactional ? entityManager.getTransaction().begin(); return id; } public void commitTransaction(Long transactionId) { EntityManager entityManager = entityManagerMap.get(transactionId); entityManager.getTransaction().commit(); } public Object callBusinessLogic(String module, String function, Object paramObject, Long transactionid) { EntityManager em = entityManagerMap.get(transactionId); // ================================= // HOW TO DO THIS???? // ================================= putEntityManagerIntoCurrentPersistenceContext(em); return executeBusinessLogic(module, function, paramObject, transactionid); }
And an example of a service method:
public class Inventory {
Anyway to achieve this?
Thanks!
bertie
source share