[英]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 有一种方法可以在运行时在persistence.xml中更改jta-data-source并保持在管理模式下
@PersistenceContext(unitName="BookUnit")
private EntityManager em;
(i know i can use EntityManagerFactory but i want to stay in managed mode like i wrote before) (我知道我可以使用EntityManagerFactory,但我想像我之前写的那样保持在托管模式下)
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 我有一个以上的环境(本地,测试,产品),并且对于每个环境,都有不同的jndi数据源,我不会为每个要在代码中管理它的环境更改
persistence.xml
文件
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 1)在persistence.xml中使用环境变量,并根据您的环境适当地对其进行初始化,即在启动服务器时将此变量初始化为适当的值
2) As your instances for dev, test & production would be different, for the same JNDI name configure your datasource appropriately. 2)由于开发,测试和生产的实例会有所不同,因为相同的JNDI名称会适当地配置您的数据源。
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. 我将始终选择选项2 ,因为这是标准的处理方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.