在glassfish jdbc连接池中指定模式?

时间:2017-04-12 10:11:13

标签: postgresql jpa jdbc glassfish multi-tenant

我已经阅读了一些答案,例如通过附加?searchpath = myschema或?currentSchema = myschema但它仍然不适合我的情况。我使用NetBeans,我可以使用预期的模式执行连接命令,但它运行良好,但在运行时,Glassfish只连接到忽略?currentSchema = myschema的公共模式。我的postgresql版本是9.6,JDBC驱动程序版本是最新的42.0.0

这是我的glassfish-resource.xml:

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_aegwyncreds_dbexerphi_dbaPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="5432"/>
        <property name="databaseName" value="mydb"/>
        <property name="User" value="user"/>
        <property name="Password" value="pass"/>
        <property name="URL" value="jdbc:postgresql://localhost:5432/mydb?currentSchema=myschema"/>
        <property name="driverClass" value="org.postgresql.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/myjndisource" object-type="user" pool-name="post-gre-sql_mydb_user_dbaPool"/>
</resources>

这是我的持久单位:

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <jta-data-source>java:app/myjndisource</jta-data-source>
    <class>myclass</class>
    . . . . . . .  . . .
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:1)

在结合谷歌的一些结果后,我终于明白了这一点,我希望这对像我这样的人有用。以下是我的一些结论。

  1. 在jdbc-conncetion-pool元素中添加currentSchema = myschema,如果在JPA中使用该连接并在持久性单元中指定,则不会更改JPA中的任何内容,如我之前的示例所示:

    的java:应用程序/ myjndisource

    EntityManager仍然使用公共架构并忽略指定的架构。

  2. 我们需要使用orm.xml文件来指定我们想要的架构,如下例所示:

    JPA - EclipseLink - How to change default schema

    orm.xml文件可以使用任何名称设置,但必须位于类路径中。在NetBeans中,您可以使用此文件夹结构作为示例,因为您无法使用内置帮助中的Netbeans生成此文件。

    网页 - &gt; WEB-INF - &gt;课程 - &gt; META-INF - &gt; (映射文件)

  3. Netbeans web application structure

    然后在持久性单元中我们可以指定mapping-file元素,但要记住这两个点

    一个。在Netbeans(或者其他IDES)中,您必须将mapping-file元素放在jta-data-source元素之后但在class元素之前,否则会出现一些部署错误,如此链接:

    https://netbeans.org/bugzilla/show_bug.cgi?id=170348

    B中。如果使用名称orm.xml命名映射文件并将其与持久性单元包含在同一目录中,则自动持久性单元包含该映射文件,尽管您不必使用mapping-file元素指定它。

    因此,如果您有2个映射文件,其中一个名为orm.xml,并且它们都包含架构元素,并且您的持久性单元对另一个映射文件使用mapping-file元素,则会因为架构冲突而出错。