简体   繁体   中英

Spring boot hibernate/jpa batch update not working

application property

spring.datasource.url=jdbc:as400://localhost/

spring.jpa.properties.hibernate.jdbc.batch_size = 30
spring.jpa.properties.hibernate.order_updates = true
spring.jpa.properties.hibernate.batch_versioned_data = true

Entity -

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity(name = "entity")
public class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false);

   @ManyToOne
   @JoinColumn(name = "HDR_ID", referencedColumnName = "HDR_ID")
   @JsonIgnore
   private Header header;

   @ManyToOne
   @JoinColumn(name = "STS_ID", referencedColumnName = "STS_ID")
   private Status status;
}

Service -

this.myRepository.saveAll(MyEntitylist);

Gradle -

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
    implementation 'org.springframework.boot:spring-boot-starter-web'

I am using Spring boot 2.3.5.RELEASE. I am trying to update bulk records using hibernate batch but it seems like it's not working. I am not getting any errors, Records are updating in DB without any problem but there is no performance improvement. Actually, I could not find any difference with or without Hibernate batch process. I checked with logs and their query is logging for every single record, for example, if I try to save 1000 records then there will be 1000 update queries in logs.

I am expecting fewer update queries after batch, depends on batch size and it should take comparatively less time than without batch queries.

Is there any configuration issue in my code? Is there any option like reWriteBatchedInserts for jdbc:as400 to show a multi-row insert query?

Just because you see the queries in the database logs does not mean that batching does not work. JDBC batching is a protocol level optimization and is dependent on the driver support. A JDBC driver may implement this by issuing individual statements. The optimization usually comes from reusing the same server handle though and sending values in batches. The gain might be too small for 1000 elements though. You should consult your driver vendor about this if you do not see any improvements. You are not showing what kind of queries are logged though, are you sure that no other statements are executed in between the update statements?

Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.

https://docs.jboss.org/hibernate/core/4.3/devguide/en-US/html_single/#batch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM