Spring jdbctemplate batch update quick alternative

I am trying to insert about 5000 values ​​into a MySql table using J7BC Spring template batch update as shown here.

http://www.mkyong.com/spring/spring-jdbctemplate-batchupdate-example/

As I understand it, it does as many inserts as there are rows I give in one transaction. But it is still slow.

I tried to form a query like

INSERT INTO CUSTOMER " +
    "(CUST_ID, NAME, AGE) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?),(?, ?, ?)....

      

for the number of lines I have. It was much faster, but I had to form the request manually. I wonder if there are alternatives to batch update for such cases?

PS I know the max batch size should be considered, the query size should not exceed the limit (although the limit can be configured on the MySql server) when building such large queries.

+3


source to share


1 answer


You can do it like this



  • Get connection object
  • set the autocommit property of the connection to false. usingconnection.autocommit(false)

  • Run an insert query query.
  • execute connection.commit();

+2


source







All Articles