0

In my Java SE/Java EE application I have a DAO interface to wrap around entity manager to allow simple CRUD operations. I also have an extended DAO interface that allows the simple CRUD operations to be performed as a batch operation, meaning all operations will be done under the same transaction. This allows me to roll back if one of the sequential CRUD operation fails.

public interface DomEntityManager {
   void createEntity(Object aEntity) throws TransactionException;
   void deleteEntity(Object aEntity) throws TransactionException;
   void updateEntity(Object aEntity) throws TransactionException;
   <T> T findById(int aId, Class<T> aClass);
   <T> T getNewEntity(Class<T> aClass) throws TransactionException;
}

public interface BatchDomEntityManager extends DomEntityManager {
   void execute(BatchTransation transaction);
}

public interface BatchTransaction {
   public void run();
}

In this above design, what I am trying to achieve is, if the client calling code calls any of the DomEntityManager operations within the execute BatchTransaction method, then I want the operations to use a single transaction.

Currently, the Dom entity manager implementation has the straight forward implementation, where for each request an entity manager is created and new transaction is started and committed at the end of the request inside the DAO method. I am little stuck as how can I implement the share entity manager case. Should I be using ThreadLocal, in which case should the execute transaction block be executed in a separate thread. It's ok for me to block the execute call until the transaction is completed.

Thanks for any suggestion.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
user320587
  • 1,347
  • 7
  • 29
  • 57
  • 1
    So, is your app a Java SE or Java EE application? What's your environment? How are you creating your entity manager? – JB Nizet May 10 '13 at 15:37
  • The entity manager will be created using the EntityManagerFactory.createEntityManager method and planning to use RESOURCE_LOCAL transaction. I am trying to build a component that can be accessed by both a J2SE application and J2EE application (if needed) – user320587 May 10 '13 at 15:59
  • 1
    A Java EE application will not use RESOURCE_LOCAL. And it will use dependency injection to get a reference to the entity manager. I would say that a Java SE application should also use dependency injection and AOP to handle transactions declaratively, using Spring for example. Trying to build DAOs and use JPA without knowing the environment is not something I would try. I would at least leave the responsibility of the entity manager creation/injection and the responsibility of transaction management to the caller of the DAO. – JB Nizet May 10 '13 at 16:06
  • @JBNizet Unfortunately, I can't use Spring. – user320587 May 10 '13 at 21:20
  • >`In my Java SE/Java EE application` - This is not 100% logical. Typically your application is either Java SE or Java EE, but never or only rarely both. Or do you mean you have a Java SE application talking to a Java EE one? – Arjan Tijms May 11 '13 at 15:31
  • Sorry, what I meant is I have a Java SE and Java EE application both wanting to communicate with the same database. I am trying to create a reusable JPA component that I could use on both the applications. – user320587 May 13 '13 at 00:23

0 Answers0