gpt4 book ai didi

java - 对于主页、J2EE,JPA Entitymanagerfactory 花费了 3-4 秒的时间

转载 作者:行者123 更新时间:2023-12-01 13:21:20 26 4
gpt4 key购买 nike

我正在 JBoss 服务器上使用 J2EE 构建一个网站,并且我有一个通过 JPA 连接到的数据库。

但是这一行:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceService");

大约需要 3-4 秒。获得更快速度的正常做法是什么?

我读到:Hibernate faster EntityManagerFactory creation

但它在这里并不适用,因为我没有应用程序,我有一个网站,而且我没有动态 db-url,它总是一样的。

如果它是一个普通的应用程序,我只会在用户需要它之前在初始化时启动一个线程。但它是一个网站,所以我不能这样做。如果我将它存储在服务器上并让用户从前端调用它,我估计当同时有太多用户请求时会出现很大的问题。

这是它抛出的一些日志信息:

WARN HibernatePersistence:58 - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
INFO Environment:239 - HHH000206: hibernate.properties not found
INFO Environment:346 - HHH000021: Bytecode provider name : javassist
DEBUG BasicTypeRegistry:146 - Adding type registration boolean -> org.hibernate.type.BooleanType@764e2837 // this line about 40 times
WARN DriverManagerConnectionProviderImpl:93 - HHH000402: Using Hibernate built-in connection pool (not for production use!)
11:52:30,793 INFO DriverManagerConnectionProviderImpl:166 - HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://...]
11:52:30,793 INFO DriverManagerConnectionProviderImpl:175 - HHH000046: Connection properties: {user=******, password=****}
11:52:30,793 INFO DriverManagerConnectionProviderImpl:180 - HHH000006: Autocommit mode: false
11:52:30,795 INFO DriverManagerConnectionProviderImpl:102 - HHH000115: Hibernate connection pool size: 20 (min=1)
11:52:32,895 INFO Dialect:145 - HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
11:52:32,913 INFO LobCreatorBuilder:123 - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
11:52:33,116 INFO ASTQueryTranslatorFactory:47 - HHH000397: Using ASTQueryTranslatorFactory

来 self 的 persistence.xml:

<persistence-unit name="PlayerService" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>

编辑:

好的,我尝试使用

@PersistenceContext(unitName="PlayerService") private EntityManager em;

但是当我尝试使用它时 em 为空。我有合适的 jar 吗

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<persistence-unit name="PlayerService" transaction-type="JTA">

?我有 hibernate-entitymanager-4.3.1.Final.jar。

我读到一些关于我只能在 bean 中使用 @PersistenceContext(...) 的内容,但是如何将类声明为 bean?

最佳答案

您尝试做的事情通常在 J2SE 应用程序中使用,您需要调用 Persistence bootstrap 来获取对 EntityManagerFactory 的引用并自己完成所有循环,但在 J2EE 中,这种方法不以这种方式使用。

您应该考虑用户容器管理的EntityManager

When a container-managed entity manager is used, the lifecycle of the persistence context is always managed automatically, transparently to the application, and the persistence context is propagated with the JTA transaction.

A container-managed persistence context may be defined to have either a lifetime that is scoped to a single transaction or an extended lifetime that spans multiple transactions, depending on the PersistenceContextType that is specified when its entity manager is created. This specification refers to such persistence contexts as transaction-scoped persistence contexts and extended persistence contexts respectively.

考虑更改您的实现以使用此方法,这样 EntityManager 就可以使用 @PersistenceContext 来使用,并且开始使用 EM 的时间不会超过毫秒。

@PersistenceContext(unitName="")
EntityManager em;

更改它非常简单,只需将 persistence.xml 添加到您的 Web 应用程序的 META-INF 中,确保您有提供的 jar。

The persistence scope of the container managed entity manager is Transaction by default. The transaction-type is always JTA.

为了能够使用 EJB,请使用此方法。

@Stateless
public class TriggerPersister {

@PersistenceContext(unitName="PlayerService")
private EntityManager entityManager;

不要忘记放置 META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">


<!-- Add the persistence context for OrderDetail -->
<persistence-unit name="PlayerService"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDs</jta-data-source>
<class>our.class<class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>

您在 jboss 的部署文件夹中应该有一个有效的数据源。 jta-data-source 标记指示将使用该应用程序的内容

检查此链接 https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/Connectors_on_JBoss-Configuring_JDBC_DataSources.html

关于java - 对于主页、J2EE,JPA Entitymanagerfactory 花费了 3-4 秒的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22012233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com