简体   繁体   中英

Manege jta-data-source in persistence.xml on deploy

There is way to change jta-data-source in persistence.xml on runtime and stay in manage mode

@PersistenceContext(unitName="BookUnit")
private EntityManager em;

(i know i can use EntityManagerFactory but i want to stay in managed mode like i wrote before)

I have more then one environment (local,test,prod) and for each environment there is different jndi datasource i don't wont to change the persistence.xml file for each environment i want to manage it in the code

There are multiple ways of achieving this kind of functionality -

1) Use environmental variables in persistence.xml and initialize them appropriately based on your environment, ie, while starting the server init this variable to appropriate value

2) As your instances for dev, test & production would be different, for the same JNDI name configure your datasource appropriately.

Ex: on your dev server configure your datasoruce to connect to dev DB, similarly for test environment to test DB and in production to Production DB.

I would always go with Option 2 , as this is standard way of doing things.

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