2012-03-14 65 views
0

我記錄了10000條記錄的保存時間,我不知道爲什麼地球上需要40分鐘才能保存10000條記錄。我有一個類,所有的基本會話工廠工作完成,然後擴展(繼承該類到[DAO(nameofentity)]類的名稱,並要求getsession,然後保存功能。爲什麼地球上花費這麼多時間?我有log4j的初始化它向下滾動就像一個大的顯示屏。數據庫記錄在休眠時保存時間非常高!爲什麼?

這裏是基類的在休眠獲得會話代碼。

package DAO; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

public class BaseDAO { 
private static final ThreadLocal<Session> session= new ThreadLocal<Session>(); 

private static final SessionFactory sessionFactory = new Configuration() 
     .configure().buildSessionFactory(); 

public Session getSession() { 

    Session session = (Session) BaseDAO.session.get(); 
    if (session == null) { 
     session = sessionFactory.openSession(); 
     BaseDAO.session.set(session); 
    } 
    return session; 
} 

protected void begin() { 
    getSession().beginTransaction(); 
} 

protected void commit() { 
    getSession().getTransaction().commit(); 

} 


protected void rollback() { 
    try { 
     getSession().getTransaction().rollback(); 
    } catch (HibernateException e) { 
     System.out.println(e.getMessage()); 
    } 
    try { 
     getSession().close(); 
    } catch (HibernateException e) { 
     System.out.println(e.getMessage()); 
    } 
    BaseDAO.session.set(null); 
} 


public void close() { 
    getSession().close(); 
    BaseDAO.session.set(null); 
} 
    } 

然後擴展它

 package DAO; 

    import java.util.Iterator; 

    import org.hibernate.HibernateException; 
    import org.hibernate.Query; 

    import pojo.Address; 
    import pojo.Clinic; 
    import pojo.Patient; 

    public class PatientDAO extends BaseDAO { 


    public PatientDAO(){ 

    } 

    It takes more 4 seconds to add just 100 elements so roughly bout 8 mins to 
    add 10K elements. Is there some thing erong with my code. i.e the 
    SAVE() method Below.And ya i tried removing the additional update that i am doing 
    it did not make much difference. 
    I have beeen asked to bring the time for 10K to <1 min..!! :(please help 

    public void create(Patient p) throws ApplicationException { 
    try { 
     begin(); 
     getSession().save(p); 
     Query query = getSession().createQuery 
      ("update Address patientid=? where id=?"); 

     query.setParameter(0,p.getId()); 
     query.setParameter(1, p.getAddress().getId()); 
     query.executeUpdate(); 
     commit(); 

    } catch (HibernateException e) { 
     rollback(); 
     throw new ApplicationException(e.getCause().getMessage()); 
    } 
} 

Hibernate cfg。

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">self</property> 
    <property name="hibernate.connection.url"> 
    jdbc:mysql://localhost:3306 /rajtest1</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.default_schema">rajtest1</property> 

    <property name="dialect"> 
      org.hibernate.dialect.MySQLDialect 
     </property> 
     <property name="connection.pool_size">10</property> 

    <!-- property name="current_session_context_class">thread</property--> 
    <!--property 
    name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property--> 
     <property name="show_sql">false</property> 
     <property name="hbm2ddl.auto" >update</property> 
    <mapping resource ="Clinic.hbm.xml"/> 
       <mapping resource="Doctor.hbm.xml"/> 
     <mapping resource="Patient.hbm.xml"/> 
     <mapping resource="Address.hbm.xml"/> 
     </session-factory> 
     </hibernate-configuration> 
+1

讓我解釋一下你的問題:*我有一些代碼,它很慢。爲什麼?*你能回答這樣的問題嗎? – 2012-03-14 08:21:55

+0

對不起,我應該知道更詳細的信息。像下面的代碼 – 2012-03-14 12:36:35

+0

對不起@JB Nizet。 。現在我已經把代碼。感謝fr讓我知道 – 2012-03-14 12:54:44

回答

1

第一:

你提交的每一次插入。這是低效的。對許多插入進行一次提交確實更有效。

二:

爲什麼你在創建後病人做地址更新?可能你剛剛創建了地址。將地址定義爲患者對象的成員(例如,具有多對一關係或一對一關係)並將兩者一起插入單個保存(使用級聯)或將該地址插入後患者。在這種情況下,hibernate管理將地址設置爲patientid。

第三:

跟蹤你的SQL語句與

<property name="show_sql">true</property> 
Hibernate的配置文件中

,看看是否有多餘的SQL語句,

四:

直接嘗試插入在SQL命令行中。也許你的數據庫不是更快。

+0

嘿,非常感謝。解決了這個問題不知道爲什麼,但當我提交後開始關閉會話。時間縮短了。 – 2012-03-19 17:58:08

-1

如果你打算保存10000條記錄,你不應該一次只做一條記錄。創建一個proc來執行基於集合的插入或更新,而不是使用n_Hibernate。