Thursday, July 24, 2014

JPA - JTA or Resouce_Local

Before using JPA, we have to consider to use transaction type as JTA or Resource_Local because it will cause different coding and transaction management.


  • JTA - container-managered entity manager
  1. injected to app automatically by j2ee container
  2. close/rollback transaction automatically without extra coding
  3. transaction propagation supported by j2ee container
  4. multiple data resources are required, such as more than 1 DB, DB and JMS,etc  
  • Resource_Local - application-managed entity manager
  1. can be used in j2ee container, J2SE and web container
  2. need to create entity manager explicitly
  3. need to close entity manager explicitly
  4. NO transaction propagation
But I think we can rely on Spring framework to inject entity manager and support transaction propagation.


  • eliminate DAO or JPA DAO Pattern
There are some discussion on DAO tier since JPA is introduced. I prefer to adopt jpa dao pattern personally.

1. A generic DAO interface

public interface GenericDao <T, PK extends Serializable> {

    PK create(T newInstance);
    T read(PK id);
    void update(T transientObject);
}

2. implement the genericDAO
3. For particular entity, if any new behavior is required
public interface UserDAO extends GenericDao<Person,ID> {
// define new method
List<User> findByName(String name);
}


Reference URL:
http://employees.oneonta.edu/higgindm/sweng/Generic_DAO.htm
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

No comments:

Post a Comment