How to define two persistence units (one for production, one for unit testing)?

后端 未结 4 1000
一整个雨季
一整个雨季 2020-12-31 14:25

This is my stateless bean:

@Stateless
public class Finder {
  @PersistenceContext(unitName = \"production\")
  EntityManager em;
  [...]
}

4条回答
  •  太阳男子
    2020-12-31 14:46

    I had a similar problem and I want to provide another approach. I wanted to run test an prod, but I don't use two persistence.xml or code modifications. I just have one persistence unit, but different runtime environments (standalone.xml, Wildfly). Let's say that I want run against my development database, I start Wildfly with the test runtime environment. When I want simulate it as real user I run against the prod environment. The only difference is the datasource entry in standalone.xml. The descriptor is always the same (e.g. myappDS, so the persistence unit declaration in persistence.xml is always the same too), but in test server the datasource entry points to my test database, in prod the datasource entry points to my prod database.

提交回复
热议问题