如何使用 EclipseLink/JPA 启用交换数据库连接参数
我们有各种类型的项目,例如后端 java 进程和前端 tomcat 应用程序,当前使用 EclipseLink/JPA 和 persistence.xml 来获取数据库连接信息。有没有办法允许动态更改这些参数,例如在后端数据库已移动到另一台服务器的情况下。 persistence.xml 打包在 jar 中,不能直接修改。我看到各种讨论动态持久性的线程,但没有讨论这个特定主题。有没有地方用任何例子专门记录这一点?
谢谢
We have projects of various types such as a backend java process and a front-end tomcat app that currently use EclipseLink/JPA and persistence.xml to get the db connection info. Is there a way to allow for those parameters to be dynamically changed, say in the case that the backend db has been moved to another server. The persistence.xml is packaged in the jar and is not directly modifiable. I see various threads that talk about dynamic persistence but nothing on this particular theme. Is there somewhere that documents this specifically with any examples?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的持久性上下文是应用程序管理的(即您使用
Persistence.createEntityManagerFactory(persistenceUnitName)
来获取它),您可以通过传递自定义属性来覆盖persistence.xml
中的属性通过createEntityManagerFactory(persistenceUnitName,属性)。If your persistence context is application-managed (i.e. you use
Persistence.createEntityManagerFactory(persistenceUnitName)
to obtain it), you can override properties frompersistence.xml
by passing your custom properties viacreateEntityManagerFactory(persistenceUnitName, properties)
.您应该做的是将 JDBC 连接信息放在应用程序服务器中,而不是放在 persistence.xml 文件中。
因此,像这样设置 persistence.xml 文件。
然后将应用程序服务器配置为具有名为 jdbc/MyDBDS 的数据库连接。然后,您可以在需要时更新连接信息,而无需更改 persistence.xml 文件。
What you should do is put the JDBC connection information in the application server instead of the persistence.xml file.
So, setup your persistence.xml file like this.
Then configure your application server to have a database connection named jdbc/MyDBDS. You can then update the connection information whenever you need to without changing the persistence.xml file.