有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA Entitymanagerfactory对于主页J2EE来说花费了34秒的时间

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

但这句话:

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

大约需要3-4秒。通常的做法是什么来加快速度

我读到:Hibernate faster EntityManagerFactory creation

但它不适用于这里,因为我没有应用程序,我有网站,我没有动态数据库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

因为我的坚持。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是空的。我有适合你的罐子吗

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

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

?? 我有hibernate-entitymanager-4.3.1。最终的罐子

我读了一些关于@PersistenceContext(…)的文章仅在bean中,但如何将类声明为bean


共 (1) 个答案

  1. # 1 楼答案

    在J2SE应用程序中,您通常需要调用持久性引导程序来获取对EntityManagerFactory的引用,并自己完成整个周期,但在J2EE中,这种方法并没有以这种方式使用

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

    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.

    考虑改变你的实现,使用这个方法,当EntyMealMeor可以使用@持久化文本时,不要超过MS开始使用EM.P/P>

    @PersistenceContext(unitName="")
    EntityManager em;
    

    对其进行更改非常简单,只需添加持久性。xml到META-INF中的web应用程序,请确保您拥有提供的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/持久性。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的deploy文件夹中应该有一个有效的数据源。以及jta-data-source标记,以指示将使用该应用程序的内容

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