0

I use a springboot application and try to insert 35000 records in the database

Here is the main class

@SpringBootApplication @EnableTransactionManagement public class DemoImmobilierBackApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(DemoImmobilierBackApplication.class, args); } 

Here is the service

@Service public class ReferenceServiceImpl implements ReferenceService { Logger logger = LoggerFactory.getLogger(ReferenceServiceImpl.class); @Autowired private ReferenceRepository referenceRepository; @Transactional public void loadDataBaseCodePostalNomCommune(Map<String,String> tokens) { List<CodePostalNomCommune> list = new ArrayList<CodePostalNomCommune>(); Iterator it = tokens.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); list.add(new CodePostalNomCommune((String)pair.getKey(), (String)pair.getValue())); } referenceRepository.persistCodePostalNomCommune(list); } } 

here is the repository

@Repository public interface ReferenceRepository { void persistCodePostalNomCommune(List<CodePostalNomCommune> list); } and public class ReferenceRepositoryImpl implements ReferenceRepository { Logger logger = LoggerFactory.getLogger(ReferenceRepositoryImpl.class); @PersistenceContext private EntityManager em; @Override @Transactional public void persistCodePostalNomCommune(List<CodePostalNomCommune> list) { logger.info("List<CodePostalNomCommune> SIZE="+list.size()); int i = 0; for (CodePostalNomCommune codePostalNomCommune : list) { em.persist(codePostalNomCommune); i++; if (i%20 == 0 ) { em.flush(); em.clear(); } } } } 

But no data is created in mysql database

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit.

6
  • There are some questions,... who triggers this method, was there entrys in the list, was the persistCodePostalNomCommune triggered ? Can you check it. Commented Oct 25, 2020 at 16:24
  • Hello pL4GGu33, I trigger this method by requesting a list of string. I just took the opportunity of that request to call that method. I checked that this method is called. Everything in the log and the time spent suggest that the persist operation for the 39000 rows are trigerred, but when I inspect the data base table, it is empty Commented Oct 25, 2020 at 17:25
  • Could you try to open and close the transaction by hand. Like em.getTransaction().begin(); and at the end em.getTransaction().commit(); Commented Oct 25, 2020 at 20:04
  • Hi pL4GGu33, I tried what you suggested (em.getTransaction().begin(); and at the end em.getTransaction().commit();) in the two following case: removing and not removing @Transaction annotation, but it gives me the error java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead Commented Oct 26, 2020 at 7:11
  • okay nevermind.... i tried now a simple example with your configuration (incl. @Transactional) and it works out of the box. I am a little bit helpless now why i doesnt work in our case. Have you some special configurations somewhere? Commented Oct 26, 2020 at 16:42

1 Answer 1

1

I found the solution. The datasource was badly configured. In the class of main method, I added the following

@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("password"); dataSource.setUrl( "jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"); return dataSource; 

}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.