简体   繁体   English

SQLNonTransientConnectionException: 地址已在使用中

[英]SQLNonTransientConnectionException: Address already in use

I am using JdbcTemplate mechanism of Spring framework .我正在使用Spring 框架的JdbcTemplate机制。 I have also scheduler classes to parse the large XML files (>500mb) which are scheduled with cron expressions in xml based configuration with pool-size =10 .When a scheduler reads an XML file , parse each element and insert into mariadb database, it insert around 8000 records but after that it freezes or skips some records with following exception:我还有调度程序类来解析大型 XML 文件(> 500mb),这些文件在基于 xml 的配置中使用 cron 表达式调度,pool-size =10。当调度程序读取 XML 文件时,解析每个元素并插入到 mariadb 数据库中,它插入大约 8000 条记录,但之后它会冻结或跳过一些记录,但有以下异常:

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=3307)(type=master) : Address already in use: connect
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:245)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy36.findAccountByACCust(Unknown Source)
    at com.intersect.aml.service.impl.AccountMasterServiceImpl.findAccountByACCust(AccountMasterServiceImpl.java:147)
    at com.intersect.aml.schedule.CbsAcctMasterXmlScheduler.xmlParsingToEntity(CbsAcctMasterXmlScheduler.java:182)
    at com.intersect.aml.schedule.CbsAcctMasterXmlScheduler.execute(CbsAcctMasterXmlScheduler.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=3307)(type=master) : Address already in use: connect
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:234)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.connException(ExceptionMapper.java:95)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1132)
    at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:560)
    at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:174)
    at org.mariadb.jdbc.Driver.connect(Driver.java:92)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:204)
    ... 24 more
Caused by: java.net.BindException: Address already in use: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:406)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1124)
    ... 34 more

My xml parser class snippet is as follows:我的 xml 解析器类片段如下:

public class ModelXmlScheduler {
........
            File fXmlFile = new File(xmlFile);
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder;
                    Document doc = null;
                    try {
                        dBuilder = dbFactory.newDocumentBuilder();
                        doc = dBuilder.parse(fXmlFile);
                    } catch (ParserConfigurationException e) {
                        System.out.println("ParserConfigurationException:" + e.getMessage());
                        e.printStackTrace();
                    } catch (SAXException ee) {
                        System.out.println("SAXException" + ee.getMessage());
                        ee.printStackTrace();
                    }
                    doc.getDocumentElement().normalize();
                    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                    NodeList dataElement = doc.getElementsByTagName("row");
                    if (dataElement.getLength() > 0) {

                        xmlParsingToEntity(dataElement);
                    }
    .
    .
    .
    .
    .
            boolean xmlParsingToEntity(NodeList nodeList) {
                Modal dto = null;
                for (int i = 0; i < nodeList.getLength(); i++){
                    dto = new Modal();        
     dto.setField(nodeElement.getElementsByTagName("tag_name").item(0).getTextContent());


       int exists = modalService.findByNumber(dto.getNumber());
        if(exists>0)
           modalService.updateModel(dto);
        else
           modalService.insertModel(dto);
        dto=null;
        }

My scheduler configuration xml :我的调度程序配置 xml :

.
.
.
<task:scheduled-tasks scheduler="xmlSchedulers">
...
<task:scheduled ref="ModalXmlScheduler" method="execute" cron="0 01 21 * * *" />
...
</task:scheduled-tasks>
<task:scheduler id="cbsXmlSchedulers" pool-size="1" />

Also schedulerConfig.java:还有 schedulerConfig.java:

.
.
.
     @Bean(destroyMethod="shutdown")
        public Executor taskExecutor() {
            return Executors.newScheduledThreadPool(1);
        }
}

I have already set Heap memory to 2gb for both Java and tomcat, and there are five such schedulars for respective xml files to read data and insert into database.All the threads goes in TIME_WAIT state please help to resolve address already in use exception in this case我已经将 Java 和 tomcat 的堆内存设置为 2gb,并且有五个这样的调度程序用于相应的 xml 文件读取数据并插入数据库。所有线程都处于TIME_WAIT状态,请帮助解决此地址已在使用中的异常案件

  1. switched to prepared statements for finding , bulk inserting and bulk updating records切换到用于查找、批量插入和批量更新记录的准备好的语句

  2. used the pool datasource and set max & min idle,wait and active time following properties in application.properties file使用池数据源并根据application.properties 文件中的属性设置最大和最小空闲、等待和活动时间

    datasource.type =org.apache.tomcat.jdbc.pool.DataSource datasource.initial-size=15 datasource.max-wait=6000 datasource.max-active=50 datasource.max-idle=3600 datasource.min-idle=8

  3. used the StAX parser approach to read large sized XML files使用StAX 解析器方法读取大型 XML 文件

    JournalDev StAX Parser example JournalDev StAX 解析器示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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