mysql - HIbernate batch insert or update not working in spring boot -


i need update bulk inserts(nearly 10000) in mysql database. using jpa/hibernate , spring boot. read performing bulk insert/update in hibernate hibernate documentation ,i think code not working sequentially inserting hibernate queries instead performing in group. below code. am missing something?

here datasource configuration.

@component public class datasource  `enter code here`{     @autowired      envconfiguration configuration;      private static final logger logger = loggerfactory.getlogger(datasource.class);      @bean     public datasource datasource(){         logger.info("datasource bean creation...");         drivermanagerdatasource datasource = new drivermanagerdatasource();         datasource.setdriverclassname(configuration.getdbdriver());         datasource.seturl("jdbc:mysql://"+configuration.getdbip()+":"+configuration.getdbport()+"/"+configuration.getdbname()+"?autoreconnect=true&usessl=false");         datasource.setusername(configuration.getdbuser());         datasource.setpassword(configuration.getdbpass().trim());         return datasource;     }     @bean     public hibernatejpasessionfactorybean sessionfactory() {         return new hibernatejpasessionfactorybean();     }  } 

code role domain

//role.java      @entity     @table(name = "role",uniqueconstraints = @uniqueconstraint(             columnnames = { "rolename"}))     public class role {          @id         @generatedvalue(strategy = generationtype.auto)         private long roleid;         @notnull         private string rolename;         public role(){}          public role(string rolename){             this.rolename = rolename;         }          public long getroleid() {             return roleid;         }          public void setroleid(long roleid) {             this.roleid = roleid;         }          public string getrolename() {             return rolename;         }          public void setrolename(string rolename) {             this.rolename = rolename;         }       } 

below service code. here manully flushing sesssion. have added sleep function in order find whether insert query executed 1 one or executing in batch of 10 happen in jdbc batch.

@service public class roleservice{       @autowired     private sessionfactory factory;      @autowired     private datasource source;      private static final logger logger =  loggerfactory.getlogger(walletservice.class);       public void insertrole(collection<registerwallet> walletmetacollection){              if(factory==null){                     system.out.println("factory null");                 }else{                     system.out.println("factory working");                     session session = factory.opensession();                     transaction tx = session.begintransaction();                      ( int i=0; i<100000; i++ ) {                     role role=new role(""+i);                     session.persist(role);                 system.out.println("this role id "+role.getroleid());                      try {                         thread.sleep(1000);                     } catch (interruptedexception e){                         // todo auto-generated catch block                         e.printstacktrace();                     }                 if ( % 10 == 0 ) { //20, same jdbc batch size                  //flush batch of inserts , release memory:                     session.flush();                     session.clear();                 }             }                        tx.commit();                     session.close();                 }                }            } 

as per understanding on batch operation 10 roles should inserted @ once avoid number of jdbc round trips used. output of above code quiet unexpected. executing 1 insert per session.persist(..) call.

//this log of above code.        hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 14     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 15     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 16     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 17     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 18     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 19     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 20     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 21     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 22     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 23     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 24     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 25     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 26     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 27     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 28     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 29     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 30     hibernate: insert role (active, role_description, role_name) values (?, ?, ?)     role id 31     </pre>  ------------------------------------------------------------- 

following application.properties configuration

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.springsessioncontext spring.jpa.properties.hibernate.jdbc.batch_size=10 

am missing something?

please help.

you are't missing anything.your output quite normal.you can more info on link: [1]http://www.dineshonjava.com/2012/06/hibernate-batch-processing_10.html


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -