简体   繁体   中英

HikariPool-1 - Connection is not available when use Threading

Hi Guys, i got following error when I Iterate List and Store in DataBase Using Thread, error come into picture after inserting 1,000 records

error:

 "Thread-2614" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction;

    java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30080ms.

org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection

My Code

while (rowIt.hasNext()) {
                try {
                    Thread t1 = new Thread() {
                        public void run() {

                            DimCompany comp = new DimCompany();
                            Row row = rowIt.next();
                            Cell c = row.getCell(cmpName);
                            if (c != null && c.getCellType() != c.CELL_TYPE_BLANK) {
                                if (cmpName != null && row.getCell(cmpName) != null) {
                                    comp.setCompanyName(row.getCell(cmpName).toString());
                                    comp.setSourecLevel1("MCA");
                                }
                                if (cmpUId != null && row.getCell(cmpUId) != null) {
                                    comp.setCompanyUniqueId(row.getCell(cmpUId).toString());
                                }
                                if (compType != null && row.getCell(compType) != null) {
                                    comp.setCompType(row.getCell(compType).toString());
                                }
                                if (foundedDate != null && row.getCell(foundedDate) != null) {
                                    comp.setFoundedDate(row.getCell(foundedDate).toString());
                                }

                              //save record
                              int i = parseDataDao.saveCompany(comp);
                      }
                      }
                    };
                    t1.start();
                } catch (Exception e) {
                    System.out.println(e);
                }
    }

Unbounded thread creation is the main problem in your code.

By default Hikari has a pool size of 10 connections. You can increase it using:

hikariDataSource.setMaximumPoolSize(...);

You are creating over a thousand threads and running them at once. These are much more in number than the hikari pool size (whatever you have set, or the default as above). So your queries wait in queue to acquire a connection and eventually timeout.

You should use a thread pool. The easiest way to quickly do it is to use:

CompletableFuture.supplyAsync(() -> {/* contents of your run method here */});

This will use the ForkJoin common pool. You can fine tune it by using an implementation of the ExecutorService .

Use a ThreadPool of 10 threads and submit your tasks to it, like below

ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.submit(task1);

This will make sure your task runs async and running out of connections do not happen

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