【发布时间】:2018-07-25 09:34:30
【问题描述】:
我设法使用以下文件部署和使用 JPA 连接。 这目前有效,但并不理想。
连接应该使用 postgres 池驱动程序和 JNDI 数据源,而不是 web.xml 中的数据源。
在 JEE 容器中部署数据库连接的最佳实践方式是什么。
这是我当前的设置,使用未使用池驱动程序的全局连接。
persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pcc" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:global/pccData</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="CONFIG"/>
<property name="eclipselink.jdbc.fetch-size" value="1000"/>
<property name="eclipselink.jdbc.cache-statements" value="true"/>
<property name="eclipselink.persistence-context.flush-mode" value="commit"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
</properties>
</persistence-unit>
</persistence>
web.xml
我确定PGSimpleDataSource 不是推荐的方法
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>PCC Web Application</display-name>
<data-source>
<name>java:global/pccData</name>
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
<server-name>localhost</server-name>
<port-number>5432</port-number>
<database-name>pcc</database-name>
<user>postgres</user>
<password>postgres</password>
<property>
<name>fish.payara.slow-query-threshold-in-seconds</name>
<value>5</value>
</property>
</data-source>
</web-app>
【问题讨论】:
标签: java glassfish jndi payara