2014-01-14 59 views
0

我使用Spring 3.0和休眠4.x,但我面臨事務管理問題。我在服務層使用@Transactional註解,但將大量數據保存到數據庫需要很長時間。使用@Transactional彈簧3.x和休眠4.x事務管理問題使用@Transactional

這裏是我的事務管理器:

<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

而且我使用這行代碼中插入數據:

sessionFactory.getCurrentSession().save(deal); 

請,如果有人有解決方案,幫助我。

+0

有插入或更新操作? –

+0

在一般的解決方案是批量插入: http://stackoverflow.com/questions/9718305/batch-insertions-with-hibernate-spring –

+0

我插入數據到數據庫。 – user3061376

回答

0

@Transactional不對您的表現負責。 對於大數據量的首先使用批處理如下

int i = 0; 
for(Deal deal : deals) 
    session.save(deal); 
    if (i % 20 == 0) { //20, same as the JDBC batch size 
     //flush a batch of inserts and release memory: 
     session.flush(); 
     session.clear(); 
    } 
    i++; 
} 
session.flush(); 
session.clear(); 

閱讀更多關於章13. Batch processing

有時是無法查詢本身引起放緩 - 另一個查詢操作上由於事務隔離和鎖定,該表可以輕鬆插入以減慢速度。 閱讀更多關於這個外部factores

link1

innodb_buffer_pool_size=614M

too many indexes?

Primary Key Genration

Why is MySQL InnoDB insert so slow?

希望這種事情會幫助你