休眠呼叫需要一分多钟

时间:2018-06-18 08:34:11

标签: hibernate

我有一个非常小的数据库,有5个表和2个记录 每件事都运行正常,直到我做了一个DB更改并在eclipse中运行“hibernate代码生成”来自动生成hibernate文件 现在,当我调用使用hibernate 5的Web服务时,执行此记录需要2分钟以上

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

更新: 我忘了提到如果我把战争放在tomcat服务器上(不是我的日食),它需要几秒钟

这是完整的代码

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);

这是我的配置gile

<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;
}

2 个答案:

答案 0 :(得分: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已经在容器启动时初始化了。希望这会有所帮助。

答案 1 :(得分:0)

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

为了解决我添加的问题

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

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

这解决了我的问题