【问题标题】:hibernate call takes more then a minute休眠调用需要一分钟以上
【发布时间】:2018-06-26 21:15:21
【问题描述】:

我有一个非常小的数据库,有 5 个表和 2 个记录 在我进行数据库更改并在 Eclipse 中运行“休眠代码生成”以自动生成休眠文件之前,一切都运行良好 现在,当我调用使用休眠 5 的 Web 服务时,执行此记录需要超过 2 分钟

this.sessionFactory = this.configuration.buildSessionFactory(this.sr);

更新: 我忘了说如果我把战争放在一个tomcat服务器上(不是我的eclipse),它需要几秒钟

这是完整的代码

Configuration configuration;
ServiceRegistry sr;
SessionFactory sessionFactory;

在init方法中

this.configuration = new Configuration().configure();
configuration.addClass(Surveys.class);
configuration.addClass(MembersAnswers.class);
configuration.addClass(Categories.class);
configuration.addClass(PossibleAnswers.class);
configuration.addClass(Questions.class);
configuration.addClass(CategoriesAnswers.class);
this.sr = new StandardServiceRegistryBuilder().applySettings( 
this.configuration.getProperties()).build();
this.sessionFactory = this.configuration.buildSessionFactory(this.sr);

这是我的配置文件

<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.url">jdbc:postgresql://xxxxxxxxx/yyy</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.enable_lazy_load_no_trans">true</property>
    <property name="hibernate.search.autoregister_listeners">true</property>
    <property name="hibernate.validator.apply_to_ddl">false</property>
    <mapping class="DBFiles.MembersAnswers"/>
    <mapping class="DBFiles.PossibleAnswers"/>
    <mapping class="DBFiles.CategoriesAnswers"/>
    <mapping class="DBFiles.Questions"/>
    <mapping class="DBFiles.Categories"/>
    <mapping class="DBFiles.Surveys"/>
</session-factory>

这是自动生成的文件之一的示例

@Entity
@Table(name = "categories", schema = "edi_ms")
public class Categories implements java.io.Serializable {

private long categoryId;
private String categoryName;
private Date effectiveDate;
private Date expirationDate;
private Set<CategoriesAnswers> categoriesAnswerses = new HashSet<CategoriesAnswers>(0);

public Categories() {
}

public Categories(long categoryId, String categoryName, Date effectiveDate) {
    this.categoryId = categoryId;
    this.categoryName = categoryName;
    this.effectiveDate = effectiveDate;
}

public Categories(long categoryId, String categoryName, Date effectiveDate, Date expirationDate,
        Set<CategoriesAnswers> categoriesAnswerses) {
    this.categoryId = categoryId;
    this.categoryName = categoryName;
    this.effectiveDate = effectiveDate;
    this.expirationDate = expirationDate;
    this.categoriesAnswerses = categoriesAnswerses;
}

@Id

@Column(name = "category_id", unique = true, nullable = false)
public long getCategoryId() {
    return this.categoryId;
}

public void setCategoryId(long categoryId) {
    this.categoryId = categoryId;
}

@Column(name = "category_name", nullable = false, length = 20)
public String getCategoryName() {
    return this.categoryName;
}

public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

@Temporal(TemporalType.DATE)
@Column(name = "effective_date", nullable = false, length = 13)
public Date getEffectiveDate() {
    return this.effectiveDate;
}

public void setEffectiveDate(Date effectiveDate) {
    this.effectiveDate = effectiveDate;
}

@Temporal(TemporalType.DATE)
@Column(name = "expiration_date", length = 13)
public Date getExpirationDate() {
    return this.expirationDate;
}

public void setExpirationDate(Date expirationDate) {
    this.expirationDate = expirationDate;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "categories")
public Set<CategoriesAnswers> getCategoriesAnswerses() {
    return this.categoriesAnswerses;
}

public void setCategoriesAnswerses(Set<CategoriesAnswers> categoriesAnswerses) {
    this.categoriesAnswerses = categoriesAnswerses;
}

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    首先查看SessionFactory 内部发生的情况。 Hibernate 提供了一个 JMX 连接器,请参阅documentation here

    如果启用hibernate.generate_statistics 配置属性,Hibernate 将 通过SessionFactory.getStatistics() 公开一些指标。 Hibernate 甚至可以 配置为通过 JMX 公开这些统计信息。 这样,您可以访问包含各种类型的 Statistics 类 二级缓存指标。

    然后您可以开始研究您的热点以及如何重构它们。您确实需要从收集更多指标开始。现在它可能是任何事情,包括 GC 问题。

    或者
    似乎SessionFactory 被创建了好几次。您需要确保 SessionFactory 被创建一次。

    public class HibernateUtil {
      // Private constructor; Class cannot be initialized
      private HibernateUtil() {}
    
      private static final SessionFactory sessionFactory;
      // create sessionFactory only once   
      static {
        // A SessionFactory is set up once for an application!
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure() // configures settings from hibernate.cfg.xml
            .build();
        try {
          sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
          // The registry would be destroyed by the SessionFactory, but we had trouble building
          // the SessionFactory so destroy it manually.
          StandardServiceRegistryBuilder.destroy( registry );
        }
      }
    
      public static SessionFactory getSessionFactory() {
        return sessionFactory;
      }
    }
    

    现在您可以在代码中调用初始化的SessionFactory

    Session session = HibernateUtil.getSessionFactory().openSession();
    

    这样,Hibernate 已经在容器启动时初始化了。希望这会有所帮助。

    【讨论】:

    • 谢谢 Julius ,但它仍然无法正常工作,我无法添加统计信息,因为统计方法适用于需要时间构建的对象,这意味着在创建统计信息之前就出现了问题
    【解决方案2】:

    我们认为问题在于,由于服务器靠近数据库,因此工作正常 还在靠近数据库的 PC 上工作

    为了解决我添加的问题

    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    

    到 hibernate.cfg.xml 以防止休眠进入数据库并验证结构。

    这解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多