[英]JPA variable persistence units
I'm trying to develop a website using java EE, which will be deployed to a remote server, i am trying to implement JPA into the application. 我正在尝试使用Java EE开发一个网站,该网站将部署到远程服务器,我正在尝试在应用程序中实现JPA。
For testing purposes i'd like to create a variable persistence unit, so that on the local deployment, the application will use my local mySQL server, while on the remote deployment it will use the server's provided mySQL server. 为了进行测试,我想创建一个变量持久性单元,以便在本地部署中,应用程序将使用我的本地mySQL服务器,而在远程部署中,它将使用服务器提供的mySQL服务器。
the problem however is that i'm running on glassfish locally, and jboss remote, so i can't make the resource JNDI names for the datasources the same (since jboss requires "java:/" or "java:jboss/" as a prefix, while glassfish doesn't allow :'s in JNDI names) 但是问题是我在本地的glassfish上运行,并且jboss远程运行,因此我无法使数据源的资源JNDI名称相同(因为jboss需要将“ java:/”或“ java:jboss /”作为前缀,而glassfish不允许使用:的JNDI名称)
another problem is that i'm not simply allowed to create 2 persistence units with the same name, 另一个问题是,我不能简单地创建两个具有相同名称的持久性单元,
i've tried making 2 different persistence units, but then the deployment fails because one of the persistence units fails to resolve. 我尝试制作2个不同的持久性单元,但是部署失败,因为其中一个持久性单元无法解析。
below is my persistence.xml at this time: 以下是我目前的persistence.xml:
<persistence-unit name="LocalPU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/website</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
<persistence-unit name="RemotePU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/website</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
so my question is, is it possible to have an entitymanager resolve to EITHER of these persistence units, but not require both persistence units to be available 所以我的问题是,是否有一个实体管理器可以解决这些持久性单元中的任何一个,但是不要求两个持久性单元都可用
EDIT: 编辑:
about 5 minutes after posting this question, i found an article that suggests using environment variables 发布此问题大约5分钟后,我发现了一篇建议使用环境变量的文章
however this does not seem to work within glassfish, 但是这似乎不适用于玻璃鱼,
this persistence.xml: 这个persistence.xml:
<persistence-unit name="LocalPU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>${myds}</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
and the JVM-option -Dmyds=jndi/website
results in the following error: 并且JVM选项-Dmyds=jndi/website
导致以下错误:
Exception while preparing the app : Invalid resource : ${myds}__pm
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : ${myds}__pm
which leads me to believe environment variables can't be parsed within glassfish (???) 这使我相信在玻璃鱼中无法解析环境变量(???)
after following the tips provided in the first comment above, i have concluded that the question is irrelevant, and that my issues were caused by my misunderstanding of how JNDI names are displayed/parsed differently between glassfish, jboss, and JPA (in my case this has switched to hibernate, since openshift's Jboss servers turned out not to support eclipselink after all) 在遵循了上面第一条评论中提供的技巧之后,我得出结论,这个问题是不相关的,并且我的问题是由于我对在glassfish,jboss和JPA之间如何显示/解析JNDI名称的误解引起的(在我的情况下,已切换为休眠状态,因为openshift的Jboss服务器原来根本不支持eclipselink)
Glassfish, names the JNDI resource as jdbc/website
, yet parses it as java:jdbc/website
Glassfish将JNDI资源命名为jdbc/website
,但将其解析为java:jdbc/website
Jboss on the other hand, requires the "java:" prefix to be defined explicitly, so in order to adress the same data source, it should be named java:jdbc/website
另一方面,Jboss要求明确定义“ java:”前缀,因此,为了获得相同的数据源,应将其命名为java:jdbc/website
i'm not entirely sure how JPA/Java EE parse the JNDI names internally, but it seems as though either version works, so both java:jdbc/website
AND jdbc/website
would succesfully connect to the datasource defined in both the glassfish and the jboss environment. 我不完全确定JPA / Java EE是如何在内部解析JNDI名称的,但是似乎这两个版本都可以工作,因此java:jdbc/website
和jdbc/website
都可以成功连接到glassfish和jboss环境。 at least i've been able to succesfully build to both jboss and glassfish using the java:jdbc/website
datasource name, and i've had the same result with jdbc/website
至少我已经能够使用java:jdbc/website
数据源名称成功地构建到jboss和glassfish,并且与jdbc/website
取得了相同的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.