Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 休眠呼叫需要一分钟以上的时间_Hibernate - Fatal编程技术网

Hibernate 休眠呼叫需要一分钟以上的时间

Hibernate 休眠呼叫需要一分钟以上的时间,hibernate,Hibernate,我有一个非常小的数据库,有5个表和2条记录 在我更改数据库并在eclipse中运行“hibernate代码生成”以自动生成hibernate文件之前,一切都很正常 现在,当我调用使用hibernate 5的web服务时,执行此记录需要2分钟以上的时间 this.sessionFactory = this.configuration.buildSessionFactory(this.sr); 更新: 我忘了提到,如果我把war放在tomcat服务器上(不是我的eclipse),需要几秒钟 这是完

我有一个非常小的数据库,有5个表和2条记录 在我更改数据库并在eclipse中运行“hibernate代码生成”以自动生成hibernate文件之前,一切都很正常 现在,当我调用使用hibernate 5的web服务时,执行此记录需要2分钟以上的时间

this.sessionFactory = this.configuration.buildSessionFactory(this.sr);
更新: 我忘了提到,如果我把war放在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);
这是我的配置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>

假的
org.postgresql.Driver
密码
jdbc:postgresql://xxxxxxxxx/yyy
用户
org.hibernate.dialogue.PostgreSqlDialogue
真的
真的
假的
下面是一个自动生成的文件示例

@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;
}
@实体
@表(name=“categories”,schema=“edi\u ms”)
公共类类别实现java.io.Serializable{
私人长类别;
私有字符串categoryName;
私人日期生效日期;
私人日期到期日期;
私有集分类应答=新哈希集(0);
公共类别(){
}
公共类别(长类别ID、字符串类别名称、日期生效日期){
this.categoryId=categoryId;
this.categoryName=categoryName;
this.effectiveDate=effectiveDate;
}
公共类别(长类别ID、字符串类别名称、日期生效日期、日期到期日期、,
设置类别(回答){
this.categoryId=categoryId;
this.categoryName=categoryName;
this.effectiveDate=effectiveDate;
this.expirationDate=到期日期;
this.categoriesansweres=categoriesansweres;
}
@身份证
@列(name=“category\u id”,unique=true,nullable=false)
公共长getCategoryId(){
返回此.categoryId;
}
公共无效设置类别ID(长类别ID){
this.categoryId=categoryId;
}
@列(name=“category\u name”,null=false,长度=20)
公共字符串getCategoryName(){
返回this.categoryName;
}
公共无效setCategoryName(字符串categoryName){
this.categoryName=categoryName;
}
@时态(TemporalType.DATE)
@列(name=“生效日期”,null=false,长度=13)
公共日期getEffectiveDate(){
返回此.effectiveDate;
}
公共无效设置生效日期(日期生效日期){
this.effectiveDate=effectiveDate;
}
@时态(TemporalType.DATE)
@列(name=“到期日”,长度=13)
公共日期getExpirationDate(){
返回此。过期日期;
}
public void setExpirationDate(日期expirationDate){
this.expirationDate=到期日期;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“categories”)
公共集getCategoriesAnswerses(){
返回此.categoriesansweres;
}
公共无效集合分类应答(集合分类应答){
this.categoriesansweres=categoriesansweres;
}

首先查看
SessionFactory中发生的事情。Hibernate提供了一个JMX连接器,请参阅

如果启用
hibernate.generate_statistics
configuration属性,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在容器启动时就已经初始化了。希望这能有所帮助。

我们认为问题在于,由于服务器位于数据库附近,所以工作正常 还曾在DB附近的PC上工作

为了解决这个问题,我添加了

<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
false
到hibernate.cfg.xml,以防止hibernate转到数据库并验证结构


这解决了我的问题

谢谢朱利叶斯,但它仍然不起作用,我无法添加统计数据,因为统计方法是在需要时间构建的对象上,这意味着在创建统计数据之前问题已经出现