简体   繁体   中英

Multiple logins of a persistence unit?

Hey guys I'm using eclipse link for JPA in my Java SE project. I'm using "jpa controller" classes for each of my tables in my databases(is this the correct way). I'm also providing an EntityManagerFactory for every "jpa controller class" so I can get the entitymanager. When the program starts up it shows AnalyzerPU login successful 2 times. Should I only be using one EntityManagerFactory. It seems for every one of those I have it shows login. If I keep going I'll have like 12 logins, 1 for each table in my database. I know this can't be correct?

public class PkgLineControllerImpl extends AbstractController implements PkgLineController {

private EntityManagerFactory emf = null;

public PkgLineControllerImpl() {

    super(StateHistoryImpl.class);
    emf = Persistence.createEntityManagerFactory("StateHistoryAnalyzerPU");
}

public EntityManager getEntityManager() {
    return emf.createEntityManager();
}

@Override
public List<PkgLine> findAll() {
    EntityManager em = getEntityManager();

    List<PkgLine> pkgLineList = new ArrayList<PkgLine>();

    try {
        pkgLineList = em.createNamedQuery("PkgLine.findAll").getResultList();
        return pkgLineList;
    } finally {
        em.close();
    }
}

}

This is how I"m laying my controllers out. Thanks for any help.

Sometimes it will do this.

[EL Info]: 2011-02-01 14:34:07.991--ServerSession(18450577)--EclipseLink, version: Eclipse Persistence Services - 2.0.2.v20100323-r6872
[EL Info]: 2011-02-01 14:34:08.381--ServerSession(18450577)--fileStateHistoryAnalyzerPU login successful
100
200
100
200
100
null
null
null
null
null

Sometimes it will do this.

Persistence Services - 2.0.2.v20100323-r6872
[EL Info]: 2011-02-01 14:34:08.381--ServerSession(18450577)--fileStateHistoryAnalyzerPU login successful
100
200
100
200
100
Persistence Services - 2.0.2.v20100323-r6872
[EL Info]: 2011-02-01 14:34:08.381--ServerSession(18450577)--fileStateHistoryAnalyzerPU login successful
null
null
null
null
null

Where the null is one JPAController class method returning some data and the numbers are the other JPA controller class returning data. I've put one entitymanagerfactory in an AbstractClass but it's still doing it.

You should have one EntityManagerFactory, and one EntityManager per transaction/request/session. You should also have one persistence unit with all of your classes in it.

Technically creating multiple factories with the same persistence unit name and no arguments should share the same persistence unit, cache/connection pool, but if you have different persistence units then each would have its own cache/connection pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM