2017-08-15 236 views
1

我想用700K +記錄讀取Excel文件並批量插入MySQL數據庫表中的那些文件。春季數據JPA批量插入非常慢

請注意,Excel解析速度很快,我可以在50秒左右的時間內將我的實體對象放入ArrayList

我正在使用Spring Boot和Spring Data JPA。

下面是我的部分application.properties文件:

hibernate.jdbc.batch_size=1000 
spring.jpa.hibernate.use-new-id-generator-mappings=true 

和我的部分Entity class

@Entity 
@Table(name = "WHT_APPS", schema = "TEST") 
public class WHTApps { 

    @Id 
    @TableGenerator(name = "whtAppsGen", table = "ID_GEN", pkColumnName = "GEN_KEY", valueColumnName = "GEN_VAL") 
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "whtAppsGen") 
    private Long id; 

    @Column(name = "VENDOR_CODE") 
    private int vendorCode; 
    . 
    . 
    . 
    . 

下面是我DAO

@Repository 
@Transactional 
public class JapanWHTDaoImpl implements JapanWHTDao { 

    @Autowired 
    JapanWHTAppsRepository appsRepo; 

    @Override 
    public void storeApps(List<WHTApps> whtAppsList) { 
     appsRepo.save(whtAppsList); 

    } 

及以下Repository類:

@Transactional 
public interface JapanWHTAppsRepository extends JpaRepository<WHTApps, Long> { 

} 

有人請賜教我,我在做什麼不正確嗎?

編輯:

過程沒有完成,最終拋出錯誤: -

2017-08-15 15:15:24.516 WARN 14710 --- [tp1413491716-17] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08S01 
2017-08-15 15:15:24.516 ERROR 14710 --- [tp1413491716-17] o.h.engine.jdbc.spi.SqlExceptionHelper : Communications link failure 

The last packet successfully received from the server was 107,472 milliseconds ago. The last packet sent successfully to the server was 107,472 milliseconds ago. 
2017-08-15 15:15:24.518 INFO 14710 --- [tp1413491716-17] o.h.e.j.b.internal.AbstractBatchImpl  : HHH000010: On release of batch it still contained JDBC statements 
2017-08-15 15:15:24.525 WARN 14710 --- [tp1413491716-17] c.m.v.c3p0.impl.DefaultConnectionTester : SQL State '08007' of Exception tested by statusOnException() implies that the database is invalid, and the pool should refill itself with fresh Connections. 

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown. 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131] 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131] 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131] 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.43.jar:5.1.43] 
    . 
    . 
    . 
    . 
    2017-08-15 15:15:24.526 WARN 14710 --- [tp1413491716-17] c.m.v2.c3p0.impl.NewPooledConnection  : [c3p0] A PooledConnection that has already signalled a Connection error is still in use! 
2017-08-15 15:15:24.527 WARN 14710 --- [tp1413491716-17] c.m.v2.c3p0.impl.NewPooledConnection  : [c3p0] Another error has occurred [ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown. ] which will not be reported to listeners! 

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown. 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131] 

感謝

+1

hibernate文檔有一個部分解釋瞭如何快速批量插入。 –

+0

你能指點我到任何特定的位置嗎? –

+2

就在這裏,在目錄中,在「配料」或「批量插入」下。 Ctrl-F是你的朋友。 http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#batch –

回答

3

我想指出一兩件事。問題可能不僅僅是休眠而是數據庫。

當您在一個事務中插入700k個對象時,它可以存儲在數據庫的回滾段中,等待事務提交。

如果可能的話,將邏輯拆分爲在中間提交。

從主列表創建1k大小的子列表,保存子列表並在每個子列表保存後提交。

+0

我可以看到該過程未完成,並且會引發錯誤最終,請看我最新的問題。可能是我會嘗試你的建議... –

+0

也可能是連接超時。先嚐試一個小列表,例如1k是否有效 – StanislavL

+0

要在數據庫中存儲大約1200條記錄所花費的時間是〜7秒 –