有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ORA12518,TNS:侦听器无法切换客户端连接,该连接来自内存访问量大的循环

我有一个从甲骨文访问大量内存的循环

    int firstResult = 0;
    int maxResult = 500;
    int targetTotal = 8000; // more or less

    int phase = 1;
    for (int i = 0; i<= targetTotal; i += maxResult) {
        try {
            Session session = .... init hibernate session ...
            // Start Transaction
            List<Accounts> importableInvAcList = ...getting list using session and firstResult-maxResult...

            List<ContractData> dataList = new ArrayList<>();
            List<ErrorData> errorDataList = new ArrayList<>();

            for (Accounts account : importableInvAcList) {
                ... Converting 500 Accounts object to ContractData object ...
                ... along with 5 more database call using existing session ...
                .. On converting The object we generate thousands of ErrorData...

                dataList.add(.. converted account to Contract data ..);
                errorDataList.add(.. generated error data ..);
            }

            dataList.stream().forEach(session::save); // 500 data

            errorDataList.stream().forEach(session::save); // 10,000-5,000 data

            ... Commit Transaction ...
            phase++;
        } catch (Exception e) {

            return;
        }
    }

在第二阶段(第二个循环)出现异常。有时在第三或第五阶段会出现例外

我还检查了运行时内存

    Runtime runtime = Runtime.getRuntime();
    long total = runtime.totalMemory();
    long free = runtime.freeMemory();
    long used = total - free;
    long max = runtime.maxMemory();

在第二阶段,样本的状态如下

已使用:1022MB,免费:313MB,总分配:1335MB

堆栈跟踪在这里

    org.hibernate.exception.GenericJDBCException: Cannot open connection
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
        at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
        at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
        at ibbl.remote.tx.TxSessionImpl.beginTx(TxSessionImpl.java:41)
        at ibbl.remote.tx.TxController.initPersistence(TxController.java:70)
        at com.ibbl.data.util.CDExporter2.run(CDExporter2.java:130)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12518, TNS:listener could not hand off client connection

注意,这个进程在Thread中运行,一次运行3个类似的Thread为什么循环运行一段时间后会出现此异常


共 (1) 个答案

  1. # 1 楼答案

    there are 3 similar Thread running at a time.

    如果您的代码总共创建了3个线程,那么最好只需要3个Oracle连接。在创建任何线程之前创建所有线程。创建线程,为每个线程分配一个连接,然后启动线程

    不过,很有可能您的代码在托管它的任何机器上都会过度消耗资源。即使您消除了ORA-12518,RDBMS服务器也可能“南下”。所谓“往南走”,我的意思是,如果你的应用程序消耗了太多的资源,托管它的机器或托管RDBMS服务器的机器可能会“恐慌”或其他同样可怕的事情