總結hibernate優化方法

 今天學到一點點hibernate優化的東西.先記錄下來.

hibernate優化其實做到兩點就行

第一點,配置文件,每次commit提交次數設置好

第二save的時候,及時清除緩存,清除緩存和上面設置的次數保持一致
 
還有一點、有的人會把 show_sql 設爲true..其實這是不正確的
1
  

 
 
2
 
  1. public List hello() 
  2.         { 
  3.             List list=(List) this.getHibernateTemplate().execute(new HibernateCallback() { 
  4.              
  5.                 public Object doInHibernate(Session session) throws HibernateException, 
  6.                         SQLException { 
  7.                     // TODO Auto-generated method stub 
  8.                     String sql="select uid,uname,uport from userlist"
  9.                     List ls=session.createQuery(sql).list(); 
  10.                     return ls; 
  11.                 } 
  12.             }); 
  13.             return list; 
  14.         } 
  15.         public void doInsert() 
  16.         { 
  17.             //hibernate 優化 
  18.             this.getHibernateTemplate().execute(new HibernateCallback() { 
  19.                  
  20.                 public Object doInHibernate(Session session) throws HibernateException, 
  21.                         SQLException { 
  22.                     /* 
  23.                      * 假設一次性插入10W行數據 
  24.                      */ 
  25.                     int i=0,c=0
  26.                     String exeSql="insert into userlist values(?,?,?)"
  27.                     Query query=null
  28.                     long begin =System.currentTimeMillis(); 
  29.                     for(;i<100000;i++) 
  30.                     { 
  31.                         query=session.createQuery(exeSql); 
  32.                         query.setParameter(1,i); 
  33.                         query.setParameter(2,i); 
  34.                         query.setParameter(3,new SimpleDateFormat("yyyy-MM-dd 24HH:mm:ss").format(new Date())); 
  35.                         query.executeUpdate(); 
  36.                         c=c+1
  37.                         if(c%50==0
  38.                         { 
  39.                             session.flush(); 
  40.                             session.clear(); 
  41.                         } 
  42.                     } 
  43.                     long end =System.currentTimeMillis(); 
  44.                     System.out.println("doing time:"+(end-begin)); 
  45.                 return null
  46.                 } 
  47.             }); 
  48.         } 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章