[英]Creating Hibernate SessionFactory using persistence.xml
[英]Using JNDI is significantly slower than explicit connection in persistence.xml (Jetty 9/Hibernate)
在獨立的Jetty 9.4服務器上,我正在部署一個使用休眠JPA的應用程序。 我觀察到以下奇怪行為(可能並不奇怪,我敢肯定我只是缺少一些概念)
如果我在應用程序持久性xml中顯式聲明數據庫連接屬性,則對於從數據庫讀取一些虛擬數據的服務,我將獲得15-20毫秒的響應。 第一次嘗試大約是300毫秒,然后開始合並。
如果我使用JNDI數據源並在持久性xml中引用它,那么我將獲得1秒的初始響應時間,那么在同一本地環境下,它的響應時間為300-400毫秒。
我的問題是那是正常的行為嗎? JNDI總是那么慢還是它可能是一些錯誤的配置? 碼頭實施緩慢嗎?
ps我試圖將PoolDataSource聲明為JNDI資源,但是persistence.xml不接受它。
預先感謝您的回答。
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="flapweb">
<non-jta-data-source>java:comp/env/jdbc/mydatasource</non-jta-data-source>
<properties>
<!--property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5433/postgres" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" /-->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <!-- DB Dialect -->
<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
<property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
<property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
</persistence>
數據源JNDI聲明
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="mydatasource" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mydatasource</Arg>
<Arg>
<New class="org.postgresql.ds.PGSimpleDataSource">
<Set name="databaseName">postgres</Set>
<Set name="serverName">localhost</Set>
<Set name="portNumber">5433</Set>
<Set name="user">user</Set>
<Set name="password">password</Set>
</New>
</Arg>
</New>
</Configure>
好的,經過多次嘗試,我找到了解決方案。
簡短的回答,我必須使用c3po數據源作為JNDI資源,因此檢索到的連接將始終來自於池。 我也不需要任何hibernate.c3p0。*設置。 例如: https : //www.eclipse.org/jetty/documentation/9.4.x/jndi-datasource-examples.html#c3p0-datasource 。 c3po和postgres罐子必須位於碼頭類路徑下。
我以前的解決方案不起作用的原因是因為hibernate似乎沒有將任何c3po /連接池設置應用於JNDI檢索到的數據源,因為它假定它是由應用程序服務器/ servlet容器管理的。
我也不能使用PGConnectionPoolDataSource,因為該類未實現javax.sql.DataSource,因此無法在持久性xml中引用它。
我希望這會在將來對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.